blob: 139aea1a2a04783958ac652291f4a0b7b08ed32f [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg380deee2012-05-21 17:12:41 -040011 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040027
28#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040030#include <stdio.h>
31#include <string.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <unistd.h>
37#include <signal.h>
Pekka Paalanen505237e2016-12-01 15:41:11 +020038#include <assert.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040039#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050040#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040041
Daniel Stone67fe3db2016-10-31 14:51:18 +000042#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040043#include "xwayland.h"
Quentin Glidic955cec02016-08-12 10:41:35 +020044#include "xwayland-internal-interface.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040045
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080046#include "cairo-util.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040047#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070048#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040049
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070050struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070051 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070052 int32_t x, y;
53 int32_t width, height; /* should set so old wm's don't mess up */
54 int32_t min_width, min_height;
55 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070056 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070057 struct {
58 int32_t x;
59 int32_t y;
60 } min_aspect, max_aspect;
61 int32_t base_width, base_height;
62 int32_t win_gravity;
63};
64
65#define USPosition (1L << 0)
66#define USSize (1L << 1)
67#define PPosition (1L << 2)
68#define PSize (1L << 3)
69#define PMinSize (1L << 4)
70#define PMaxSize (1L << 5)
71#define PResizeInc (1L << 6)
72#define PAspect (1L << 7)
73#define PBaseSize (1L << 8)
74#define PWinGravity (1L << 9)
75
Kristian Høgsberg380deee2012-05-21 17:12:41 -040076struct motif_wm_hints {
77 uint32_t flags;
78 uint32_t functions;
79 uint32_t decorations;
80 int32_t input_mode;
81 uint32_t status;
82};
83
84#define MWM_HINTS_FUNCTIONS (1L << 0)
85#define MWM_HINTS_DECORATIONS (1L << 1)
86#define MWM_HINTS_INPUT_MODE (1L << 2)
87#define MWM_HINTS_STATUS (1L << 3)
88
89#define MWM_FUNC_ALL (1L << 0)
90#define MWM_FUNC_RESIZE (1L << 1)
91#define MWM_FUNC_MOVE (1L << 2)
92#define MWM_FUNC_MINIMIZE (1L << 3)
93#define MWM_FUNC_MAXIMIZE (1L << 4)
94#define MWM_FUNC_CLOSE (1L << 5)
95
96#define MWM_DECOR_ALL (1L << 0)
97#define MWM_DECOR_BORDER (1L << 1)
98#define MWM_DECOR_RESIZEH (1L << 2)
99#define MWM_DECOR_TITLE (1L << 3)
100#define MWM_DECOR_MENU (1L << 4)
101#define MWM_DECOR_MINIMIZE (1L << 5)
102#define MWM_DECOR_MAXIMIZE (1L << 6)
103
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700104#define MWM_DECOR_EVERYTHING \
105 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
106 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
107
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400108#define MWM_INPUT_MODELESS 0
109#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
110#define MWM_INPUT_SYSTEM_MODAL 2
111#define MWM_INPUT_FULL_APPLICATION_MODAL 3
112#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
113
114#define MWM_TEAROFF_WINDOW (1L<<0)
115
116#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
117#define _NET_WM_MOVERESIZE_SIZE_TOP 1
118#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
119#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
120#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
121#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
122#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
123#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
124#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
125#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
126#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
127#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
128
Pekka Paalanen505237e2016-12-01 15:41:11 +0200129struct weston_output_weak_ref {
130 struct weston_output *output;
131 struct wl_listener destroy_listener;
132};
133
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400134struct weston_wm_window {
135 struct weston_wm *wm;
136 xcb_window_t id;
137 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500138 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400139 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700140 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400141 struct weston_surface *surface;
Quentin Glidic955cec02016-08-12 10:41:35 +0200142 struct weston_desktop_xwayland_surface *shsurf;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400143 struct wl_listener surface_destroy_listener;
144 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400145 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400146 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300147 int pid;
148 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400149 char *class;
150 char *name;
151 struct weston_wm_window *transient_for;
152 uint32_t protocols;
153 xcb_atom_t type;
154 int width, height;
155 int x, y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200156 bool pos_dirty;
Pekka Paalanen505237e2016-12-01 15:41:11 +0200157 struct weston_output_weak_ref legacy_fullscreen_output;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500158 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400159 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300160 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500161 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500162 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700163 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200164 int maximized_vert;
165 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700166 struct wm_size_hints size_hints;
167 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700168 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400169};
170
171static struct weston_wm_window *
172get_wm_window(struct weston_surface *surface);
173
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400174static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200175weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
176
177static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400178weston_wm_window_schedule_repaint(struct weston_wm_window *window);
179
Pekka Paalanen505237e2016-12-01 15:41:11 +0200180static int
181legacy_fullscreen(struct weston_wm *wm,
182 struct weston_wm_window *window,
183 struct weston_output **output_ret);
184
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700185static void
186xserver_map_shell_surface(struct weston_wm_window *window,
187 struct weston_surface *surface);
188
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400189static int __attribute__ ((format (printf, 1, 2)))
190wm_log(const char *fmt, ...)
191{
192#ifdef WM_DEBUG
193 int l;
194 va_list argp;
195
196 va_start(argp, fmt);
197 l = weston_vlog(fmt, argp);
198 va_end(argp);
199
200 return l;
201#else
202 return 0;
203#endif
204}
205
206static int __attribute__ ((format (printf, 1, 2)))
207wm_log_continue(const char *fmt, ...)
208{
209#ifdef WM_DEBUG
210 int l;
211 va_list argp;
212
213 va_start(argp, fmt);
214 l = weston_vlog_continue(fmt, argp);
215 va_end(argp);
216
217 return l;
218#else
219 return 0;
220#endif
221}
222
Pekka Paalanen505237e2016-12-01 15:41:11 +0200223static void
224weston_output_weak_ref_init(struct weston_output_weak_ref *ref)
225{
226 ref->output = NULL;
227}
228
229static void
230weston_output_weak_ref_clear(struct weston_output_weak_ref *ref)
231{
232 if (!ref->output)
233 return;
234
235 wl_list_remove(&ref->destroy_listener.link);
236 ref->output = NULL;
237}
238
239static void
240weston_output_weak_ref_handle_destroy(struct wl_listener *listener, void *data)
241{
242 struct weston_output_weak_ref *ref;
243
244 ref = wl_container_of(listener, ref, destroy_listener);
245 assert(ref->output == data);
246
247 weston_output_weak_ref_clear(ref);
248}
249
250static void
251weston_output_weak_ref_set(struct weston_output_weak_ref *ref,
252 struct weston_output *output)
253{
254 weston_output_weak_ref_clear(ref);
255
256 if (!output)
257 return;
258
259 ref->destroy_listener.notify = weston_output_weak_ref_handle_destroy;
260 wl_signal_add(&output->destroy_signal, &ref->destroy_listener);
261 ref->output = output;
262}
263
Derek Foreman49372142015-04-09 10:51:22 -0500264static bool __attribute__ ((warn_unused_result))
265wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
266 struct weston_wm_window **window)
267{
268 *window = hash_table_lookup(wm->window_hash, hash);
269 if (*window)
270 return true;
271 return false;
272}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400273
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400274const char *
275get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
276{
277 xcb_get_atom_name_cookie_t cookie;
278 xcb_get_atom_name_reply_t *reply;
279 xcb_generic_error_t *e;
280 static char buffer[64];
281
282 if (atom == XCB_ATOM_NONE)
283 return "None";
284
285 cookie = xcb_get_atom_name (c, atom);
286 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500287
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300288 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500289 snprintf(buffer, sizeof buffer, "%.*s",
290 xcb_get_atom_name_name_length (reply),
291 xcb_get_atom_name_name (reply));
292 } else {
293 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
294 }
295
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400296 free(reply);
297
298 return buffer;
299}
300
Tiago Vignatti90fada42012-07-16 12:02:08 -0400301static xcb_cursor_t
302xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
303{
304 xcb_connection_t *c = wm->conn;
305 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
306 xcb_screen_t *screen = s.data;
307 xcb_gcontext_t gc;
308 xcb_pixmap_t pix;
309 xcb_render_picture_t pic;
310 xcb_cursor_t cursor;
311 int stride = img->width * 4;
312
313 pix = xcb_generate_id(c);
314 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
315
316 pic = xcb_generate_id(c);
317 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
318
319 gc = xcb_generate_id(c);
320 xcb_create_gc(c, gc, pix, 0, 0);
321
322 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
323 img->width, img->height, 0, 0, 0, 32,
324 stride * img->height, (uint8_t *) img->pixels);
325 xcb_free_gc(c, gc);
326
327 cursor = xcb_generate_id(c);
328 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
329
330 xcb_render_free_picture(c, pic);
331 xcb_free_pixmap(c, pix);
332
333 return cursor;
334}
335
336static xcb_cursor_t
337xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
338{
339 /* TODO: treat animated cursors as well */
340 if (images->nimage != 1)
341 return -1;
342
343 return xcb_cursor_image_load_cursor(wm, images->images[0]);
344}
345
346static xcb_cursor_t
347xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
348{
349 xcb_cursor_t cursor;
350 XcursorImages *images;
351 char *v = NULL;
352 int size = 0;
353
354 if (!file)
355 return 0;
356
357 v = getenv ("XCURSOR_SIZE");
358 if (v)
359 size = atoi(v);
360
361 if (!size)
362 size = 32;
363
364 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300365 if (!images)
366 return -1;
367
Tiago Vignatti90fada42012-07-16 12:02:08 -0400368 cursor = xcb_cursor_images_load_cursor (wm, images);
369 XcursorImagesDestroy (images);
370
371 return cursor;
372}
373
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400374void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400375dump_property(struct weston_wm *wm,
376 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400377{
378 int32_t *incr_value;
379 const char *text_value, *name;
380 xcb_atom_t *atom_value;
381 int width, len;
382 uint32_t i;
383
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400384 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400386 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400387 return;
388 }
389
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400390 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
391 get_atom_name(wm->conn, reply->type),
392 reply->format,
393 xcb_get_property_value_length(reply),
394 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395
396 if (reply->type == wm->atom.incr) {
397 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400398 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399 } else if (reply->type == wm->atom.utf8_string ||
400 reply->type == wm->atom.string) {
401 text_value = xcb_get_property_value(reply);
402 if (reply->value_len > 40)
403 len = 40;
404 else
405 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400406 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400407 } else if (reply->type == XCB_ATOM_ATOM) {
408 atom_value = xcb_get_property_value(reply);
409 for (i = 0; i < reply->value_len; i++) {
410 name = get_atom_name(wm->conn, atom_value[i]);
411 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400412 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400413 width = 4;
414 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400415 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400416 }
417
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400418 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400419 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400420 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400421 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400422 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 }
424}
425
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200426static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400427read_and_dump_property(struct weston_wm *wm,
428 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400429{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400430 xcb_get_property_reply_t *reply;
431 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400432
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400433 cookie = xcb_get_property(wm->conn, 0, window,
434 property, XCB_ATOM_ANY, 0, 2048);
435 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400436
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400437 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400438
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400439 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400440}
441
Pekka Paalanen20111d52016-12-01 17:07:12 +0200442/* We reuse some predefined, but otherwise useles atoms
443 * as local type placeholders that never touch the X11 server,
444 * to make weston_wm_window_read_properties() less exceptional.
445 */
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400446#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
447#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500448#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700449#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400450
451static void
452weston_wm_window_read_properties(struct weston_wm_window *window)
453{
454 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +0200455 const struct weston_desktop_xwayland_interface *xwayland_interface =
456 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400457
Pekka Paalanen20111d52016-12-01 17:07:12 +0200458#define F(field) (&window->field)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400459 const struct {
460 xcb_atom_t atom;
461 xcb_atom_t type;
Pekka Paalanen20111d52016-12-01 17:07:12 +0200462 void *ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400463 } props[] = {
Pekka Paalanen20111d52016-12-01 17:07:12 +0200464 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
465 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
466 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
467 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, NULL },
468 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, NULL },
469 { wm->atom.net_wm_state, TYPE_NET_WM_STATE, NULL },
470 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
471 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
472 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
473 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, NULL },
474 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400475 };
476#undef F
477
478 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
479 xcb_get_property_reply_t *reply;
480 void *p;
481 uint32_t *xid;
482 xcb_atom_t *atom;
483 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200484 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400485
486 if (!window->properties_dirty)
487 return;
488 window->properties_dirty = 0;
489
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400490 for (i = 0; i < ARRAY_LENGTH(props); i++)
491 cookie[i] = xcb_get_property(wm->conn,
492 0, /* delete */
493 window->id,
494 props[i].atom,
495 XCB_ATOM_ANY, 0, 2048);
496
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700497 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700498 window->size_hints.flags = 0;
499 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700500 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700501
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400502 for (i = 0; i < ARRAY_LENGTH(props); i++) {
503 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
504 if (!reply)
505 /* Bad window, typically */
506 continue;
507 if (reply->type == XCB_ATOM_NONE) {
508 /* No such property */
509 free(reply);
510 continue;
511 }
512
Pekka Paalanen20111d52016-12-01 17:07:12 +0200513 p = props[i].ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400514
515 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300516 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400517 case XCB_ATOM_STRING:
518 /* FIXME: We're using this for both string and
519 utf8_string */
520 if (*(char **) p)
521 free(*(char **) p);
522
523 *(char **) p =
524 strndup(xcb_get_property_value(reply),
525 xcb_get_property_value_length(reply));
526 break;
527 case XCB_ATOM_WINDOW:
528 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500529 if (!wm_lookup_window(wm, *xid, p))
530 weston_log("XCB_ATOM_WINDOW contains window"
531 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400532 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300533 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400534 case XCB_ATOM_ATOM:
535 atom = xcb_get_property_value(reply);
536 *(xcb_atom_t *) p = *atom;
537 break;
538 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700539 atom = xcb_get_property_value(reply);
540 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500541 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700542 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500543 break;
544 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400545 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700546 case TYPE_WM_NORMAL_HINTS:
547 memcpy(&window->size_hints,
548 xcb_get_property_value(reply),
549 sizeof window->size_hints);
550 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500551 case TYPE_NET_WM_STATE:
552 window->fullscreen = 0;
553 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900554 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500555 if (atom[i] == wm->atom.net_wm_state_fullscreen)
556 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200557 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
558 window->maximized_vert = 1;
559 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
560 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900561 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500562 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400563 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700564 memcpy(&window->motif_hints,
565 xcb_get_property_value(reply),
566 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700567 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
568 if (window->motif_hints.decorations & MWM_DECOR_ALL)
569 /* MWM_DECOR_ALL means all except the other values listed. */
570 window->decorate =
571 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
572 else
573 window->decorate =
574 window->motif_hints.decorations;
575 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400576 break;
577 default:
578 break;
579 }
580 free(reply);
581 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200582
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200583 if (window->pid > 0) {
584 gethostname(name, sizeof(name));
585 for (i = 0; i < sizeof(name); i++) {
586 if (name[i] == '\0')
587 break;
588 }
589 if (i == sizeof(name))
590 name[0] = '\0'; /* ignore stupid hostnames */
591
592 /* this is only one heuristic to guess the PID of a client is
593 * valid, assuming it's compliant with icccm and ewmh.
594 * Non-compliants and remote applications of course fail. */
595 if (!window->machine || strcmp(window->machine, name))
596 window->pid = 0;
597 }
598
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200599 if (window->shsurf && window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +0200600 xwayland_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500601 if (window->frame && window->name)
602 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200603 if (window->shsurf && window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +0200604 xwayland_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400605}
606
Pekka Paalanen20111d52016-12-01 17:07:12 +0200607#undef TYPE_WM_PROTOCOLS
608#undef TYPE_MOTIF_WM_HINTS
609#undef TYPE_NET_WM_STATE
610#undef TYPE_WM_NORMAL_HINTS
611
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400612static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400613weston_wm_window_get_frame_size(struct weston_wm_window *window,
614 int *width, int *height)
615{
616 struct theme *t = window->wm->theme;
617
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500618 if (window->fullscreen) {
619 *width = window->width;
620 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800621 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500622 *width = frame_width(window->frame);
623 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400624 } else {
625 *width = window->width + t->margin * 2;
626 *height = window->height + t->margin * 2;
627 }
628}
629
630static void
631weston_wm_window_get_child_position(struct weston_wm_window *window,
632 int *x, int *y)
633{
634 struct theme *t = window->wm->theme;
635
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500636 if (window->fullscreen) {
637 *x = 0;
638 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800639 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500640 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400641 } else {
642 *x = t->margin;
643 *y = t->margin;
644 }
645}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400646
647static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500648weston_wm_window_send_configure_notify(struct weston_wm_window *window)
649{
650 xcb_configure_notify_event_t configure_notify;
651 struct weston_wm *wm = window->wm;
652 int x, y;
653
654 weston_wm_window_get_child_position(window, &x, &y);
655 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
656 configure_notify.pad0 = 0;
657 configure_notify.event = window->id;
658 configure_notify.window = window->id;
659 configure_notify.above_sibling = XCB_WINDOW_NONE;
660 configure_notify.x = x;
661 configure_notify.y = y;
662 configure_notify.width = window->width;
663 configure_notify.height = window->height;
664 configure_notify.border_width = 0;
665 configure_notify.override_redirect = 0;
666 configure_notify.pad1 = 0;
667
Murray Calavera883ac022015-06-06 13:02:22 +0000668 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500669 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
670 (char *) &configure_notify);
671}
672
673static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400674weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
675{
Murray Calavera883ac022015-06-06 13:02:22 +0000676 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400677 (xcb_configure_request_event_t *) event;
678 struct weston_wm_window *window;
679 uint32_t mask, values[16];
680 int x, y, width, height, i = 0;
681
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400682 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
683 configure_request->window,
684 configure_request->x, configure_request->y,
685 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400686
Derek Foreman49372142015-04-09 10:51:22 -0500687 if (!wm_lookup_window(wm, configure_request->window, &window))
688 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400689
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500690 if (window->fullscreen) {
691 weston_wm_window_send_configure_notify(window);
692 return;
693 }
694
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400695 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
696 window->width = configure_request->width;
697 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
698 window->height = configure_request->height;
699
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500700 if (window->frame)
701 frame_resize_inside(window->frame, window->width, window->height);
702
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400703 weston_wm_window_get_child_position(window, &x, &y);
704 values[i++] = x;
705 values[i++] = y;
706 values[i++] = window->width;
707 values[i++] = window->height;
708 values[i++] = 0;
709 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
710 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
711 XCB_CONFIG_WINDOW_BORDER_WIDTH;
712 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
713 values[i++] = configure_request->sibling;
714 mask |= XCB_CONFIG_WINDOW_SIBLING;
715 }
716 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
717 values[i++] = configure_request->stack_mode;
718 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
719 }
720
721 xcb_configure_window(wm->conn, window->id, mask, values);
722
723 weston_wm_window_get_frame_size(window, &width, &height);
724 values[0] = width;
725 values[1] = height;
726 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
727 xcb_configure_window(wm->conn, window->frame_id, mask, values);
728
729 weston_wm_window_schedule_repaint(window);
730}
731
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400732static int
733our_resource(struct weston_wm *wm, uint32_t id)
734{
735 const xcb_setup_t *setup;
736
737 setup = xcb_get_setup(wm->conn);
738
739 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
740}
741
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400742static void
743weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
744{
Murray Calavera883ac022015-06-06 13:02:22 +0000745 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400746 (xcb_configure_notify_event_t *) event;
747 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400748
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400749 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400750 configure_notify->window,
751 configure_notify->x, configure_notify->y,
752 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300753
Derek Foreman49372142015-04-09 10:51:22 -0500754 if (!wm_lookup_window(wm, configure_notify->window, &window))
755 return;
756
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700757 window->x = configure_notify->x;
758 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200759 window->pos_dirty = false;
760
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700761 if (window->override_redirect) {
762 window->width = configure_notify->width;
763 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500764 if (window->frame)
765 frame_resize_inside(window->frame,
766 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700767 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400768}
769
770static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300771weston_wm_kill_client(struct wl_listener *listener, void *data)
772{
773 struct weston_surface *surface = data;
774 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300775 if (!window)
776 return;
777
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200778 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300779 kill(window->pid, SIGKILL);
780}
781
782static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700783weston_wm_create_surface(struct wl_listener *listener, void *data)
784{
785 struct weston_surface *surface = data;
786 struct weston_wm *wm =
787 container_of(listener,
788 struct weston_wm, create_surface_listener);
789 struct weston_wm_window *window;
790
791 if (wl_resource_get_client(surface->resource) != wm->server->client)
792 return;
793
794 wl_list_for_each(window, &wm->unpaired_window_list, link)
795 if (window->surface_id ==
796 wl_resource_get_id(surface->resource)) {
797 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700798 window->surface_id = 0;
799 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700800 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000801 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700802}
803
804static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300805weston_wm_send_focus_window(struct weston_wm *wm,
806 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400807{
Scott Moreau85ecac02012-05-21 15:49:14 -0600808 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400809
Scott Moreau85ecac02012-05-21 15:49:14 -0600810 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700811 uint32_t values[1];
812
Boyan Dingb9f863c2014-08-30 10:33:23 +0800813 if (window->override_redirect)
814 return;
815
Scott Moreau85ecac02012-05-21 15:49:14 -0600816 client_message.response_type = XCB_CLIENT_MESSAGE;
817 client_message.format = 32;
818 client_message.window = window->id;
819 client_message.type = wm->atom.wm_protocols;
820 client_message.data.data32[0] = wm->atom.wm_take_focus;
821 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
822
Murray Calavera883ac022015-06-06 13:02:22 +0000823 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600824 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
825 (char *) &client_message);
826
827 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
828 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700829
830 values[0] = XCB_STACK_MODE_ABOVE;
831 xcb_configure_window (wm->conn, window->frame_id,
832 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600833 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400834 xcb_set_input_focus (wm->conn,
835 XCB_INPUT_FOCUS_POINTER_ROOT,
836 XCB_NONE,
837 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600838 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300839}
840
841static void
842weston_wm_window_activate(struct wl_listener *listener, void *data)
843{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800844 struct weston_surface_activation_data *activation_data = data;
845 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300846 struct weston_wm_window *window = NULL;
847 struct weston_wm *wm =
848 container_of(listener, struct weston_wm, activate_listener);
849
850 if (surface) {
851 window = get_wm_window(surface);
852 }
853
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200854 if (window) {
855 weston_wm_set_net_active_window(wm, window->id);
856 } else {
857 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
858 }
859
Giulio Camuffob18f7882015-03-29 14:20:23 +0300860 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400861
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500862 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800863 if (wm->focus_window->frame)
864 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500866 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500868 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800869 if (wm->focus_window->frame)
870 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400871 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500872 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200873
874 xcb_flush(wm->conn);
875
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400876}
877
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400878#define ICCCM_WITHDRAWN_STATE 0
879#define ICCCM_NORMAL_STATE 1
880#define ICCCM_ICONIC_STATE 3
881
882static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500883weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400884{
885 struct weston_wm *wm = window->wm;
886 uint32_t property[2];
887
888 property[0] = state;
889 property[1] = XCB_WINDOW_NONE;
890
891 xcb_change_property(wm->conn,
892 XCB_PROP_MODE_REPLACE,
893 window->id,
894 wm->atom.wm_state,
895 wm->atom.wm_state,
896 32, /* format */
897 2, property);
898}
899
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400900static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500901weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
902{
903 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200904 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500905 int i;
906
907 i = 0;
908 if (window->fullscreen)
909 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200910 if (window->maximized_vert)
911 property[i++] = wm->atom.net_wm_state_maximized_vert;
912 if (window->maximized_horz)
913 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500914
915 xcb_change_property(wm->conn,
916 XCB_PROP_MODE_REPLACE,
917 window->id,
918 wm->atom.net_wm_state,
919 XCB_ATOM_ATOM,
920 32, /* format */
921 i, property);
922}
923
924static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700925weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400926{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700927 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400928 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400929 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200930 int buttons = FRAME_BUTTON_CLOSE;
931
932 if (window->decorate & MWM_DECOR_MAXIMIZE)
933 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400934
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500935 window->frame = frame_create(window->wm->theme,
936 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200937 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500938 frame_resize_inside(window->frame, window->width, window->height);
939
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400940 weston_wm_window_get_frame_size(window, &width, &height);
941 weston_wm_window_get_child_position(window, &x, &y);
942
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400943 values[0] = wm->screen->black_pixel;
944 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400945 XCB_EVENT_MASK_KEY_PRESS |
946 XCB_EVENT_MASK_KEY_RELEASE |
947 XCB_EVENT_MASK_BUTTON_PRESS |
948 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400949 XCB_EVENT_MASK_POINTER_MOTION |
950 XCB_EVENT_MASK_ENTER_WINDOW |
951 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400952 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400953 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400954 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400955
956 window->frame_id = xcb_generate_id(wm->conn);
957 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400958 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400959 window->frame_id,
960 wm->screen->root,
961 0, 0,
962 width, height,
963 0,
964 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400965 wm->visual_id,
966 XCB_CW_BORDER_PIXEL |
967 XCB_CW_EVENT_MASK |
968 XCB_CW_COLORMAP, values);
969
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400970 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
971
972 values[0] = 0;
973 xcb_configure_window(wm->conn, window->id,
974 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
975
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400976 window->cairo_surface =
977 cairo_xcb_surface_create_with_xrender_format(wm->conn,
978 wm->screen,
979 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400980 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400981 width, height);
982
983 hash_table_insert(wm->window_hash, window->frame_id, window);
984}
985
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200986/*
987 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
988 * Passing a <0 desktop value deletes the property.
989 */
990static void
991weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700992 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200993{
994 if (desktop >= 0) {
995 xcb_change_property(window->wm->conn,
996 XCB_PROP_MODE_REPLACE,
997 window->id,
998 window->wm->atom.net_wm_desktop,
999 XCB_ATOM_CARDINAL,
1000 32, /* format */
1001 1, &desktop);
1002 } else {
1003 xcb_delete_property(window->wm->conn,
1004 window->id,
1005 window->wm->atom.net_wm_desktop);
1006 }
1007}
1008
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001009static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001010weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
1011{
1012 xcb_map_request_event_t *map_request =
1013 (xcb_map_request_event_t *) event;
1014 struct weston_wm_window *window;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001015 struct weston_output *output;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001016
1017 if (our_resource(wm, map_request->window)) {
1018 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
1019 map_request->window);
1020 return;
1021 }
1022
Derek Foreman49372142015-04-09 10:51:22 -05001023 if (!wm_lookup_window(wm, map_request->window, &window))
1024 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001025
1026 weston_wm_window_read_properties(window);
1027
1028 if (window->frame_id == XCB_WINDOW_NONE)
1029 weston_wm_window_create_frame(window);
1030
Pekka Paalanen44660c32016-11-14 15:38:43 +02001031 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d, %dx%d @ %d,%d)\n",
1032 window->id, window, window->frame_id,
1033 window->width, window->height, window->x, window->y);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001034
1035 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1036 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001037 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001038
Pekka Paalanen505237e2016-12-01 15:41:11 +02001039 if (legacy_fullscreen(wm, window, &output)) {
1040 window->fullscreen = 1;
1041 weston_output_weak_ref_set(&window->legacy_fullscreen_output,
1042 output);
1043 }
1044
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001045 xcb_map_window(wm->conn, map_request->window);
1046 xcb_map_window(wm->conn, window->frame_id);
1047}
1048
1049static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001050weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1051{
1052 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1053
1054 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001055 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1056 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001057 return;
1058 }
1059
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001060 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001061}
1062
1063static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001064weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1065{
1066 xcb_unmap_notify_event_t *unmap_notify =
1067 (xcb_unmap_notify_event_t *) event;
1068 struct weston_wm_window *window;
1069
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001070 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1071 unmap_notify->window,
1072 unmap_notify->event,
1073 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001074
1075 if (our_resource(wm, unmap_notify->window))
1076 return;
1077
MoD31700122013-06-11 19:58:55 -05001078 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001079 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1080 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001081 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001082
Derek Foreman49372142015-04-09 10:51:22 -05001083 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1084 return;
1085
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001086 if (window->surface_id) {
1087 /* Make sure we're not on the unpaired surface list or we
1088 * could be assigned a surface during surface creation that
1089 * was mapped before this unmap request.
1090 */
1091 wl_list_remove(&window->link);
1092 window->surface_id = 0;
1093 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001094 if (wm->focus_window == window)
1095 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001096 if (window->surface)
1097 wl_list_remove(&window->surface_destroy_listener.link);
1098 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001099 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001100
1101 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1102 weston_wm_window_set_virtual_desktop(window, -1);
1103
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001104 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001105}
1106
1107static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001108weston_wm_window_draw_decoration(void *data)
1109{
1110 struct weston_wm_window *window = data;
1111 struct weston_wm *wm = window->wm;
1112 struct theme *t = wm->theme;
1113 cairo_t *cr;
1114 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001115 int32_t input_x, input_y, input_w, input_h;
Quentin Glidic955cec02016-08-12 10:41:35 +02001116 const struct weston_desktop_xwayland_interface *xwayland_interface =
1117 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001118 uint32_t flags = 0;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001119 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001120
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001121 wm_log("XWM: start draw decoration, win %d\n", window->id);
1122
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001123 weston_wm_window_read_properties(window);
1124
1125 window->repaint_source = NULL;
1126
1127 weston_wm_window_get_frame_size(window, &width, &height);
1128 weston_wm_window_get_child_position(window, &x, &y);
1129
1130 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1131 cr = cairo_create(window->cairo_surface);
1132
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001133 if (window->fullscreen) {
1134 /* nothing */
1135 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001136 if (wm->focus_window == window)
1137 flags |= THEME_FRAME_ACTIVE;
1138
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001139 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001140 } else {
1141 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1142 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1143 cairo_paint(cr);
1144
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001145 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001146 }
1147
1148 cairo_destroy(cr);
1149
1150 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001151 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001152 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001153 pixman_region32_init(&window->surface->pending.opaque);
1154 } else {
1155 /* We leave an extra pixel around the X window area to
1156 * make sure we don't sample from the undefined alpha
1157 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001158 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001159 x - 1, y - 1,
1160 window->width + 2,
1161 window->height + 2);
1162 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001163 wl_list_for_each(view, &window->surface->views, surface_link)
1164 weston_view_geometry_dirty(view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001165
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001166 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001167
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001168 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001169 frame_input_rect(window->frame, &input_x, &input_y,
1170 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001171 } else {
1172 input_x = x;
1173 input_y = y;
1174 input_w = width;
1175 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001176 }
1177
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001178 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001179 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001180
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001181 wm_log("XWM: draw decoration, win %d geometry: %d,%d %dx%d\n",
1182 window->id, input_x, input_y, input_w, input_h);
1183
Quentin Glidic955cec02016-08-12 10:41:35 +02001184 xwayland_interface->set_window_geometry(window->shsurf,
1185 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001186 }
1187}
1188
1189static void
1190weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1191{
1192 struct weston_wm *wm = window->wm;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001193 struct weston_view *view;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001194 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001195
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001196 if (window->frame_id == XCB_WINDOW_NONE) {
1197 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001198 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001199 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001200 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001201 pixman_region32_init(&window->surface->pending.opaque);
1202 } else {
1203 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1204 width, height);
1205 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001206 wl_list_for_each(view, &window->surface->views, surface_link)
1207 weston_view_geometry_dirty(view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001208 }
1209 return;
1210 }
1211
1212 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001213 return;
1214
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001215 wm_log("XWM: schedule repaint, win %d\n", window->id);
1216
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001217 window->repaint_source =
1218 wl_event_loop_add_idle(wm->server->loop,
1219 weston_wm_window_draw_decoration,
1220 window);
1221}
1222
1223static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001224weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1225{
1226 xcb_property_notify_event_t *property_notify =
1227 (xcb_property_notify_event_t *) event;
1228 struct weston_wm_window *window;
1229
Derek Foreman49372142015-04-09 10:51:22 -05001230 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001231 return;
1232
1233 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001234
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001235 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001236 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001237 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001238 else
1239 read_and_dump_property(wm, property_notify->window,
1240 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001241
1242 if (property_notify->atom == wm->atom.net_wm_name ||
1243 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001244 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001245}
1246
1247static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001248weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001249 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001250{
1251 struct weston_wm_window *window;
1252 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001253 xcb_get_geometry_cookie_t geometry_cookie;
1254 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001255
Peter Huttererf3d62272013-08-08 11:57:05 +10001256 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001257 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001258 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001259 return;
1260 }
1261
MoD384a11a2013-06-22 11:04:21 -05001262 geometry_cookie = xcb_get_geometry(wm->conn, id);
1263
Giulio Camuffob18f7882015-03-29 14:20:23 +03001264 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1265 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001266 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1267
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001268 window->wm = wm;
1269 window->id = id;
1270 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001271 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001272 window->width = width;
1273 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001274 window->x = x;
1275 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001276 window->pos_dirty = false;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001277 weston_output_weak_ref_init(&window->legacy_fullscreen_output);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001278
MoD384a11a2013-06-22 11:04:21 -05001279 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1280 /* technically we should use XRender and check the visual format's
1281 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001282 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001283 window->has_alpha = geometry_reply->depth == 32;
1284 free(geometry_reply);
1285
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001286 hash_table_insert(wm->window_hash, id, window);
1287}
1288
1289static void
1290weston_wm_window_destroy(struct weston_wm_window *window)
1291{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001292 struct weston_wm *wm = window->wm;
1293
Pekka Paalanen505237e2016-12-01 15:41:11 +02001294 weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1295
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001296 if (window->repaint_source)
1297 wl_event_source_remove(window->repaint_source);
1298 if (window->cairo_surface)
1299 cairo_surface_destroy(window->cairo_surface);
1300
1301 if (window->frame_id) {
1302 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1303 xcb_destroy_window(wm->conn, window->frame_id);
1304 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001305 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001306 hash_table_remove(wm->window_hash, window->frame_id);
1307 window->frame_id = XCB_WINDOW_NONE;
1308 }
1309
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001310 if (window->surface_id)
1311 wl_list_remove(&window->link);
1312
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001313 if (window->surface)
1314 wl_list_remove(&window->surface_destroy_listener.link);
1315
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001316 hash_table_remove(window->wm->window_hash, window->id);
1317 free(window);
1318}
1319
1320static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001321weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1322{
1323 xcb_create_notify_event_t *create_notify =
1324 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001325
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001326 wm_log("XCB_CREATE_NOTIFY (window %d, at (%d, %d), width %d, height %d%s%s)\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001327 create_notify->window,
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001328 create_notify->x, create_notify->y,
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001329 create_notify->width, create_notify->height,
1330 create_notify->override_redirect ? ", override" : "",
1331 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001332
1333 if (our_resource(wm, create_notify->window))
1334 return;
1335
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001336 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001337 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001338 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001339 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001340}
1341
1342static void
1343weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1344{
1345 xcb_destroy_notify_event_t *destroy_notify =
1346 (xcb_destroy_notify_event_t *) event;
1347 struct weston_wm_window *window;
1348
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001349 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1350 destroy_notify->window,
1351 destroy_notify->event,
1352 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001353
1354 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001355 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001356
Derek Foreman49372142015-04-09 10:51:22 -05001357 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1358 return;
1359
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001360 weston_wm_window_destroy(window);
1361}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001362
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001363static void
1364weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1365{
1366 xcb_reparent_notify_event_t *reparent_notify =
1367 (xcb_reparent_notify_event_t *) event;
1368 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001369
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001370 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1371 reparent_notify->window,
1372 reparent_notify->parent,
1373 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001374
1375 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001376 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001377 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001378 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001379 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001380 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1381 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001382 weston_wm_window_destroy(window);
1383 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001384}
1385
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001386struct weston_seat *
1387weston_wm_pick_seat(struct weston_wm *wm)
1388{
Tom Hochsteine7fff212016-11-01 14:14:00 -05001389 struct wl_list *seats = wm->server->compositor->seat_list.next;
1390 if (wl_list_empty(seats))
1391 return NULL;
1392 return container_of(seats, struct weston_seat, link);
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001393}
1394
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001395static struct weston_seat *
1396weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1397{
1398 struct weston_wm *wm = window->wm;
1399 struct weston_seat *seat, *s;
1400
1401 seat = NULL;
1402 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001403 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1404 struct weston_pointer *old_pointer =
1405 weston_seat_get_pointer(seat);
1406
1407 if (pointer && pointer->focus &&
1408 pointer->focus->surface == window->surface &&
1409 pointer->button_count > 0 &&
1410 (!seat ||
1411 pointer->grab_serial -
1412 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001413 seat = s;
1414 }
1415
1416 return seat;
1417}
1418
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001419static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001420weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1421 xcb_client_message_event_t *client_message)
1422{
1423 static const int map[] = {
1424 THEME_LOCATION_RESIZING_TOP_LEFT,
1425 THEME_LOCATION_RESIZING_TOP,
1426 THEME_LOCATION_RESIZING_TOP_RIGHT,
1427 THEME_LOCATION_RESIZING_RIGHT,
1428 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1429 THEME_LOCATION_RESIZING_BOTTOM,
1430 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1431 THEME_LOCATION_RESIZING_LEFT
1432 };
1433
1434 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001435 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001436 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001437 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001438 const struct weston_desktop_xwayland_interface *xwayland_interface =
1439 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001440
Derek Foreman1281a362015-07-31 16:55:32 -05001441 if (!pointer || pointer->button_count != 1
1442 || !pointer->focus
1443 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001444 return;
1445
1446 detail = client_message->data.data32[2];
1447 switch (detail) {
1448 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001449 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001450 break;
1451 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1452 case _NET_WM_MOVERESIZE_SIZE_TOP:
1453 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1454 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1455 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1456 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1457 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1458 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001459 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001460 break;
1461 case _NET_WM_MOVERESIZE_CANCEL:
1462 break;
1463 }
1464}
1465
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001466#define _NET_WM_STATE_REMOVE 0
1467#define _NET_WM_STATE_ADD 1
1468#define _NET_WM_STATE_TOGGLE 2
1469
1470static int
1471update_state(int action, int *state)
1472{
1473 int new_state, changed;
1474
1475 switch (action) {
1476 case _NET_WM_STATE_REMOVE:
1477 new_state = 0;
1478 break;
1479 case _NET_WM_STATE_ADD:
1480 new_state = 1;
1481 break;
1482 case _NET_WM_STATE_TOGGLE:
1483 new_state = !*state;
1484 break;
1485 default:
1486 return 0;
1487 }
1488
1489 changed = (*state != new_state);
1490 *state = new_state;
1491
1492 return changed;
1493}
1494
1495static void
1496weston_wm_window_configure(void *data);
1497
1498static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001499weston_wm_window_set_toplevel(struct weston_wm_window *window)
1500{
Quentin Glidic955cec02016-08-12 10:41:35 +02001501 const struct weston_desktop_xwayland_interface *xwayland_interface =
1502 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001503
Quentin Glidic955cec02016-08-12 10:41:35 +02001504 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001505 window->width = window->saved_width;
1506 window->height = window->saved_height;
1507 if (window->frame)
1508 frame_resize_inside(window->frame,
1509 window->width,
1510 window->height);
1511 weston_wm_window_configure(window);
1512}
1513
1514static inline bool
1515weston_wm_window_is_maximized(struct weston_wm_window *window)
1516{
1517 return window->maximized_horz && window->maximized_vert;
1518}
1519
1520static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001521weston_wm_window_handle_state(struct weston_wm_window *window,
1522 xcb_client_message_event_t *client_message)
1523{
1524 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001525 const struct weston_desktop_xwayland_interface *xwayland_interface =
1526 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001527 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001528 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001529
1530 action = client_message->data.data32[0];
1531 property = client_message->data.data32[1];
1532
1533 if (property == wm->atom.net_wm_state_fullscreen &&
1534 update_state(action, &window->fullscreen)) {
1535 weston_wm_window_set_net_wm_state(window);
1536 if (window->fullscreen) {
1537 window->saved_width = window->width;
1538 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001539
1540 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001541 xwayland_interface->set_fullscreen(window->shsurf,
1542 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001543 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001544 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001545 weston_wm_window_set_toplevel(window);
1546 }
1547 } else {
1548 if (property == wm->atom.net_wm_state_maximized_vert &&
1549 update_state(action, &window->maximized_vert))
1550 weston_wm_window_set_net_wm_state(window);
1551 if (property == wm->atom.net_wm_state_maximized_horz &&
1552 update_state(action, &window->maximized_horz))
1553 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001554
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001555 if (maximized != weston_wm_window_is_maximized(window)) {
1556 if (weston_wm_window_is_maximized(window)) {
1557 window->saved_width = window->width;
1558 window->saved_height = window->height;
1559
1560 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001561 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001562 } else if (window->shsurf) {
1563 weston_wm_window_set_toplevel(window);
1564 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001565 }
1566 }
1567}
1568
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001569static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001570surface_destroy(struct wl_listener *listener, void *data)
1571{
1572 struct weston_wm_window *window =
1573 container_of(listener,
1574 struct weston_wm_window, surface_destroy_listener);
1575
1576 wm_log("surface for xid %d destroyed\n", window->id);
1577
1578 /* This should have been freed by the shell.
1579 * Don't try to use it later. */
1580 window->shsurf = NULL;
1581 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001582}
1583
1584static void
1585weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1586 xcb_client_message_event_t *client_message)
1587{
1588 struct weston_wm *wm = window->wm;
1589 struct wl_resource *resource;
1590
1591 if (window->surface_id != 0) {
1592 wm_log("already have surface id for window %d\n", window->id);
1593 return;
1594 }
1595
1596 /* Xwayland will send the wayland requests to create the
1597 * wl_surface before sending this client message. Even so, we
1598 * can end up handling the X event before the wayland requests
1599 * and thus when we try to look up the surface ID, the surface
1600 * hasn't been created yet. In that case put the window on
1601 * the unpaired window list and continue when the surface gets
1602 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001603 uint32_t id = client_message->data.data32[0];
1604 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001605 if (resource) {
1606 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001607 xserver_map_shell_surface(window,
1608 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001609 }
1610 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001611 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001612 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001613 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001614}
1615
1616static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001617weston_wm_handle_client_message(struct weston_wm *wm,
1618 xcb_generic_event_t *event)
1619{
1620 xcb_client_message_event_t *client_message =
1621 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001622 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001623
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001624 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1625 get_atom_name(wm->conn, client_message->type),
1626 client_message->data.data32[0],
1627 client_message->data.data32[1],
1628 client_message->data.data32[2],
1629 client_message->data.data32[3],
1630 client_message->data.data32[4],
1631 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001632
Jason Ekstrand0250a742014-07-24 13:17:47 -07001633 /* The window may get created and destroyed before we actually
1634 * handle the message. If it doesn't exist, bail.
1635 */
Derek Foreman49372142015-04-09 10:51:22 -05001636 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001637 return;
1638
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001639 if (client_message->type == wm->atom.net_wm_moveresize)
1640 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001641 else if (client_message->type == wm->atom.net_wm_state)
1642 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001643 else if (client_message->type == wm->atom.wl_surface_id)
1644 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001645}
1646
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001647enum cursor_type {
1648 XWM_CURSOR_TOP,
1649 XWM_CURSOR_BOTTOM,
1650 XWM_CURSOR_LEFT,
1651 XWM_CURSOR_RIGHT,
1652 XWM_CURSOR_TOP_LEFT,
1653 XWM_CURSOR_TOP_RIGHT,
1654 XWM_CURSOR_BOTTOM_LEFT,
1655 XWM_CURSOR_BOTTOM_RIGHT,
1656 XWM_CURSOR_LEFT_PTR,
1657};
1658
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001659/*
1660 * The following correspondences between file names and cursors was copied
1661 * from: https://bugs.kde.org/attachment.cgi?id=67313
1662 */
1663
1664static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001665 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001666 "sw-resize",
1667 "size_bdiag"
1668};
1669
1670static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001671 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001672 "se-resize",
1673 "size_fdiag"
1674};
1675
1676static const char *bottom_sides[] = {
1677 "bottom_side",
1678 "s-resize",
1679 "size_ver"
1680};
1681
1682static const char *left_ptrs[] = {
1683 "left_ptr",
1684 "default",
1685 "top_left_arrow",
1686 "left-arrow"
1687};
1688
1689static const char *left_sides[] = {
1690 "left_side",
1691 "w-resize",
1692 "size_hor"
1693};
1694
1695static const char *right_sides[] = {
1696 "right_side",
1697 "e-resize",
1698 "size_hor"
1699};
1700
1701static const char *top_left_corners[] = {
1702 "top_left_corner",
1703 "nw-resize",
1704 "size_fdiag"
1705};
1706
1707static const char *top_right_corners[] = {
1708 "top_right_corner",
1709 "ne-resize",
1710 "size_bdiag"
1711};
1712
1713static const char *top_sides[] = {
1714 "top_side",
1715 "n-resize",
1716 "size_ver"
1717};
1718
1719struct cursor_alternatives {
1720 const char **names;
1721 size_t count;
1722};
1723
1724static const struct cursor_alternatives cursors[] = {
1725 {top_sides, ARRAY_LENGTH(top_sides)},
1726 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1727 {left_sides, ARRAY_LENGTH(left_sides)},
1728 {right_sides, ARRAY_LENGTH(right_sides)},
1729 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1730 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1731 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1732 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1733 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001734};
1735
1736static void
1737weston_wm_create_cursors(struct weston_wm *wm)
1738{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001739 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001740 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001741 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001742
1743 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1744 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001745 for (j = 0; j < cursors[i].count; j++) {
1746 name = cursors[i].names[j];
1747 wm->cursors[i] =
1748 xcb_cursor_library_load_cursor(wm, name);
1749 if (wm->cursors[i] != (xcb_cursor_t)-1)
1750 break;
1751 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001752 }
1753
1754 wm->last_cursor = -1;
1755}
1756
1757static void
1758weston_wm_destroy_cursors(struct weston_wm *wm)
1759{
1760 uint8_t i;
1761
1762 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1763 xcb_free_cursor(wm->conn, wm->cursors[i]);
1764
1765 free(wm->cursors);
1766}
1767
1768static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001769get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001770{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001771 switch (location) {
1772 case THEME_LOCATION_RESIZING_TOP:
1773 return XWM_CURSOR_TOP;
1774 case THEME_LOCATION_RESIZING_BOTTOM:
1775 return XWM_CURSOR_BOTTOM;
1776 case THEME_LOCATION_RESIZING_LEFT:
1777 return XWM_CURSOR_LEFT;
1778 case THEME_LOCATION_RESIZING_RIGHT:
1779 return XWM_CURSOR_RIGHT;
1780 case THEME_LOCATION_RESIZING_TOP_LEFT:
1781 return XWM_CURSOR_TOP_LEFT;
1782 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1783 return XWM_CURSOR_TOP_RIGHT;
1784 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1785 return XWM_CURSOR_BOTTOM_LEFT;
1786 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1787 return XWM_CURSOR_BOTTOM_RIGHT;
1788 case THEME_LOCATION_EXTERIOR:
1789 case THEME_LOCATION_TITLEBAR:
1790 default:
1791 return XWM_CURSOR_LEFT_PTR;
1792 }
1793}
1794
1795static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001796weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1797 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001798{
1799 uint32_t cursor_value_list;
1800
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001801 if (wm->last_cursor == cursor)
1802 return;
1803
1804 wm->last_cursor = cursor;
1805
1806 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001807 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001808 XCB_CW_CURSOR, &cursor_value_list);
1809 xcb_flush(wm->conn);
1810}
1811
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001812static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001813weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001814{
1815 xcb_client_message_event_t client_message;
1816
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001817 if (window->delete_window) {
1818 client_message.response_type = XCB_CLIENT_MESSAGE;
1819 client_message.format = 32;
1820 client_message.window = window->id;
1821 client_message.type = window->wm->atom.wm_protocols;
1822 client_message.data.data32[0] =
1823 window->wm->atom.wm_delete_window;
1824 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001825
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001826 xcb_send_event(window->wm->conn, 0, window->id,
1827 XCB_EVENT_MASK_NO_EVENT,
1828 (char *) &client_message);
1829 } else {
1830 xcb_kill_client(window->wm->conn, window->id);
1831 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001832}
1833
1834static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001835weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1836{
1837 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001838 const struct weston_desktop_xwayland_interface *xwayland_interface =
1839 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001840 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001841 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001842 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001843 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001844 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001845 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001846
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001847 wm_log("XCB_BUTTON_%s (detail %d)\n",
1848 button->response_type == XCB_BUTTON_PRESS ?
1849 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001850
Derek Foreman49372142015-04-09 10:51:22 -05001851 if (!wm_lookup_window(wm, button->event, &window) ||
1852 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001853 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001854
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001855 if (button->detail != 1 && button->detail != 2)
1856 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001857
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001858 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001859 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001860
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001861 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001862 WL_POINTER_BUTTON_STATE_PRESSED :
1863 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001864 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1865
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001866 /* Make sure we're looking at the right location. The frame
1867 * could have received a motion event from a pointer from a
1868 * different wl_seat, but under X it looks like our core
1869 * pointer moved. Move the frame pointer to the button press
1870 * location before deciding what to do. */
1871 location = frame_pointer_motion(window->frame, NULL,
1872 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001873 location = frame_pointer_button(window->frame, NULL,
1874 button_id, button_state);
1875 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1876 weston_wm_window_schedule_repaint(window);
1877
1878 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001879 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001880 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001881 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1882 }
1883
1884 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001885 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001886 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001887 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1888 }
1889
1890 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001891 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001892 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001893 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001894
1895 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1896 window->maximized_horz = !window->maximized_horz;
1897 window->maximized_vert = !window->maximized_vert;
1898 if (weston_wm_window_is_maximized(window)) {
1899 window->saved_width = window->width;
1900 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001901 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001902 } else {
1903 weston_wm_window_set_toplevel(window);
1904 }
1905 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1906 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001907}
1908
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001909static void
1910weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1911{
1912 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1913 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001914 enum theme_location location;
1915 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001916
Derek Foreman49372142015-04-09 10:51:22 -05001917 if (!wm_lookup_window(wm, motion->event, &window) ||
1918 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001919 return;
1920
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001921 location = frame_pointer_motion(window->frame, NULL,
1922 motion->event_x, motion->event_y);
1923 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1924 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001925
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001926 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001927 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001928}
1929
1930static void
1931weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1932{
1933 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1934 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001935 enum theme_location location;
1936 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001937
Derek Foreman49372142015-04-09 10:51:22 -05001938 if (!wm_lookup_window(wm, enter->event, &window) ||
1939 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001940 return;
1941
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001942 location = frame_pointer_enter(window->frame, NULL,
1943 enter->event_x, enter->event_y);
1944 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1945 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001946
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001947 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001948 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001949}
1950
1951static void
1952weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1953{
1954 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1955 struct weston_wm_window *window;
1956
Derek Foreman49372142015-04-09 10:51:22 -05001957 if (!wm_lookup_window(wm, leave->event, &window) ||
1958 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001959 return;
1960
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001961 frame_pointer_leave(window->frame, NULL);
1962 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1963 weston_wm_window_schedule_repaint(window);
1964
Tiago Vignattic1903232012-07-16 12:15:37 -04001965 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001966}
1967
Giulio Camuffob18f7882015-03-29 14:20:23 +03001968static void
1969weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1970{
1971 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1972 /* Do not let X clients change the focus behind the compositor's
1973 * back. Reset the focus to the old one if it changed. */
1974 if (!wm->focus_window || focus->event != wm->focus_window->id)
1975 weston_wm_send_focus_window(wm, wm->focus_window);
1976}
1977
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001978static int
1979weston_wm_handle_event(int fd, uint32_t mask, void *data)
1980{
1981 struct weston_wm *wm = data;
1982 xcb_generic_event_t *event;
1983 int count = 0;
1984
1985 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1986 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001987 free(event);
1988 count++;
1989 continue;
1990 }
1991
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001992 if (weston_wm_handle_dnd_event(wm, event)) {
1993 free(event);
1994 count++;
1995 continue;
1996 }
1997
MoD31700122013-06-11 19:58:55 -05001998 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001999 case XCB_BUTTON_PRESS:
2000 case XCB_BUTTON_RELEASE:
2001 weston_wm_handle_button(wm, event);
2002 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002003 case XCB_ENTER_NOTIFY:
2004 weston_wm_handle_enter(wm, event);
2005 break;
2006 case XCB_LEAVE_NOTIFY:
2007 weston_wm_handle_leave(wm, event);
2008 break;
2009 case XCB_MOTION_NOTIFY:
2010 weston_wm_handle_motion(wm, event);
2011 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002012 case XCB_CREATE_NOTIFY:
2013 weston_wm_handle_create_notify(wm, event);
2014 break;
2015 case XCB_MAP_REQUEST:
2016 weston_wm_handle_map_request(wm, event);
2017 break;
2018 case XCB_MAP_NOTIFY:
2019 weston_wm_handle_map_notify(wm, event);
2020 break;
2021 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04002022 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002023 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04002024 case XCB_REPARENT_NOTIFY:
2025 weston_wm_handle_reparent_notify(wm, event);
2026 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002027 case XCB_CONFIGURE_REQUEST:
2028 weston_wm_handle_configure_request(wm, event);
2029 break;
2030 case XCB_CONFIGURE_NOTIFY:
2031 weston_wm_handle_configure_notify(wm, event);
2032 break;
2033 case XCB_DESTROY_NOTIFY:
2034 weston_wm_handle_destroy_notify(wm, event);
2035 break;
2036 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002037 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002038 break;
2039 case XCB_PROPERTY_NOTIFY:
2040 weston_wm_handle_property_notify(wm, event);
2041 break;
2042 case XCB_CLIENT_MESSAGE:
2043 weston_wm_handle_client_message(wm, event);
2044 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03002045 case XCB_FOCUS_IN:
2046 weston_wm_handle_focus_in(wm, event);
2047 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002048 }
2049
2050 free(event);
2051 count++;
2052 }
2053
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002054 if (count != 0)
2055 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002056
2057 return count;
2058}
2059
2060static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002061weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2062 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2063 wm->screen->root, wm->atom.net_active_window,
2064 wm->atom.window, 32, 1, &window);
2065}
2066
2067static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002068weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2069{
2070 xcb_depth_iterator_t d_iter;
2071 xcb_visualtype_iterator_t vt_iter;
2072 xcb_visualtype_t *visualtype;
2073
2074 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2075 visualtype = NULL;
2076 while (d_iter.rem > 0) {
2077 if (d_iter.data->depth == 32) {
2078 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2079 visualtype = vt_iter.data;
2080 break;
2081 }
2082
2083 xcb_depth_next(&d_iter);
2084 }
2085
2086 if (visualtype == NULL) {
2087 weston_log("no 32 bit visualtype\n");
2088 return;
2089 }
2090
2091 wm->visual_id = visualtype->visual_id;
2092 wm->colormap = xcb_generate_id(wm->conn);
2093 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2094 wm->colormap, wm->screen->root, wm->visual_id);
2095}
2096
2097static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002098weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002099{
2100
2101#define F(field) offsetof(struct weston_wm, field)
2102
2103 static const struct { const char *name; int offset; } atoms[] = {
2104 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002105 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002106 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2107 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002108 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002109 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002110 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002111 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002112 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002113 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002114 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2115 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002116 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2117 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002118 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2119 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2120 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002121 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002122 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2123
2124 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2125 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2126 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2127 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2128 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2129 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2130 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002131 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2132 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002133 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2134 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2135 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2136 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2137 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2138
2139 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2140 { "_NET_SUPPORTING_WM_CHECK",
2141 F(atom.net_supporting_wm_check) },
2142 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002143 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002144 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2145 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002146 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002147 { "TARGETS", F(atom.targets) },
2148 { "UTF8_STRING", F(atom.utf8_string) },
2149 { "_WL_SELECTION", F(atom.wl_selection) },
2150 { "INCR", F(atom.incr) },
2151 { "TIMESTAMP", F(atom.timestamp) },
2152 { "MULTIPLE", F(atom.multiple) },
2153 { "UTF8_STRING" , F(atom.utf8_string) },
2154 { "COMPOUND_TEXT", F(atom.compound_text) },
2155 { "TEXT", F(atom.text) },
2156 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002157 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002158 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2159 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002160 { "XdndSelection", F(atom.xdnd_selection) },
2161 { "XdndAware", F(atom.xdnd_aware) },
2162 { "XdndEnter", F(atom.xdnd_enter) },
2163 { "XdndLeave", F(atom.xdnd_leave) },
2164 { "XdndDrop", F(atom.xdnd_drop) },
2165 { "XdndStatus", F(atom.xdnd_status) },
2166 { "XdndFinished", F(atom.xdnd_finished) },
2167 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002168 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2169 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002170 };
2171#undef F
2172
2173 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2174 xcb_xfixes_query_version_reply_t *xfixes_reply;
2175 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2176 xcb_intern_atom_reply_t *reply;
2177 xcb_render_query_pict_formats_reply_t *formats_reply;
2178 xcb_render_query_pict_formats_cookie_t formats_cookie;
2179 xcb_render_pictforminfo_t *formats;
2180 uint32_t i;
2181
2182 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002183 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002184
2185 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2186
2187 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2188 cookies[i] = xcb_intern_atom (wm->conn, 0,
2189 strlen(atoms[i].name),
2190 atoms[i].name);
2191
2192 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2193 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2194 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2195 free(reply);
2196 }
2197
2198 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2199 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002200 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002201
2202 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2203 XCB_XFIXES_MAJOR_VERSION,
2204 XCB_XFIXES_MINOR_VERSION);
2205 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2206 xfixes_cookie, NULL);
2207
Martin Minarik6d118362012-06-07 18:01:59 +02002208 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002209 xfixes_reply->major_version, xfixes_reply->minor_version);
2210
2211 free(xfixes_reply);
2212
2213 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2214 formats_cookie, 0);
2215 if (formats_reply == NULL)
2216 return;
2217
2218 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002219 for (i = 0; i < formats_reply->num_formats; i++) {
2220 if (formats[i].direct.red_mask != 0xff &&
2221 formats[i].direct.red_shift != 16)
2222 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002223 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2224 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002225 wm->format_rgb = formats[i];
2226 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2227 formats[i].depth == 32 &&
2228 formats[i].direct.alpha_mask == 0xff &&
2229 formats[i].direct.alpha_shift == 24)
2230 wm->format_rgba = formats[i];
2231 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002232
2233 free(formats_reply);
2234}
2235
2236static void
2237weston_wm_create_wm_window(struct weston_wm *wm)
2238{
2239 static const char name[] = "Weston WM";
2240
2241 wm->wm_window = xcb_generate_id(wm->conn);
2242 xcb_create_window(wm->conn,
2243 XCB_COPY_FROM_PARENT,
2244 wm->wm_window,
2245 wm->screen->root,
2246 0, 0,
2247 10, 10,
2248 0,
2249 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2250 wm->screen->root_visual,
2251 0, NULL);
2252
2253 xcb_change_property(wm->conn,
2254 XCB_PROP_MODE_REPLACE,
2255 wm->wm_window,
2256 wm->atom.net_supporting_wm_check,
2257 XCB_ATOM_WINDOW,
2258 32, /* format */
2259 1, &wm->wm_window);
2260
2261 xcb_change_property(wm->conn,
2262 XCB_PROP_MODE_REPLACE,
2263 wm->wm_window,
2264 wm->atom.net_wm_name,
2265 wm->atom.utf8_string,
2266 8, /* format */
2267 strlen(name), name);
2268
2269 xcb_change_property(wm->conn,
2270 XCB_PROP_MODE_REPLACE,
2271 wm->screen->root,
2272 wm->atom.net_supporting_wm_check,
2273 XCB_ATOM_WINDOW,
2274 32, /* format */
2275 1, &wm->wm_window);
2276
Abdur Rehmanb8b150b2017-01-01 19:46:46 +05002277 /* Claim the WM_S0 selection even though we don't support
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002278 * the --replace functionality. */
2279 xcb_set_selection_owner(wm->conn,
2280 wm->wm_window,
2281 wm->atom.wm_s0,
2282 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002283
2284 xcb_set_selection_owner(wm->conn,
2285 wm->wm_window,
2286 wm->atom.net_wm_cm_s0,
2287 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002288}
2289
2290struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002291weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002292{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002293 struct weston_wm *wm;
2294 struct wl_event_loop *loop;
2295 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002296 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002297 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002298
Peter Huttererf3d62272013-08-08 11:57:05 +10002299 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002300 if (wm == NULL)
2301 return NULL;
2302
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002303 wm->server = wxs;
2304 wm->window_hash = hash_table_create();
2305 if (wm->window_hash == NULL) {
2306 free(wm);
2307 return NULL;
2308 }
2309
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002310 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002311 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002312 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002313 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002314 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002315 hash_table_destroy(wm->window_hash);
2316 free(wm);
2317 return NULL;
2318 }
2319
2320 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2321 wm->screen = s.data;
2322
2323 loop = wl_display_get_event_loop(wxs->wl_display);
2324 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002325 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002326 WL_EVENT_READABLE,
2327 weston_wm_handle_event, wm);
2328 wl_event_source_check(wm->source);
2329
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002330 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002331 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002332
2333 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002334 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2335 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2336 XCB_EVENT_MASK_PROPERTY_CHANGE;
2337 xcb_change_window_attributes(wm->conn, wm->screen->root,
2338 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002339
2340 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2341 XCB_COMPOSITE_REDIRECT_MANUAL);
2342
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002343 wm->theme = theme_create();
2344
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002345 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002346 supported[1] = wm->atom.net_wm_state;
2347 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002348 supported[3] = wm->atom.net_wm_state_maximized_vert;
2349 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002350 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002351 xcb_change_property(wm->conn,
2352 XCB_PROP_MODE_REPLACE,
2353 wm->screen->root,
2354 wm->atom.net_supported,
2355 XCB_ATOM_ATOM,
2356 32, /* format */
2357 ARRAY_LENGTH(supported), supported);
2358
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002359 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2360
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002361 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002362
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002363 weston_wm_dnd_init(wm);
2364
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002365 xcb_flush(wm->conn);
2366
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002367 wm->create_surface_listener.notify = weston_wm_create_surface;
2368 wl_signal_add(&wxs->compositor->create_surface_signal,
2369 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002370 wm->activate_listener.notify = weston_wm_window_activate;
2371 wl_signal_add(&wxs->compositor->activate_signal,
2372 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002373 wm->kill_listener.notify = weston_wm_kill_client;
2374 wl_signal_add(&wxs->compositor->kill_signal,
2375 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002376 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002377
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002378 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002379 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002380
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002381 /* Create wm window and take WM_S0 selection last, which
2382 * signals to Xwayland that we're done with setup. */
2383 weston_wm_create_wm_window(wm);
2384
2385 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002386
2387 return wm;
2388}
2389
2390void
2391weston_wm_destroy(struct weston_wm *wm)
2392{
2393 /* FIXME: Free windows in hash. */
2394 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002395 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002396 xcb_disconnect(wm->conn);
2397 wl_event_source_remove(wm->source);
2398 wl_list_remove(&wm->selection_listener.link);
2399 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002400 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002401 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002402
2403 free(wm);
2404}
2405
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002406static struct weston_wm_window *
2407get_wm_window(struct weston_surface *surface)
2408{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002409 struct wl_listener *listener;
2410
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002411 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002412 if (listener)
2413 return container_of(listener, struct weston_wm_window,
2414 surface_destroy_listener);
2415
2416 return NULL;
2417}
2418
Quentin Glidic955cec02016-08-12 10:41:35 +02002419static bool
2420is_wm_window(struct weston_surface *surface)
2421{
2422 return get_wm_window(surface) != NULL;
2423}
2424
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002425static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002426weston_wm_window_configure(void *data)
2427{
2428 struct weston_wm_window *window = data;
2429 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002430 uint32_t values[4];
2431 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002432
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002433 weston_wm_window_get_child_position(window, &x, &y);
2434 values[0] = x;
2435 values[1] = y;
2436 values[2] = window->width;
2437 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002438 xcb_configure_window(wm->conn,
2439 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002440 XCB_CONFIG_WINDOW_X |
2441 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002442 XCB_CONFIG_WINDOW_WIDTH |
2443 XCB_CONFIG_WINDOW_HEIGHT,
2444 values);
2445
2446 weston_wm_window_get_frame_size(window, &width, &height);
2447 values[0] = width;
2448 values[1] = height;
2449 xcb_configure_window(wm->conn,
2450 window->frame_id,
2451 XCB_CONFIG_WINDOW_WIDTH |
2452 XCB_CONFIG_WINDOW_HEIGHT,
2453 values);
2454
2455 window->configure_source = NULL;
2456
2457 weston_wm_window_schedule_repaint(window);
2458}
2459
2460static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002461send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002462{
2463 struct weston_wm_window *window = get_wm_window(surface);
2464 struct weston_wm *wm = window->wm;
2465 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002466 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002467
Marek Chalupae9fe4672014-10-29 13:44:44 +01002468 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002469 hborder = 2 * t->width;
2470 vborder = t->titlebar_height + t->width;
2471 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002472 hborder = 0;
2473 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002474 }
2475
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002476 if (width > hborder)
2477 window->width = width - hborder;
2478 else
2479 window->width = 1;
2480
2481 if (height > vborder)
2482 window->height = height - vborder;
2483 else
2484 window->height = 1;
2485
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002486 if (window->frame)
2487 frame_resize_inside(window->frame, window->width, window->height);
2488
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002489 if (window->configure_source)
2490 return;
2491
2492 window->configure_source =
2493 wl_event_loop_add_idle(wm->server->loop,
2494 weston_wm_window_configure, window);
2495}
2496
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002497static void
2498send_position(struct weston_surface *surface, int32_t x, int32_t y)
2499{
2500 struct weston_wm_window *window = get_wm_window(surface);
2501 struct weston_wm *wm;
2502 uint32_t mask, values[2];
2503
2504 if (!window || !window->wm)
2505 return;
2506
2507 wm = window->wm;
2508 /* We use pos_dirty to tell whether a configure message is in flight.
2509 * This is needed in case we send two configure events in a very
2510 * short time, since window->x/y is set in after a roundtrip, hence
2511 * we cannot just check if the current x and y are different. */
2512 if (window->x != x || window->y != y || window->pos_dirty) {
2513 window->pos_dirty = true;
2514 values[0] = x;
2515 values[1] = y;
2516 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2517
2518 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2519 xcb_flush(wm->conn);
2520 }
2521}
2522
Quentin Glidic955cec02016-08-12 10:41:35 +02002523static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002524 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002525};
2526
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002527static int
2528legacy_fullscreen(struct weston_wm *wm,
2529 struct weston_wm_window *window,
2530 struct weston_output **output_ret)
2531{
2532 struct weston_compositor *compositor = wm->server->compositor;
2533 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002534 uint32_t minmax = PMinSize | PMaxSize;
2535 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002536
2537 /* Heuristics for detecting legacy fullscreen windows... */
2538
2539 wl_list_for_each(output, &compositor->output_list, link) {
2540 if (output->x == window->x &&
2541 output->y == window->y &&
2542 output->width == window->width &&
2543 output->height == window->height &&
2544 window->override_redirect) {
2545 *output_ret = output;
2546 return 1;
2547 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002548
2549 matching_size = 0;
2550 if ((window->size_hints.flags & (USSize |PSize)) &&
2551 window->size_hints.width == output->width &&
2552 window->size_hints.height == output->height)
2553 matching_size = 1;
2554 if ((window->size_hints.flags & minmax) == minmax &&
2555 window->size_hints.min_width == output->width &&
2556 window->size_hints.min_height == output->height &&
2557 window->size_hints.max_width == output->width &&
2558 window->size_hints.max_height == output->height)
2559 matching_size = 1;
2560
2561 if (matching_size && !window->decorate &&
2562 (window->size_hints.flags & (USPosition | PPosition)) &&
2563 window->size_hints.x == output->x &&
2564 window->size_hints.y == output->y) {
2565 *output_ret = output;
2566 return 1;
2567 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002568 }
2569
2570 return 0;
2571}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002572
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002573static bool
2574weston_wm_window_type_inactive(struct weston_wm_window *window)
2575{
2576 struct weston_wm *wm = window->wm;
2577
2578 return window->type == wm->atom.net_wm_window_type_tooltip ||
2579 window->type == wm->atom.net_wm_window_type_dropdown ||
2580 window->type == wm->atom.net_wm_window_type_dnd ||
2581 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002582 window->type == wm->atom.net_wm_window_type_popup ||
2583 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002584}
2585
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002586static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002587xserver_map_shell_surface(struct weston_wm_window *window,
2588 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002589{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002590 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002591 struct weston_desktop_xwayland *xwayland =
2592 wm->server->compositor->xwayland;
2593 const struct weston_desktop_xwayland_interface *xwayland_interface =
2594 wm->server->compositor->xwayland_interface;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002595 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002596
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002597 weston_wm_window_read_properties(window);
2598
2599 /* A weston_wm_window may have many different surfaces assigned
2600 * throughout its life, so we must make sure to remove the listener
2601 * from the old surface signal list. */
2602 if (window->surface)
2603 wl_list_remove(&window->surface_destroy_listener.link);
2604
2605 window->surface = surface;
2606 window->surface_destroy_listener.notify = surface_destroy;
2607 wl_signal_add(&window->surface->destroy_signal,
2608 &window->surface_destroy_listener);
2609
2610 weston_wm_window_schedule_repaint(window);
2611
Quentin Glidic955cec02016-08-12 10:41:35 +02002612 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002613 return;
2614
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002615 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002616 weston_log("warning, unexpected in %s: "
2617 "surface's configure hook is already set.\n",
2618 __func__);
2619 return;
2620 }
2621
Murray Calavera883ac022015-06-06 13:02:22 +00002622 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002623 xwayland_interface->create_surface(xwayland,
2624 window->surface,
2625 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002626
Pekka Paalanena04eacc2016-11-28 16:42:25 +02002627 wm_log("XWM: map shell surface, win %d, xwayland surface %p\n",
2628 window->id, window->shsurf);
2629
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002630 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002631 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002632 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002633 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002634
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002635 if (window->fullscreen) {
2636 window->saved_width = window->width;
2637 window->saved_height = window->height;
Pekka Paalanen505237e2016-12-01 15:41:11 +02002638 xwayland_interface->set_fullscreen(window->shsurf,
2639 window->legacy_fullscreen_output.output);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002640 return;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002641 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002642 xwayland_interface->set_xwayland(window->shsurf,
2643 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002644 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002645 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002646 if (weston_wm_window_type_inactive(window)) {
2647 xwayland_interface->set_transient(window->shsurf,
2648 parent->surface,
2649 window->x - parent->x,
2650 window->y - parent->y);
2651 } else {
2652 xwayland_interface->set_toplevel(window->shsurf);
2653 xwayland_interface->set_parent(window->shsurf,
2654 parent->surface);
2655 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002656 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002657 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002658 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002659 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002660 xwayland_interface->set_xwayland(window->shsurf,
2661 window->x,
2662 window->y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002663 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002664 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002665 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002666 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002667}
Quentin Glidic955cec02016-08-12 10:41:35 +02002668
2669const struct weston_xwayland_surface_api surface_api = {
2670 is_wm_window,
2671 send_position,
2672};