blob: 76d5ab212929bc246bf22eefea1d6425f2ba3a31 [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 Paalanen882aff02016-11-16 14:15:18 +020038#include <limits.h>
Pekka Paalanen505237e2016-12-01 15:41:11 +020039#include <assert.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040040#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050041#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040042
Daniel Stone67fe3db2016-10-31 14:51:18 +000043#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040044#include "xwayland.h"
Quentin Glidic955cec02016-08-12 10:41:35 +020045#include "xwayland-internal-interface.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040046
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080047#include "cairo-util.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040048#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070049#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040050
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070051struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070052 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070053 int32_t x, y;
54 int32_t width, height; /* should set so old wm's don't mess up */
55 int32_t min_width, min_height;
56 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070057 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070058 struct {
59 int32_t x;
60 int32_t y;
61 } min_aspect, max_aspect;
62 int32_t base_width, base_height;
63 int32_t win_gravity;
64};
65
66#define USPosition (1L << 0)
67#define USSize (1L << 1)
68#define PPosition (1L << 2)
69#define PSize (1L << 3)
70#define PMinSize (1L << 4)
71#define PMaxSize (1L << 5)
72#define PResizeInc (1L << 6)
73#define PAspect (1L << 7)
74#define PBaseSize (1L << 8)
75#define PWinGravity (1L << 9)
76
Kristian Høgsberg380deee2012-05-21 17:12:41 -040077struct motif_wm_hints {
78 uint32_t flags;
79 uint32_t functions;
80 uint32_t decorations;
81 int32_t input_mode;
82 uint32_t status;
83};
84
85#define MWM_HINTS_FUNCTIONS (1L << 0)
86#define MWM_HINTS_DECORATIONS (1L << 1)
87#define MWM_HINTS_INPUT_MODE (1L << 2)
88#define MWM_HINTS_STATUS (1L << 3)
89
90#define MWM_FUNC_ALL (1L << 0)
91#define MWM_FUNC_RESIZE (1L << 1)
92#define MWM_FUNC_MOVE (1L << 2)
93#define MWM_FUNC_MINIMIZE (1L << 3)
94#define MWM_FUNC_MAXIMIZE (1L << 4)
95#define MWM_FUNC_CLOSE (1L << 5)
96
97#define MWM_DECOR_ALL (1L << 0)
98#define MWM_DECOR_BORDER (1L << 1)
99#define MWM_DECOR_RESIZEH (1L << 2)
100#define MWM_DECOR_TITLE (1L << 3)
101#define MWM_DECOR_MENU (1L << 4)
102#define MWM_DECOR_MINIMIZE (1L << 5)
103#define MWM_DECOR_MAXIMIZE (1L << 6)
104
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700105#define MWM_DECOR_EVERYTHING \
106 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
107 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
108
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400109#define MWM_INPUT_MODELESS 0
110#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
111#define MWM_INPUT_SYSTEM_MODAL 2
112#define MWM_INPUT_FULL_APPLICATION_MODAL 3
113#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
114
115#define MWM_TEAROFF_WINDOW (1L<<0)
116
117#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
118#define _NET_WM_MOVERESIZE_SIZE_TOP 1
119#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
120#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
121#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
122#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
123#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
124#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
125#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
126#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
127#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
128#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
129
Pekka Paalanen505237e2016-12-01 15:41:11 +0200130struct weston_output_weak_ref {
131 struct weston_output *output;
132 struct wl_listener destroy_listener;
133};
134
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400135struct weston_wm_window {
136 struct weston_wm *wm;
137 xcb_window_t id;
138 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500139 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400140 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700141 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 struct weston_surface *surface;
Quentin Glidic955cec02016-08-12 10:41:35 +0200143 struct weston_desktop_xwayland_surface *shsurf;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400144 struct wl_listener surface_destroy_listener;
145 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400146 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400147 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300148 int pid;
149 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400150 char *class;
151 char *name;
152 struct weston_wm_window *transient_for;
153 uint32_t protocols;
154 xcb_atom_t type;
155 int width, height;
Pekka Paalanen882aff02016-11-16 14:15:18 +0200156 int x;
157 int y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200158 bool pos_dirty;
Pekka Paalanen882aff02016-11-16 14:15:18 +0200159 int map_request_x;
160 int map_request_y;
Pekka Paalanen505237e2016-12-01 15:41:11 +0200161 struct weston_output_weak_ref legacy_fullscreen_output;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500162 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400163 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300164 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500165 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500166 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700167 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200168 int maximized_vert;
169 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700170 struct wm_size_hints size_hints;
171 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700172 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400173};
174
175static struct weston_wm_window *
176get_wm_window(struct weston_surface *surface);
177
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400178static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200179weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
180
181static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400182weston_wm_window_schedule_repaint(struct weston_wm_window *window);
183
Pekka Paalanen505237e2016-12-01 15:41:11 +0200184static int
185legacy_fullscreen(struct weston_wm *wm,
186 struct weston_wm_window *window,
187 struct weston_output **output_ret);
188
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700189static void
190xserver_map_shell_surface(struct weston_wm_window *window,
191 struct weston_surface *surface);
192
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400193static int __attribute__ ((format (printf, 1, 2)))
194wm_log(const char *fmt, ...)
195{
196#ifdef WM_DEBUG
197 int l;
198 va_list argp;
199
200 va_start(argp, fmt);
201 l = weston_vlog(fmt, argp);
202 va_end(argp);
203
204 return l;
205#else
206 return 0;
207#endif
208}
209
210static int __attribute__ ((format (printf, 1, 2)))
211wm_log_continue(const char *fmt, ...)
212{
213#ifdef WM_DEBUG
214 int l;
215 va_list argp;
216
217 va_start(argp, fmt);
218 l = weston_vlog_continue(fmt, argp);
219 va_end(argp);
220
221 return l;
222#else
223 return 0;
224#endif
225}
226
Pekka Paalanen505237e2016-12-01 15:41:11 +0200227static void
228weston_output_weak_ref_init(struct weston_output_weak_ref *ref)
229{
230 ref->output = NULL;
231}
232
233static void
234weston_output_weak_ref_clear(struct weston_output_weak_ref *ref)
235{
236 if (!ref->output)
237 return;
238
239 wl_list_remove(&ref->destroy_listener.link);
240 ref->output = NULL;
241}
242
243static void
244weston_output_weak_ref_handle_destroy(struct wl_listener *listener, void *data)
245{
246 struct weston_output_weak_ref *ref;
247
248 ref = wl_container_of(listener, ref, destroy_listener);
249 assert(ref->output == data);
250
251 weston_output_weak_ref_clear(ref);
252}
253
254static void
255weston_output_weak_ref_set(struct weston_output_weak_ref *ref,
256 struct weston_output *output)
257{
258 weston_output_weak_ref_clear(ref);
259
260 if (!output)
261 return;
262
263 ref->destroy_listener.notify = weston_output_weak_ref_handle_destroy;
264 wl_signal_add(&output->destroy_signal, &ref->destroy_listener);
265 ref->output = output;
266}
267
Derek Foreman49372142015-04-09 10:51:22 -0500268static bool __attribute__ ((warn_unused_result))
269wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
270 struct weston_wm_window **window)
271{
272 *window = hash_table_lookup(wm->window_hash, hash);
273 if (*window)
274 return true;
275 return false;
276}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400277
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400278const char *
279get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
280{
281 xcb_get_atom_name_cookie_t cookie;
282 xcb_get_atom_name_reply_t *reply;
283 xcb_generic_error_t *e;
284 static char buffer[64];
285
286 if (atom == XCB_ATOM_NONE)
287 return "None";
288
289 cookie = xcb_get_atom_name (c, atom);
290 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500291
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300292 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500293 snprintf(buffer, sizeof buffer, "%.*s",
294 xcb_get_atom_name_name_length (reply),
295 xcb_get_atom_name_name (reply));
296 } else {
297 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
298 }
299
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400300 free(reply);
301
302 return buffer;
303}
304
Tiago Vignatti90fada42012-07-16 12:02:08 -0400305static xcb_cursor_t
306xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
307{
308 xcb_connection_t *c = wm->conn;
309 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
310 xcb_screen_t *screen = s.data;
311 xcb_gcontext_t gc;
312 xcb_pixmap_t pix;
313 xcb_render_picture_t pic;
314 xcb_cursor_t cursor;
315 int stride = img->width * 4;
316
317 pix = xcb_generate_id(c);
318 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
319
320 pic = xcb_generate_id(c);
321 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
322
323 gc = xcb_generate_id(c);
324 xcb_create_gc(c, gc, pix, 0, 0);
325
326 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
327 img->width, img->height, 0, 0, 0, 32,
328 stride * img->height, (uint8_t *) img->pixels);
329 xcb_free_gc(c, gc);
330
331 cursor = xcb_generate_id(c);
332 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
333
334 xcb_render_free_picture(c, pic);
335 xcb_free_pixmap(c, pix);
336
337 return cursor;
338}
339
340static xcb_cursor_t
341xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
342{
343 /* TODO: treat animated cursors as well */
344 if (images->nimage != 1)
345 return -1;
346
347 return xcb_cursor_image_load_cursor(wm, images->images[0]);
348}
349
350static xcb_cursor_t
351xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
352{
353 xcb_cursor_t cursor;
354 XcursorImages *images;
355 char *v = NULL;
356 int size = 0;
357
358 if (!file)
359 return 0;
360
361 v = getenv ("XCURSOR_SIZE");
362 if (v)
363 size = atoi(v);
364
365 if (!size)
366 size = 32;
367
368 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300369 if (!images)
370 return -1;
371
Tiago Vignatti90fada42012-07-16 12:02:08 -0400372 cursor = xcb_cursor_images_load_cursor (wm, images);
373 XcursorImagesDestroy (images);
374
375 return cursor;
376}
377
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400378void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400379dump_property(struct weston_wm *wm,
380 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381{
382 int32_t *incr_value;
383 const char *text_value, *name;
384 xcb_atom_t *atom_value;
385 int width, len;
386 uint32_t i;
387
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400388 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400389 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400390 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400391 return;
392 }
393
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400394 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
395 get_atom_name(wm->conn, reply->type),
396 reply->format,
397 xcb_get_property_value_length(reply),
398 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399
400 if (reply->type == wm->atom.incr) {
401 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400402 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400403 } else if (reply->type == wm->atom.utf8_string ||
404 reply->type == wm->atom.string) {
405 text_value = xcb_get_property_value(reply);
406 if (reply->value_len > 40)
407 len = 40;
408 else
409 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400410 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 } else if (reply->type == XCB_ATOM_ATOM) {
412 atom_value = xcb_get_property_value(reply);
413 for (i = 0; i < reply->value_len; i++) {
414 name = get_atom_name(wm->conn, atom_value[i]);
415 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400416 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400417 width = 4;
418 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400419 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400420 }
421
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400422 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400424 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400425 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400426 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400427 }
428}
429
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200430static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400431read_and_dump_property(struct weston_wm *wm,
432 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400433{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400434 xcb_get_property_reply_t *reply;
435 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400436
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400437 cookie = xcb_get_property(wm->conn, 0, window,
438 property, XCB_ATOM_ANY, 0, 2048);
439 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400440
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400441 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400442
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400443 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400444}
445
Pekka Paalanen20111d52016-12-01 17:07:12 +0200446/* We reuse some predefined, but otherwise useles atoms
447 * as local type placeholders that never touch the X11 server,
448 * to make weston_wm_window_read_properties() less exceptional.
449 */
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400450#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
451#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500452#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700453#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400454
455static void
456weston_wm_window_read_properties(struct weston_wm_window *window)
457{
458 struct weston_wm *wm = window->wm;
459
Pekka Paalanen20111d52016-12-01 17:07:12 +0200460#define F(field) (&window->field)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400461 const struct {
462 xcb_atom_t atom;
463 xcb_atom_t type;
Pekka Paalanen20111d52016-12-01 17:07:12 +0200464 void *ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400465 } props[] = {
Pekka Paalanen20111d52016-12-01 17:07:12 +0200466 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
467 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
468 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
469 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, NULL },
470 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, NULL },
471 { wm->atom.net_wm_state, TYPE_NET_WM_STATE, NULL },
472 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
473 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
474 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
475 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, NULL },
476 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400477 };
478#undef F
479
480 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
481 xcb_get_property_reply_t *reply;
482 void *p;
483 uint32_t *xid;
484 xcb_atom_t *atom;
485 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200486 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400487
488 if (!window->properties_dirty)
489 return;
490 window->properties_dirty = 0;
491
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400492 for (i = 0; i < ARRAY_LENGTH(props); i++)
493 cookie[i] = xcb_get_property(wm->conn,
494 0, /* delete */
495 window->id,
496 props[i].atom,
497 XCB_ATOM_ANY, 0, 2048);
498
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700499 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700500 window->size_hints.flags = 0;
501 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700502 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700503
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400504 for (i = 0; i < ARRAY_LENGTH(props); i++) {
505 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
506 if (!reply)
507 /* Bad window, typically */
508 continue;
509 if (reply->type == XCB_ATOM_NONE) {
510 /* No such property */
511 free(reply);
512 continue;
513 }
514
Pekka Paalanen20111d52016-12-01 17:07:12 +0200515 p = props[i].ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400516
517 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300518 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400519 case XCB_ATOM_STRING:
520 /* FIXME: We're using this for both string and
521 utf8_string */
522 if (*(char **) p)
523 free(*(char **) p);
524
525 *(char **) p =
526 strndup(xcb_get_property_value(reply),
527 xcb_get_property_value_length(reply));
528 break;
529 case XCB_ATOM_WINDOW:
530 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500531 if (!wm_lookup_window(wm, *xid, p))
532 weston_log("XCB_ATOM_WINDOW contains window"
533 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400534 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300535 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400536 case XCB_ATOM_ATOM:
537 atom = xcb_get_property_value(reply);
538 *(xcb_atom_t *) p = *atom;
539 break;
540 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700541 atom = xcb_get_property_value(reply);
542 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500543 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700544 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500545 break;
546 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400547 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700548 case TYPE_WM_NORMAL_HINTS:
549 memcpy(&window->size_hints,
550 xcb_get_property_value(reply),
551 sizeof window->size_hints);
552 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500553 case TYPE_NET_WM_STATE:
554 window->fullscreen = 0;
555 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900556 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500557 if (atom[i] == wm->atom.net_wm_state_fullscreen)
558 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200559 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
560 window->maximized_vert = 1;
561 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
562 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900563 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500564 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400565 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700566 memcpy(&window->motif_hints,
567 xcb_get_property_value(reply),
568 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700569 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
570 if (window->motif_hints.decorations & MWM_DECOR_ALL)
571 /* MWM_DECOR_ALL means all except the other values listed. */
572 window->decorate =
573 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
574 else
575 window->decorate =
576 window->motif_hints.decorations;
577 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400578 break;
579 default:
580 break;
581 }
582 free(reply);
583 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200584
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200585 if (window->pid > 0) {
586 gethostname(name, sizeof(name));
587 for (i = 0; i < sizeof(name); i++) {
588 if (name[i] == '\0')
589 break;
590 }
591 if (i == sizeof(name))
592 name[0] = '\0'; /* ignore stupid hostnames */
593
594 /* this is only one heuristic to guess the PID of a client is
595 * valid, assuming it's compliant with icccm and ewmh.
596 * Non-compliants and remote applications of course fail. */
597 if (!window->machine || strcmp(window->machine, name))
598 window->pid = 0;
599 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400600}
601
Pekka Paalanen20111d52016-12-01 17:07:12 +0200602#undef TYPE_WM_PROTOCOLS
603#undef TYPE_MOTIF_WM_HINTS
604#undef TYPE_NET_WM_STATE
605#undef TYPE_WM_NORMAL_HINTS
606
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400607static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400608weston_wm_window_get_frame_size(struct weston_wm_window *window,
609 int *width, int *height)
610{
611 struct theme *t = window->wm->theme;
612
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500613 if (window->fullscreen) {
614 *width = window->width;
615 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800616 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500617 *width = frame_width(window->frame);
618 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400619 } else {
620 *width = window->width + t->margin * 2;
621 *height = window->height + t->margin * 2;
622 }
623}
624
625static void
626weston_wm_window_get_child_position(struct weston_wm_window *window,
627 int *x, int *y)
628{
629 struct theme *t = window->wm->theme;
630
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500631 if (window->fullscreen) {
632 *x = 0;
633 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800634 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500635 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400636 } else {
637 *x = t->margin;
638 *y = t->margin;
639 }
640}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400641
642static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500643weston_wm_window_send_configure_notify(struct weston_wm_window *window)
644{
645 xcb_configure_notify_event_t configure_notify;
646 struct weston_wm *wm = window->wm;
647 int x, y;
648
649 weston_wm_window_get_child_position(window, &x, &y);
650 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
651 configure_notify.pad0 = 0;
652 configure_notify.event = window->id;
653 configure_notify.window = window->id;
654 configure_notify.above_sibling = XCB_WINDOW_NONE;
655 configure_notify.x = x;
656 configure_notify.y = y;
657 configure_notify.width = window->width;
658 configure_notify.height = window->height;
659 configure_notify.border_width = 0;
660 configure_notify.override_redirect = 0;
661 configure_notify.pad1 = 0;
662
Murray Calavera883ac022015-06-06 13:02:22 +0000663 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500664 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
665 (char *) &configure_notify);
666}
667
668static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400669weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
670{
Murray Calavera883ac022015-06-06 13:02:22 +0000671 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400672 (xcb_configure_request_event_t *) event;
673 struct weston_wm_window *window;
674 uint32_t mask, values[16];
675 int x, y, width, height, i = 0;
676
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400677 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
678 configure_request->window,
679 configure_request->x, configure_request->y,
680 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400681
Derek Foreman49372142015-04-09 10:51:22 -0500682 if (!wm_lookup_window(wm, configure_request->window, &window))
683 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400684
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500685 if (window->fullscreen) {
686 weston_wm_window_send_configure_notify(window);
687 return;
688 }
689
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400690 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
691 window->width = configure_request->width;
692 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
693 window->height = configure_request->height;
694
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500695 if (window->frame)
696 frame_resize_inside(window->frame, window->width, window->height);
697
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400698 weston_wm_window_get_child_position(window, &x, &y);
699 values[i++] = x;
700 values[i++] = y;
701 values[i++] = window->width;
702 values[i++] = window->height;
703 values[i++] = 0;
704 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
705 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
706 XCB_CONFIG_WINDOW_BORDER_WIDTH;
707 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
708 values[i++] = configure_request->sibling;
709 mask |= XCB_CONFIG_WINDOW_SIBLING;
710 }
711 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
712 values[i++] = configure_request->stack_mode;
713 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
714 }
715
716 xcb_configure_window(wm->conn, window->id, mask, values);
717
718 weston_wm_window_get_frame_size(window, &width, &height);
719 values[0] = width;
720 values[1] = height;
721 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
722 xcb_configure_window(wm->conn, window->frame_id, mask, values);
723
724 weston_wm_window_schedule_repaint(window);
725}
726
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400727static int
728our_resource(struct weston_wm *wm, uint32_t id)
729{
730 const xcb_setup_t *setup;
731
732 setup = xcb_get_setup(wm->conn);
733
734 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
735}
736
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737static void
738weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
739{
Murray Calavera883ac022015-06-06 13:02:22 +0000740 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400741 (xcb_configure_notify_event_t *) event;
742 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743
Pekka Paalanen73428a82016-12-19 15:10:01 +0200744 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d%s\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400745 configure_notify->window,
746 configure_notify->x, configure_notify->y,
Pekka Paalanen73428a82016-12-19 15:10:01 +0200747 configure_notify->width, configure_notify->height,
748 configure_notify->override_redirect ? ", override" : "");
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300749
Derek Foreman49372142015-04-09 10:51:22 -0500750 if (!wm_lookup_window(wm, configure_notify->window, &window))
751 return;
752
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700753 window->x = configure_notify->x;
754 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200755 window->pos_dirty = false;
756
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700757 if (window->override_redirect) {
758 window->width = configure_notify->width;
759 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500760 if (window->frame)
761 frame_resize_inside(window->frame,
762 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700763 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764}
765
766static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300767weston_wm_kill_client(struct wl_listener *listener, void *data)
768{
769 struct weston_surface *surface = data;
770 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300771 if (!window)
772 return;
773
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200774 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300775 kill(window->pid, SIGKILL);
776}
777
778static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700779weston_wm_create_surface(struct wl_listener *listener, void *data)
780{
781 struct weston_surface *surface = data;
782 struct weston_wm *wm =
783 container_of(listener,
784 struct weston_wm, create_surface_listener);
785 struct weston_wm_window *window;
786
787 if (wl_resource_get_client(surface->resource) != wm->server->client)
788 return;
789
790 wl_list_for_each(window, &wm->unpaired_window_list, link)
791 if (window->surface_id ==
792 wl_resource_get_id(surface->resource)) {
793 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700794 window->surface_id = 0;
795 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700796 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000797 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700798}
799
800static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300801weston_wm_send_focus_window(struct weston_wm *wm,
802 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400803{
Scott Moreau85ecac02012-05-21 15:49:14 -0600804 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400805
Scott Moreau85ecac02012-05-21 15:49:14 -0600806 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700807 uint32_t values[1];
808
Boyan Dingb9f863c2014-08-30 10:33:23 +0800809 if (window->override_redirect)
810 return;
811
Scott Moreau85ecac02012-05-21 15:49:14 -0600812 client_message.response_type = XCB_CLIENT_MESSAGE;
813 client_message.format = 32;
814 client_message.window = window->id;
815 client_message.type = wm->atom.wm_protocols;
816 client_message.data.data32[0] = wm->atom.wm_take_focus;
817 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
818
Murray Calavera883ac022015-06-06 13:02:22 +0000819 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600820 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
821 (char *) &client_message);
822
823 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
824 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700825
826 values[0] = XCB_STACK_MODE_ABOVE;
827 xcb_configure_window (wm->conn, window->frame_id,
828 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600829 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400830 xcb_set_input_focus (wm->conn,
831 XCB_INPUT_FOCUS_POINTER_ROOT,
832 XCB_NONE,
833 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600834 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300835}
836
837static void
838weston_wm_window_activate(struct wl_listener *listener, void *data)
839{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800840 struct weston_surface_activation_data *activation_data = data;
841 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300842 struct weston_wm_window *window = NULL;
843 struct weston_wm *wm =
844 container_of(listener, struct weston_wm, activate_listener);
845
846 if (surface) {
847 window = get_wm_window(surface);
848 }
849
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200850 if (window) {
851 weston_wm_set_net_active_window(wm, window->id);
852 } else {
853 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
854 }
855
Giulio Camuffob18f7882015-03-29 14:20:23 +0300856 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400857
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500858 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800859 if (wm->focus_window->frame)
860 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400861 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500862 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400863 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500864 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800865 if (wm->focus_window->frame)
866 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500868 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200869
870 xcb_flush(wm->conn);
871
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400872}
873
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400874#define ICCCM_WITHDRAWN_STATE 0
875#define ICCCM_NORMAL_STATE 1
876#define ICCCM_ICONIC_STATE 3
877
878static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500879weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400880{
881 struct weston_wm *wm = window->wm;
882 uint32_t property[2];
883
884 property[0] = state;
885 property[1] = XCB_WINDOW_NONE;
886
887 xcb_change_property(wm->conn,
888 XCB_PROP_MODE_REPLACE,
889 window->id,
890 wm->atom.wm_state,
891 wm->atom.wm_state,
892 32, /* format */
893 2, property);
894}
895
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400896static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500897weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
898{
899 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200900 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500901 int i;
902
903 i = 0;
904 if (window->fullscreen)
905 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200906 if (window->maximized_vert)
907 property[i++] = wm->atom.net_wm_state_maximized_vert;
908 if (window->maximized_horz)
909 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500910
911 xcb_change_property(wm->conn,
912 XCB_PROP_MODE_REPLACE,
913 window->id,
914 wm->atom.net_wm_state,
915 XCB_ATOM_ATOM,
916 32, /* format */
917 i, property);
918}
919
920static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700921weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400922{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700923 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400924 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400925 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200926 int buttons = FRAME_BUTTON_CLOSE;
927
928 if (window->decorate & MWM_DECOR_MAXIMIZE)
929 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400930
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500931 window->frame = frame_create(window->wm->theme,
932 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200933 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500934 frame_resize_inside(window->frame, window->width, window->height);
935
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400936 weston_wm_window_get_frame_size(window, &width, &height);
937 weston_wm_window_get_child_position(window, &x, &y);
938
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400939 values[0] = wm->screen->black_pixel;
940 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400941 XCB_EVENT_MASK_KEY_PRESS |
942 XCB_EVENT_MASK_KEY_RELEASE |
943 XCB_EVENT_MASK_BUTTON_PRESS |
944 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400945 XCB_EVENT_MASK_POINTER_MOTION |
946 XCB_EVENT_MASK_ENTER_WINDOW |
947 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400949 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400950 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400951
952 window->frame_id = xcb_generate_id(wm->conn);
953 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400954 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400955 window->frame_id,
956 wm->screen->root,
957 0, 0,
958 width, height,
959 0,
960 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400961 wm->visual_id,
962 XCB_CW_BORDER_PIXEL |
963 XCB_CW_EVENT_MASK |
964 XCB_CW_COLORMAP, values);
965
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400966 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
967
968 values[0] = 0;
969 xcb_configure_window(wm->conn, window->id,
970 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
971
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400972 window->cairo_surface =
973 cairo_xcb_surface_create_with_xrender_format(wm->conn,
974 wm->screen,
975 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400976 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400977 width, height);
978
979 hash_table_insert(wm->window_hash, window->frame_id, window);
980}
981
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200982/*
983 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
984 * Passing a <0 desktop value deletes the property.
985 */
986static void
987weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700988 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200989{
990 if (desktop >= 0) {
991 xcb_change_property(window->wm->conn,
992 XCB_PROP_MODE_REPLACE,
993 window->id,
994 window->wm->atom.net_wm_desktop,
995 XCB_ATOM_CARDINAL,
996 32, /* format */
997 1, &desktop);
998 } else {
999 xcb_delete_property(window->wm->conn,
1000 window->id,
1001 window->wm->atom.net_wm_desktop);
1002 }
1003}
1004
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001005static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001006weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
1007{
1008 xcb_map_request_event_t *map_request =
1009 (xcb_map_request_event_t *) event;
1010 struct weston_wm_window *window;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001011 struct weston_output *output;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001012
1013 if (our_resource(wm, map_request->window)) {
1014 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
1015 map_request->window);
1016 return;
1017 }
1018
Derek Foreman49372142015-04-09 10:51:22 -05001019 if (!wm_lookup_window(wm, map_request->window, &window))
1020 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001021
1022 weston_wm_window_read_properties(window);
1023
Pekka Paalanenc9ca2c02016-12-15 16:04:14 +02001024 /* For a new Window, MapRequest happens before the Window is realized
1025 * in Xwayland. We do the real xcb_map_window() here as a response to
1026 * MapRequest. The Window will get realized (wl_surface created in
1027 * Wayland and WL_SURFACE_ID sent in X11) when it has been mapped for
1028 * real.
1029 *
1030 * MapRequest only happens for (X11) unmapped Windows. On UnmapNotify,
1031 * we reset shsurf to NULL, so even if X11 connection races far ahead
1032 * of the Wayland connection and the X11 client is repeatedly mapping
1033 * and unmapping, we will never have shsurf set on MapRequest.
1034 */
1035 assert(!window->shsurf);
1036
Pekka Paalanen882aff02016-11-16 14:15:18 +02001037 window->map_request_x = window->x;
1038 window->map_request_y = window->y;
1039
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001040 if (window->frame_id == XCB_WINDOW_NONE)
1041 weston_wm_window_create_frame(window);
1042
Pekka Paalanen44660c32016-11-14 15:38:43 +02001043 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d, %dx%d @ %d,%d)\n",
1044 window->id, window, window->frame_id,
Pekka Paalanen882aff02016-11-16 14:15:18 +02001045 window->width, window->height,
1046 window->map_request_x, window->map_request_y);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001047
1048 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1049 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001050 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001051
Pekka Paalanen505237e2016-12-01 15:41:11 +02001052 if (legacy_fullscreen(wm, window, &output)) {
1053 window->fullscreen = 1;
1054 weston_output_weak_ref_set(&window->legacy_fullscreen_output,
1055 output);
1056 }
1057
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001058 xcb_map_window(wm->conn, map_request->window);
1059 xcb_map_window(wm->conn, window->frame_id);
1060}
1061
1062static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001063weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1064{
1065 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1066
1067 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001068 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1069 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001070 return;
1071 }
1072
Pekka Paalanen73428a82016-12-19 15:10:01 +02001073 wm_log("XCB_MAP_NOTIFY (window %d%s)\n", map_notify->window,
1074 map_notify->override_redirect ? ", override" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001075}
1076
1077static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001078weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1079{
1080 xcb_unmap_notify_event_t *unmap_notify =
1081 (xcb_unmap_notify_event_t *) event;
1082 struct weston_wm_window *window;
1083
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001084 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1085 unmap_notify->window,
1086 unmap_notify->event,
1087 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001088
1089 if (our_resource(wm, unmap_notify->window))
1090 return;
1091
MoD31700122013-06-11 19:58:55 -05001092 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001093 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1094 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001095 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001096
Derek Foreman49372142015-04-09 10:51:22 -05001097 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1098 return;
1099
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001100 if (window->surface_id) {
1101 /* Make sure we're not on the unpaired surface list or we
1102 * could be assigned a surface during surface creation that
1103 * was mapped before this unmap request.
1104 */
1105 wl_list_remove(&window->link);
1106 window->surface_id = 0;
1107 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001108 if (wm->focus_window == window)
1109 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001110 if (window->surface)
1111 wl_list_remove(&window->surface_destroy_listener.link);
1112 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001113 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001114
1115 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1116 weston_wm_window_set_virtual_desktop(window, -1);
1117
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001118 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001119}
1120
1121static void
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001122weston_wm_window_draw_decoration(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001123{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001124 cairo_t *cr;
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001125 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001126
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001127 wm_log("XWM: draw decoration, win %d\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001128
1129 weston_wm_window_get_frame_size(window, &width, &height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001130
1131 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1132 cr = cairo_create(window->cairo_surface);
1133
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001134 if (window->fullscreen) {
1135 /* nothing */
1136 } else if (window->decorate) {
Pekka Paalanen9a330e12016-12-15 15:37:24 +02001137 frame_set_title(window->frame, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001138 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001139 } else {
1140 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1141 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1142 cairo_paint(cr);
1143
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001144 render_shadow(cr, window->wm->theme->shadow,
1145 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001146 }
1147
1148 cairo_destroy(cr);
Pekka Paalanendb7b9f32017-01-16 17:01:11 +02001149 cairo_surface_flush(window->cairo_surface);
1150 xcb_flush(window->wm->conn);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001151}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001152
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001153static void
1154weston_wm_window_set_pending_state(struct weston_wm_window *window)
1155{
1156 int x, y, width, height;
1157 int32_t input_x, input_y, input_w, input_h;
1158 const struct weston_desktop_xwayland_interface *xwayland_interface =
1159 window->wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001160
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001161 if (!window->surface)
1162 return;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001163
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001164 weston_wm_window_get_frame_size(window, &width, &height);
1165 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001166
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001167 pixman_region32_fini(&window->surface->pending.opaque);
1168 if (window->has_alpha) {
1169 pixman_region32_init(&window->surface->pending.opaque);
1170 } else {
1171 /* We leave an extra pixel around the X window area to
1172 * make sure we don't sample from the undefined alpha
1173 * channel when filtering. */
1174 pixman_region32_init_rect(&window->surface->pending.opaque,
1175 x - 1, y - 1,
1176 window->width + 2,
1177 window->height + 2);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001178 }
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001179
1180 pixman_region32_fini(&window->surface->pending.input);
1181
1182 if (window->decorate && !window->fullscreen) {
1183 frame_input_rect(window->frame, &input_x, &input_y,
1184 &input_w, &input_h);
1185 } else {
1186 input_x = x;
1187 input_y = y;
1188 input_w = width;
1189 input_h = height;
1190 }
1191
1192 wm_log("XWM: win %d geometry: %d,%d %dx%d\n",
1193 window->id, input_x, input_y, input_w, input_h);
1194
1195 pixman_region32_init_rect(&window->surface->pending.input,
1196 input_x, input_y, input_w, input_h);
1197
1198 xwayland_interface->set_window_geometry(window->shsurf,
1199 input_x, input_y, input_w, input_h);
1200 if (window->name)
1201 xwayland_interface->set_title(window->shsurf, window->name);
1202 if (window->pid > 0)
1203 xwayland_interface->set_pid(window->shsurf, window->pid);
1204}
1205
1206static void
1207weston_wm_window_do_repaint(void *data)
1208{
1209 struct weston_wm_window *window = data;
1210
1211 window->repaint_source = NULL;
1212
1213 weston_wm_window_read_properties(window);
1214
1215 weston_wm_window_draw_decoration(window);
1216 weston_wm_window_set_pending_state(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001217}
1218
1219static void
1220weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1221{
1222 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001223 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001224
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001225 if (window->frame_id == XCB_WINDOW_NONE) {
1226 if (window->surface != NULL) {
Pekka Paalanen73428a82016-12-19 15:10:01 +02001227 /* Override-redirect windows go through here, but we
1228 * cannot assert(window->override_redirect); because
1229 * we do not deal with changing OR flag yet.
1230 * XXX: handle OR flag changes in message handlers
1231 */
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001232 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001233 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001234 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001235 pixman_region32_init(&window->surface->pending.opaque);
1236 } else {
1237 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1238 width, height);
1239 }
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001240 }
1241 return;
1242 }
1243
1244 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001245 return;
1246
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001247 wm_log("XWM: schedule repaint, win %d\n", window->id);
1248
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001249 window->repaint_source =
1250 wl_event_loop_add_idle(wm->server->loop,
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001251 weston_wm_window_do_repaint, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001252}
1253
1254static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001255weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1256{
1257 xcb_property_notify_event_t *property_notify =
1258 (xcb_property_notify_event_t *) event;
1259 struct weston_wm_window *window;
1260
Derek Foreman49372142015-04-09 10:51:22 -05001261 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001262 return;
1263
1264 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001265
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001266 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001267 if (property_notify->state == XCB_PROPERTY_DELETE)
Pekka Paalanen8cc153b2016-12-19 15:18:45 +02001268 wm_log_continue("deleted %s\n",
1269 get_atom_name(wm->conn, property_notify->atom));
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001270 else
1271 read_and_dump_property(wm, property_notify->window,
1272 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001273
1274 if (property_notify->atom == wm->atom.net_wm_name ||
1275 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001276 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001277}
1278
1279static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001280weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001281 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001282{
1283 struct weston_wm_window *window;
1284 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001285 xcb_get_geometry_cookie_t geometry_cookie;
1286 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001287
Peter Huttererf3d62272013-08-08 11:57:05 +10001288 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001289 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001290 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001291 return;
1292 }
1293
MoD384a11a2013-06-22 11:04:21 -05001294 geometry_cookie = xcb_get_geometry(wm->conn, id);
1295
Giulio Camuffob18f7882015-03-29 14:20:23 +03001296 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1297 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001298 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1299
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001300 window->wm = wm;
1301 window->id = id;
1302 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001303 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001304 window->width = width;
1305 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001306 window->x = x;
1307 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001308 window->pos_dirty = false;
Pekka Paalanen882aff02016-11-16 14:15:18 +02001309 window->map_request_x = INT_MIN; /* out of range for valid positions */
1310 window->map_request_y = INT_MIN; /* out of range for valid positions */
Pekka Paalanen505237e2016-12-01 15:41:11 +02001311 weston_output_weak_ref_init(&window->legacy_fullscreen_output);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001312
MoD384a11a2013-06-22 11:04:21 -05001313 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1314 /* technically we should use XRender and check the visual format's
1315 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001316 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001317 window->has_alpha = geometry_reply->depth == 32;
1318 free(geometry_reply);
1319
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001320 hash_table_insert(wm->window_hash, id, window);
1321}
1322
1323static void
1324weston_wm_window_destroy(struct weston_wm_window *window)
1325{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001326 struct weston_wm *wm = window->wm;
1327
Pekka Paalanen505237e2016-12-01 15:41:11 +02001328 weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1329
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001330 if (window->repaint_source)
1331 wl_event_source_remove(window->repaint_source);
1332 if (window->cairo_surface)
1333 cairo_surface_destroy(window->cairo_surface);
1334
1335 if (window->frame_id) {
1336 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1337 xcb_destroy_window(wm->conn, window->frame_id);
1338 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001339 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001340 hash_table_remove(wm->window_hash, window->frame_id);
1341 window->frame_id = XCB_WINDOW_NONE;
1342 }
1343
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001344 if (window->surface_id)
1345 wl_list_remove(&window->link);
1346
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001347 if (window->surface)
1348 wl_list_remove(&window->surface_destroy_listener.link);
1349
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001350 hash_table_remove(window->wm->window_hash, window->id);
1351 free(window);
1352}
1353
1354static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001355weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1356{
1357 xcb_create_notify_event_t *create_notify =
1358 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001359
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001360 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 -04001361 create_notify->window,
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001362 create_notify->x, create_notify->y,
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001363 create_notify->width, create_notify->height,
1364 create_notify->override_redirect ? ", override" : "",
1365 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001366
1367 if (our_resource(wm, create_notify->window))
1368 return;
1369
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001370 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001371 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001372 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001373 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001374}
1375
1376static void
1377weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1378{
1379 xcb_destroy_notify_event_t *destroy_notify =
1380 (xcb_destroy_notify_event_t *) event;
1381 struct weston_wm_window *window;
1382
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001383 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1384 destroy_notify->window,
1385 destroy_notify->event,
1386 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001387
1388 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001389 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001390
Derek Foreman49372142015-04-09 10:51:22 -05001391 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1392 return;
1393
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001394 weston_wm_window_destroy(window);
1395}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001396
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001397static void
1398weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1399{
1400 xcb_reparent_notify_event_t *reparent_notify =
1401 (xcb_reparent_notify_event_t *) event;
1402 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001403
Pekka Paalanen73428a82016-12-19 15:10:01 +02001404 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d%s)\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001405 reparent_notify->window,
1406 reparent_notify->parent,
Pekka Paalanen73428a82016-12-19 15:10:01 +02001407 reparent_notify->event,
1408 reparent_notify->override_redirect ? ", override" : "");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001409
1410 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001411 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001412 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001413 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001414 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001415 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1416 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001417 weston_wm_window_destroy(window);
1418 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001419}
1420
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001421struct weston_seat *
1422weston_wm_pick_seat(struct weston_wm *wm)
1423{
Tom Hochsteine7fff212016-11-01 14:14:00 -05001424 struct wl_list *seats = wm->server->compositor->seat_list.next;
1425 if (wl_list_empty(seats))
1426 return NULL;
1427 return container_of(seats, struct weston_seat, link);
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001428}
1429
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001430static struct weston_seat *
1431weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1432{
1433 struct weston_wm *wm = window->wm;
1434 struct weston_seat *seat, *s;
1435
1436 seat = NULL;
1437 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001438 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1439 struct weston_pointer *old_pointer =
1440 weston_seat_get_pointer(seat);
1441
1442 if (pointer && pointer->focus &&
1443 pointer->focus->surface == window->surface &&
1444 pointer->button_count > 0 &&
1445 (!seat ||
1446 pointer->grab_serial -
1447 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001448 seat = s;
1449 }
1450
1451 return seat;
1452}
1453
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001454static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001455weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1456 xcb_client_message_event_t *client_message)
1457{
1458 static const int map[] = {
1459 THEME_LOCATION_RESIZING_TOP_LEFT,
1460 THEME_LOCATION_RESIZING_TOP,
1461 THEME_LOCATION_RESIZING_TOP_RIGHT,
1462 THEME_LOCATION_RESIZING_RIGHT,
1463 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1464 THEME_LOCATION_RESIZING_BOTTOM,
1465 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1466 THEME_LOCATION_RESIZING_LEFT
1467 };
1468
1469 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001470 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001471 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001472 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001473 const struct weston_desktop_xwayland_interface *xwayland_interface =
1474 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001475
Derek Foreman1281a362015-07-31 16:55:32 -05001476 if (!pointer || pointer->button_count != 1
1477 || !pointer->focus
1478 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001479 return;
1480
1481 detail = client_message->data.data32[2];
1482 switch (detail) {
1483 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001484 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001485 break;
1486 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1487 case _NET_WM_MOVERESIZE_SIZE_TOP:
1488 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1489 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1490 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1491 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1492 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1493 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001494 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001495 break;
1496 case _NET_WM_MOVERESIZE_CANCEL:
1497 break;
1498 }
1499}
1500
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001501#define _NET_WM_STATE_REMOVE 0
1502#define _NET_WM_STATE_ADD 1
1503#define _NET_WM_STATE_TOGGLE 2
1504
1505static int
1506update_state(int action, int *state)
1507{
1508 int new_state, changed;
1509
1510 switch (action) {
1511 case _NET_WM_STATE_REMOVE:
1512 new_state = 0;
1513 break;
1514 case _NET_WM_STATE_ADD:
1515 new_state = 1;
1516 break;
1517 case _NET_WM_STATE_TOGGLE:
1518 new_state = !*state;
1519 break;
1520 default:
1521 return 0;
1522 }
1523
1524 changed = (*state != new_state);
1525 *state = new_state;
1526
1527 return changed;
1528}
1529
1530static void
1531weston_wm_window_configure(void *data);
1532
1533static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001534weston_wm_window_set_toplevel(struct weston_wm_window *window)
1535{
Quentin Glidic955cec02016-08-12 10:41:35 +02001536 const struct weston_desktop_xwayland_interface *xwayland_interface =
1537 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001538
Quentin Glidic955cec02016-08-12 10:41:35 +02001539 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001540 window->width = window->saved_width;
1541 window->height = window->saved_height;
1542 if (window->frame)
1543 frame_resize_inside(window->frame,
1544 window->width,
1545 window->height);
1546 weston_wm_window_configure(window);
1547}
1548
1549static inline bool
1550weston_wm_window_is_maximized(struct weston_wm_window *window)
1551{
1552 return window->maximized_horz && window->maximized_vert;
1553}
1554
1555static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001556weston_wm_window_handle_state(struct weston_wm_window *window,
1557 xcb_client_message_event_t *client_message)
1558{
1559 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001560 const struct weston_desktop_xwayland_interface *xwayland_interface =
1561 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001562 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001563 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001564
1565 action = client_message->data.data32[0];
1566 property = client_message->data.data32[1];
1567
1568 if (property == wm->atom.net_wm_state_fullscreen &&
1569 update_state(action, &window->fullscreen)) {
1570 weston_wm_window_set_net_wm_state(window);
1571 if (window->fullscreen) {
1572 window->saved_width = window->width;
1573 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001574
1575 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001576 xwayland_interface->set_fullscreen(window->shsurf,
1577 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001578 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001579 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001580 weston_wm_window_set_toplevel(window);
1581 }
1582 } else {
1583 if (property == wm->atom.net_wm_state_maximized_vert &&
1584 update_state(action, &window->maximized_vert))
1585 weston_wm_window_set_net_wm_state(window);
1586 if (property == wm->atom.net_wm_state_maximized_horz &&
1587 update_state(action, &window->maximized_horz))
1588 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001589
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001590 if (maximized != weston_wm_window_is_maximized(window)) {
1591 if (weston_wm_window_is_maximized(window)) {
1592 window->saved_width = window->width;
1593 window->saved_height = window->height;
1594
1595 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001596 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001597 } else if (window->shsurf) {
1598 weston_wm_window_set_toplevel(window);
1599 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001600 }
1601 }
1602}
1603
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001604static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001605surface_destroy(struct wl_listener *listener, void *data)
1606{
1607 struct weston_wm_window *window =
1608 container_of(listener,
1609 struct weston_wm_window, surface_destroy_listener);
1610
1611 wm_log("surface for xid %d destroyed\n", window->id);
1612
1613 /* This should have been freed by the shell.
1614 * Don't try to use it later. */
1615 window->shsurf = NULL;
1616 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001617}
1618
1619static void
1620weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1621 xcb_client_message_event_t *client_message)
1622{
1623 struct weston_wm *wm = window->wm;
1624 struct wl_resource *resource;
1625
1626 if (window->surface_id != 0) {
1627 wm_log("already have surface id for window %d\n", window->id);
1628 return;
1629 }
1630
1631 /* Xwayland will send the wayland requests to create the
1632 * wl_surface before sending this client message. Even so, we
1633 * can end up handling the X event before the wayland requests
1634 * and thus when we try to look up the surface ID, the surface
1635 * hasn't been created yet. In that case put the window on
1636 * the unpaired window list and continue when the surface gets
1637 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001638 uint32_t id = client_message->data.data32[0];
1639 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001640 if (resource) {
1641 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001642 xserver_map_shell_surface(window,
1643 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001644 }
1645 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001646 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001647 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001648 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001649}
1650
1651static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001652weston_wm_handle_client_message(struct weston_wm *wm,
1653 xcb_generic_event_t *event)
1654{
1655 xcb_client_message_event_t *client_message =
1656 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001657 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001658
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001659 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1660 get_atom_name(wm->conn, client_message->type),
1661 client_message->data.data32[0],
1662 client_message->data.data32[1],
1663 client_message->data.data32[2],
1664 client_message->data.data32[3],
1665 client_message->data.data32[4],
1666 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001667
Jason Ekstrand0250a742014-07-24 13:17:47 -07001668 /* The window may get created and destroyed before we actually
1669 * handle the message. If it doesn't exist, bail.
1670 */
Derek Foreman49372142015-04-09 10:51:22 -05001671 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001672 return;
1673
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001674 if (client_message->type == wm->atom.net_wm_moveresize)
1675 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001676 else if (client_message->type == wm->atom.net_wm_state)
1677 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001678 else if (client_message->type == wm->atom.wl_surface_id)
1679 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001680}
1681
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001682enum cursor_type {
1683 XWM_CURSOR_TOP,
1684 XWM_CURSOR_BOTTOM,
1685 XWM_CURSOR_LEFT,
1686 XWM_CURSOR_RIGHT,
1687 XWM_CURSOR_TOP_LEFT,
1688 XWM_CURSOR_TOP_RIGHT,
1689 XWM_CURSOR_BOTTOM_LEFT,
1690 XWM_CURSOR_BOTTOM_RIGHT,
1691 XWM_CURSOR_LEFT_PTR,
1692};
1693
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001694/*
1695 * The following correspondences between file names and cursors was copied
1696 * from: https://bugs.kde.org/attachment.cgi?id=67313
1697 */
1698
1699static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001700 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001701 "sw-resize",
1702 "size_bdiag"
1703};
1704
1705static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001706 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001707 "se-resize",
1708 "size_fdiag"
1709};
1710
1711static const char *bottom_sides[] = {
1712 "bottom_side",
1713 "s-resize",
1714 "size_ver"
1715};
1716
1717static const char *left_ptrs[] = {
1718 "left_ptr",
1719 "default",
1720 "top_left_arrow",
1721 "left-arrow"
1722};
1723
1724static const char *left_sides[] = {
1725 "left_side",
1726 "w-resize",
1727 "size_hor"
1728};
1729
1730static const char *right_sides[] = {
1731 "right_side",
1732 "e-resize",
1733 "size_hor"
1734};
1735
1736static const char *top_left_corners[] = {
1737 "top_left_corner",
1738 "nw-resize",
1739 "size_fdiag"
1740};
1741
1742static const char *top_right_corners[] = {
1743 "top_right_corner",
1744 "ne-resize",
1745 "size_bdiag"
1746};
1747
1748static const char *top_sides[] = {
1749 "top_side",
1750 "n-resize",
1751 "size_ver"
1752};
1753
1754struct cursor_alternatives {
1755 const char **names;
1756 size_t count;
1757};
1758
1759static const struct cursor_alternatives cursors[] = {
1760 {top_sides, ARRAY_LENGTH(top_sides)},
1761 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1762 {left_sides, ARRAY_LENGTH(left_sides)},
1763 {right_sides, ARRAY_LENGTH(right_sides)},
1764 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1765 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1766 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1767 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1768 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001769};
1770
1771static void
1772weston_wm_create_cursors(struct weston_wm *wm)
1773{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001774 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001775 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001776 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001777
1778 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1779 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001780 for (j = 0; j < cursors[i].count; j++) {
1781 name = cursors[i].names[j];
1782 wm->cursors[i] =
1783 xcb_cursor_library_load_cursor(wm, name);
1784 if (wm->cursors[i] != (xcb_cursor_t)-1)
1785 break;
1786 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001787 }
1788
1789 wm->last_cursor = -1;
1790}
1791
1792static void
1793weston_wm_destroy_cursors(struct weston_wm *wm)
1794{
1795 uint8_t i;
1796
1797 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1798 xcb_free_cursor(wm->conn, wm->cursors[i]);
1799
1800 free(wm->cursors);
1801}
1802
1803static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001804get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001805{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001806 switch (location) {
1807 case THEME_LOCATION_RESIZING_TOP:
1808 return XWM_CURSOR_TOP;
1809 case THEME_LOCATION_RESIZING_BOTTOM:
1810 return XWM_CURSOR_BOTTOM;
1811 case THEME_LOCATION_RESIZING_LEFT:
1812 return XWM_CURSOR_LEFT;
1813 case THEME_LOCATION_RESIZING_RIGHT:
1814 return XWM_CURSOR_RIGHT;
1815 case THEME_LOCATION_RESIZING_TOP_LEFT:
1816 return XWM_CURSOR_TOP_LEFT;
1817 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1818 return XWM_CURSOR_TOP_RIGHT;
1819 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1820 return XWM_CURSOR_BOTTOM_LEFT;
1821 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1822 return XWM_CURSOR_BOTTOM_RIGHT;
1823 case THEME_LOCATION_EXTERIOR:
1824 case THEME_LOCATION_TITLEBAR:
1825 default:
1826 return XWM_CURSOR_LEFT_PTR;
1827 }
1828}
1829
1830static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001831weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1832 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001833{
1834 uint32_t cursor_value_list;
1835
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001836 if (wm->last_cursor == cursor)
1837 return;
1838
1839 wm->last_cursor = cursor;
1840
1841 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001842 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001843 XCB_CW_CURSOR, &cursor_value_list);
1844 xcb_flush(wm->conn);
1845}
1846
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001847static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001848weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001849{
1850 xcb_client_message_event_t client_message;
1851
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001852 if (window->delete_window) {
1853 client_message.response_type = XCB_CLIENT_MESSAGE;
1854 client_message.format = 32;
1855 client_message.window = window->id;
1856 client_message.type = window->wm->atom.wm_protocols;
1857 client_message.data.data32[0] =
1858 window->wm->atom.wm_delete_window;
1859 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001860
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001861 xcb_send_event(window->wm->conn, 0, window->id,
1862 XCB_EVENT_MASK_NO_EVENT,
1863 (char *) &client_message);
1864 } else {
1865 xcb_kill_client(window->wm->conn, window->id);
1866 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001867}
1868
1869static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001870weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1871{
1872 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001873 const struct weston_desktop_xwayland_interface *xwayland_interface =
1874 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001875 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001876 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001877 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001878 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001879 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001880 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001881
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001882 wm_log("XCB_BUTTON_%s (detail %d)\n",
1883 button->response_type == XCB_BUTTON_PRESS ?
1884 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001885
Derek Foreman49372142015-04-09 10:51:22 -05001886 if (!wm_lookup_window(wm, button->event, &window) ||
1887 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001888 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001889
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001890 if (button->detail != 1 && button->detail != 2)
1891 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001892
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001893 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001894 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001895
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001896 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001897 WL_POINTER_BUTTON_STATE_PRESSED :
1898 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001899 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1900
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001901 /* Make sure we're looking at the right location. The frame
1902 * could have received a motion event from a pointer from a
1903 * different wl_seat, but under X it looks like our core
1904 * pointer moved. Move the frame pointer to the button press
1905 * location before deciding what to do. */
1906 location = frame_pointer_motion(window->frame, NULL,
1907 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001908 location = frame_pointer_button(window->frame, NULL,
1909 button_id, button_state);
1910 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1911 weston_wm_window_schedule_repaint(window);
1912
1913 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001914 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001915 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001916 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1917 }
1918
1919 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001920 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001921 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001922 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1923 }
1924
1925 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001926 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001927 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001928 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001929
1930 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1931 window->maximized_horz = !window->maximized_horz;
1932 window->maximized_vert = !window->maximized_vert;
1933 if (weston_wm_window_is_maximized(window)) {
1934 window->saved_width = window->width;
1935 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001936 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001937 } else {
1938 weston_wm_window_set_toplevel(window);
1939 }
1940 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1941 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001942}
1943
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001944static void
1945weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1946{
1947 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1948 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001949 enum theme_location location;
1950 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001951
Derek Foreman49372142015-04-09 10:51:22 -05001952 if (!wm_lookup_window(wm, motion->event, &window) ||
1953 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001954 return;
1955
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001956 location = frame_pointer_motion(window->frame, NULL,
1957 motion->event_x, motion->event_y);
1958 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1959 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001960
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001961 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001962 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001963}
1964
1965static void
1966weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1967{
1968 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1969 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001970 enum theme_location location;
1971 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001972
Derek Foreman49372142015-04-09 10:51:22 -05001973 if (!wm_lookup_window(wm, enter->event, &window) ||
1974 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001975 return;
1976
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001977 location = frame_pointer_enter(window->frame, NULL,
1978 enter->event_x, enter->event_y);
1979 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1980 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001981
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001982 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001983 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001984}
1985
1986static void
1987weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1988{
1989 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1990 struct weston_wm_window *window;
1991
Derek Foreman49372142015-04-09 10:51:22 -05001992 if (!wm_lookup_window(wm, leave->event, &window) ||
1993 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001994 return;
1995
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001996 frame_pointer_leave(window->frame, NULL);
1997 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1998 weston_wm_window_schedule_repaint(window);
1999
Tiago Vignattic1903232012-07-16 12:15:37 -04002000 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002001}
2002
Giulio Camuffob18f7882015-03-29 14:20:23 +03002003static void
2004weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
2005{
2006 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
2007 /* Do not let X clients change the focus behind the compositor's
2008 * back. Reset the focus to the old one if it changed. */
2009 if (!wm->focus_window || focus->event != wm->focus_window->id)
2010 weston_wm_send_focus_window(wm, wm->focus_window);
2011}
2012
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002013static int
2014weston_wm_handle_event(int fd, uint32_t mask, void *data)
2015{
2016 struct weston_wm *wm = data;
2017 xcb_generic_event_t *event;
2018 int count = 0;
2019
2020 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
2021 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002022 free(event);
2023 count++;
2024 continue;
2025 }
2026
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002027 if (weston_wm_handle_dnd_event(wm, event)) {
2028 free(event);
2029 count++;
2030 continue;
2031 }
2032
MoD31700122013-06-11 19:58:55 -05002033 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002034 case XCB_BUTTON_PRESS:
2035 case XCB_BUTTON_RELEASE:
2036 weston_wm_handle_button(wm, event);
2037 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002038 case XCB_ENTER_NOTIFY:
2039 weston_wm_handle_enter(wm, event);
2040 break;
2041 case XCB_LEAVE_NOTIFY:
2042 weston_wm_handle_leave(wm, event);
2043 break;
2044 case XCB_MOTION_NOTIFY:
2045 weston_wm_handle_motion(wm, event);
2046 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002047 case XCB_CREATE_NOTIFY:
2048 weston_wm_handle_create_notify(wm, event);
2049 break;
2050 case XCB_MAP_REQUEST:
2051 weston_wm_handle_map_request(wm, event);
2052 break;
2053 case XCB_MAP_NOTIFY:
2054 weston_wm_handle_map_notify(wm, event);
2055 break;
2056 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04002057 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002058 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04002059 case XCB_REPARENT_NOTIFY:
2060 weston_wm_handle_reparent_notify(wm, event);
2061 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002062 case XCB_CONFIGURE_REQUEST:
2063 weston_wm_handle_configure_request(wm, event);
2064 break;
2065 case XCB_CONFIGURE_NOTIFY:
2066 weston_wm_handle_configure_notify(wm, event);
2067 break;
2068 case XCB_DESTROY_NOTIFY:
2069 weston_wm_handle_destroy_notify(wm, event);
2070 break;
2071 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002072 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002073 break;
2074 case XCB_PROPERTY_NOTIFY:
2075 weston_wm_handle_property_notify(wm, event);
2076 break;
2077 case XCB_CLIENT_MESSAGE:
2078 weston_wm_handle_client_message(wm, event);
2079 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03002080 case XCB_FOCUS_IN:
2081 weston_wm_handle_focus_in(wm, event);
2082 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002083 }
2084
2085 free(event);
2086 count++;
2087 }
2088
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002089 if (count != 0)
2090 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002091
2092 return count;
2093}
2094
2095static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002096weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2097 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2098 wm->screen->root, wm->atom.net_active_window,
2099 wm->atom.window, 32, 1, &window);
2100}
2101
2102static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002103weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2104{
2105 xcb_depth_iterator_t d_iter;
2106 xcb_visualtype_iterator_t vt_iter;
2107 xcb_visualtype_t *visualtype;
2108
2109 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2110 visualtype = NULL;
2111 while (d_iter.rem > 0) {
2112 if (d_iter.data->depth == 32) {
2113 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2114 visualtype = vt_iter.data;
2115 break;
2116 }
2117
2118 xcb_depth_next(&d_iter);
2119 }
2120
2121 if (visualtype == NULL) {
2122 weston_log("no 32 bit visualtype\n");
2123 return;
2124 }
2125
2126 wm->visual_id = visualtype->visual_id;
2127 wm->colormap = xcb_generate_id(wm->conn);
2128 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2129 wm->colormap, wm->screen->root, wm->visual_id);
2130}
2131
2132static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002133weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002134{
2135
2136#define F(field) offsetof(struct weston_wm, field)
2137
2138 static const struct { const char *name; int offset; } atoms[] = {
2139 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002140 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002141 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2142 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002143 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002144 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002145 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002146 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002147 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002148 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002149 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2150 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002151 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2152 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002153 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2154 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2155 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002156 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002157 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2158
2159 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2160 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2161 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2162 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2163 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2164 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2165 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002166 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2167 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002168 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2169 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2170 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2171 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2172 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2173
2174 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2175 { "_NET_SUPPORTING_WM_CHECK",
2176 F(atom.net_supporting_wm_check) },
2177 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002178 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002179 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2180 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002181 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002182 { "TARGETS", F(atom.targets) },
2183 { "UTF8_STRING", F(atom.utf8_string) },
2184 { "_WL_SELECTION", F(atom.wl_selection) },
2185 { "INCR", F(atom.incr) },
2186 { "TIMESTAMP", F(atom.timestamp) },
2187 { "MULTIPLE", F(atom.multiple) },
2188 { "UTF8_STRING" , F(atom.utf8_string) },
2189 { "COMPOUND_TEXT", F(atom.compound_text) },
2190 { "TEXT", F(atom.text) },
2191 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002192 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002193 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2194 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002195 { "XdndSelection", F(atom.xdnd_selection) },
2196 { "XdndAware", F(atom.xdnd_aware) },
2197 { "XdndEnter", F(atom.xdnd_enter) },
2198 { "XdndLeave", F(atom.xdnd_leave) },
2199 { "XdndDrop", F(atom.xdnd_drop) },
2200 { "XdndStatus", F(atom.xdnd_status) },
2201 { "XdndFinished", F(atom.xdnd_finished) },
2202 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002203 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2204 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002205 };
2206#undef F
2207
2208 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2209 xcb_xfixes_query_version_reply_t *xfixes_reply;
2210 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2211 xcb_intern_atom_reply_t *reply;
2212 xcb_render_query_pict_formats_reply_t *formats_reply;
2213 xcb_render_query_pict_formats_cookie_t formats_cookie;
2214 xcb_render_pictforminfo_t *formats;
2215 uint32_t i;
2216
2217 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002218 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219
2220 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2221
2222 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2223 cookies[i] = xcb_intern_atom (wm->conn, 0,
2224 strlen(atoms[i].name),
2225 atoms[i].name);
2226
2227 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2228 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2229 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2230 free(reply);
2231 }
2232
2233 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2234 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002235 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002236
2237 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2238 XCB_XFIXES_MAJOR_VERSION,
2239 XCB_XFIXES_MINOR_VERSION);
2240 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2241 xfixes_cookie, NULL);
2242
Martin Minarik6d118362012-06-07 18:01:59 +02002243 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002244 xfixes_reply->major_version, xfixes_reply->minor_version);
2245
2246 free(xfixes_reply);
2247
2248 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2249 formats_cookie, 0);
2250 if (formats_reply == NULL)
2251 return;
2252
2253 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002254 for (i = 0; i < formats_reply->num_formats; i++) {
2255 if (formats[i].direct.red_mask != 0xff &&
2256 formats[i].direct.red_shift != 16)
2257 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002258 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2259 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002260 wm->format_rgb = formats[i];
2261 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2262 formats[i].depth == 32 &&
2263 formats[i].direct.alpha_mask == 0xff &&
2264 formats[i].direct.alpha_shift == 24)
2265 wm->format_rgba = formats[i];
2266 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002267
2268 free(formats_reply);
2269}
2270
2271static void
2272weston_wm_create_wm_window(struct weston_wm *wm)
2273{
2274 static const char name[] = "Weston WM";
2275
2276 wm->wm_window = xcb_generate_id(wm->conn);
2277 xcb_create_window(wm->conn,
2278 XCB_COPY_FROM_PARENT,
2279 wm->wm_window,
2280 wm->screen->root,
2281 0, 0,
2282 10, 10,
2283 0,
2284 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2285 wm->screen->root_visual,
2286 0, NULL);
2287
2288 xcb_change_property(wm->conn,
2289 XCB_PROP_MODE_REPLACE,
2290 wm->wm_window,
2291 wm->atom.net_supporting_wm_check,
2292 XCB_ATOM_WINDOW,
2293 32, /* format */
2294 1, &wm->wm_window);
2295
2296 xcb_change_property(wm->conn,
2297 XCB_PROP_MODE_REPLACE,
2298 wm->wm_window,
2299 wm->atom.net_wm_name,
2300 wm->atom.utf8_string,
2301 8, /* format */
2302 strlen(name), name);
2303
2304 xcb_change_property(wm->conn,
2305 XCB_PROP_MODE_REPLACE,
2306 wm->screen->root,
2307 wm->atom.net_supporting_wm_check,
2308 XCB_ATOM_WINDOW,
2309 32, /* format */
2310 1, &wm->wm_window);
2311
Abdur Rehmanb8b150b2017-01-01 19:46:46 +05002312 /* Claim the WM_S0 selection even though we don't support
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002313 * the --replace functionality. */
2314 xcb_set_selection_owner(wm->conn,
2315 wm->wm_window,
2316 wm->atom.wm_s0,
2317 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002318
2319 xcb_set_selection_owner(wm->conn,
2320 wm->wm_window,
2321 wm->atom.net_wm_cm_s0,
2322 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002323}
2324
2325struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002326weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002327{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002328 struct weston_wm *wm;
2329 struct wl_event_loop *loop;
2330 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002331 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002332 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002333
Peter Huttererf3d62272013-08-08 11:57:05 +10002334 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002335 if (wm == NULL)
2336 return NULL;
2337
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002338 wm->server = wxs;
2339 wm->window_hash = hash_table_create();
2340 if (wm->window_hash == NULL) {
2341 free(wm);
2342 return NULL;
2343 }
2344
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002345 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002346 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002347 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002348 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002349 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002350 hash_table_destroy(wm->window_hash);
2351 free(wm);
2352 return NULL;
2353 }
2354
2355 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2356 wm->screen = s.data;
2357
2358 loop = wl_display_get_event_loop(wxs->wl_display);
2359 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002360 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002361 WL_EVENT_READABLE,
2362 weston_wm_handle_event, wm);
2363 wl_event_source_check(wm->source);
2364
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002365 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002366 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002367
2368 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002369 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2370 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2371 XCB_EVENT_MASK_PROPERTY_CHANGE;
2372 xcb_change_window_attributes(wm->conn, wm->screen->root,
2373 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002374
2375 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2376 XCB_COMPOSITE_REDIRECT_MANUAL);
2377
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002378 wm->theme = theme_create();
2379
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002380 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002381 supported[1] = wm->atom.net_wm_state;
2382 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002383 supported[3] = wm->atom.net_wm_state_maximized_vert;
2384 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002385 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002386 xcb_change_property(wm->conn,
2387 XCB_PROP_MODE_REPLACE,
2388 wm->screen->root,
2389 wm->atom.net_supported,
2390 XCB_ATOM_ATOM,
2391 32, /* format */
2392 ARRAY_LENGTH(supported), supported);
2393
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002394 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2395
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002396 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002397
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002398 weston_wm_dnd_init(wm);
2399
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002400 xcb_flush(wm->conn);
2401
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002402 wm->create_surface_listener.notify = weston_wm_create_surface;
2403 wl_signal_add(&wxs->compositor->create_surface_signal,
2404 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002405 wm->activate_listener.notify = weston_wm_window_activate;
2406 wl_signal_add(&wxs->compositor->activate_signal,
2407 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002408 wm->kill_listener.notify = weston_wm_kill_client;
2409 wl_signal_add(&wxs->compositor->kill_signal,
2410 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002411 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002412
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002413 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002414 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002415
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002416 /* Create wm window and take WM_S0 selection last, which
2417 * signals to Xwayland that we're done with setup. */
2418 weston_wm_create_wm_window(wm);
2419
2420 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002421
2422 return wm;
2423}
2424
2425void
2426weston_wm_destroy(struct weston_wm *wm)
2427{
2428 /* FIXME: Free windows in hash. */
2429 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002430 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002431 xcb_disconnect(wm->conn);
2432 wl_event_source_remove(wm->source);
2433 wl_list_remove(&wm->selection_listener.link);
2434 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002435 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002436 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002437
2438 free(wm);
2439}
2440
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002441static struct weston_wm_window *
2442get_wm_window(struct weston_surface *surface)
2443{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002444 struct wl_listener *listener;
2445
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002446 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002447 if (listener)
2448 return container_of(listener, struct weston_wm_window,
2449 surface_destroy_listener);
2450
2451 return NULL;
2452}
2453
Quentin Glidic955cec02016-08-12 10:41:35 +02002454static bool
2455is_wm_window(struct weston_surface *surface)
2456{
2457 return get_wm_window(surface) != NULL;
2458}
2459
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002460static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002461weston_wm_window_configure(void *data)
2462{
2463 struct weston_wm_window *window = data;
2464 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002465 uint32_t values[4];
2466 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002467
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002468 weston_wm_window_get_child_position(window, &x, &y);
2469 values[0] = x;
2470 values[1] = y;
2471 values[2] = window->width;
2472 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002473 xcb_configure_window(wm->conn,
2474 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002475 XCB_CONFIG_WINDOW_X |
2476 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002477 XCB_CONFIG_WINDOW_WIDTH |
2478 XCB_CONFIG_WINDOW_HEIGHT,
2479 values);
2480
2481 weston_wm_window_get_frame_size(window, &width, &height);
2482 values[0] = width;
2483 values[1] = height;
2484 xcb_configure_window(wm->conn,
2485 window->frame_id,
2486 XCB_CONFIG_WINDOW_WIDTH |
2487 XCB_CONFIG_WINDOW_HEIGHT,
2488 values);
2489
2490 window->configure_source = NULL;
2491
2492 weston_wm_window_schedule_repaint(window);
2493}
2494
2495static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002496send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002497{
2498 struct weston_wm_window *window = get_wm_window(surface);
2499 struct weston_wm *wm = window->wm;
2500 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002501 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002502
Marek Chalupae9fe4672014-10-29 13:44:44 +01002503 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002504 hborder = 2 * t->width;
2505 vborder = t->titlebar_height + t->width;
2506 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002507 hborder = 0;
2508 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002509 }
2510
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002511 if (width > hborder)
2512 window->width = width - hborder;
2513 else
2514 window->width = 1;
2515
2516 if (height > vborder)
2517 window->height = height - vborder;
2518 else
2519 window->height = 1;
2520
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002521 if (window->frame)
2522 frame_resize_inside(window->frame, window->width, window->height);
2523
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002524 if (window->configure_source)
2525 return;
2526
2527 window->configure_source =
2528 wl_event_loop_add_idle(wm->server->loop,
2529 weston_wm_window_configure, window);
2530}
2531
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002532static void
2533send_position(struct weston_surface *surface, int32_t x, int32_t y)
2534{
2535 struct weston_wm_window *window = get_wm_window(surface);
2536 struct weston_wm *wm;
2537 uint32_t mask, values[2];
2538
2539 if (!window || !window->wm)
2540 return;
2541
2542 wm = window->wm;
2543 /* We use pos_dirty to tell whether a configure message is in flight.
2544 * This is needed in case we send two configure events in a very
2545 * short time, since window->x/y is set in after a roundtrip, hence
2546 * we cannot just check if the current x and y are different. */
2547 if (window->x != x || window->y != y || window->pos_dirty) {
2548 window->pos_dirty = true;
2549 values[0] = x;
2550 values[1] = y;
2551 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2552
2553 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2554 xcb_flush(wm->conn);
2555 }
2556}
2557
Quentin Glidic955cec02016-08-12 10:41:35 +02002558static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002559 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002560};
2561
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002562static int
2563legacy_fullscreen(struct weston_wm *wm,
2564 struct weston_wm_window *window,
2565 struct weston_output **output_ret)
2566{
2567 struct weston_compositor *compositor = wm->server->compositor;
2568 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002569 uint32_t minmax = PMinSize | PMaxSize;
2570 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002571
2572 /* Heuristics for detecting legacy fullscreen windows... */
2573
2574 wl_list_for_each(output, &compositor->output_list, link) {
2575 if (output->x == window->x &&
2576 output->y == window->y &&
2577 output->width == window->width &&
2578 output->height == window->height &&
2579 window->override_redirect) {
2580 *output_ret = output;
2581 return 1;
2582 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002583
2584 matching_size = 0;
2585 if ((window->size_hints.flags & (USSize |PSize)) &&
2586 window->size_hints.width == output->width &&
2587 window->size_hints.height == output->height)
2588 matching_size = 1;
2589 if ((window->size_hints.flags & minmax) == minmax &&
2590 window->size_hints.min_width == output->width &&
2591 window->size_hints.min_height == output->height &&
2592 window->size_hints.max_width == output->width &&
2593 window->size_hints.max_height == output->height)
2594 matching_size = 1;
2595
2596 if (matching_size && !window->decorate &&
2597 (window->size_hints.flags & (USPosition | PPosition)) &&
2598 window->size_hints.x == output->x &&
2599 window->size_hints.y == output->y) {
2600 *output_ret = output;
2601 return 1;
2602 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002603 }
2604
2605 return 0;
2606}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002607
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002608static bool
Pekka Paalanen882aff02016-11-16 14:15:18 +02002609weston_wm_window_is_positioned(struct weston_wm_window *window)
2610{
2611 if (window->map_request_x == INT_MIN ||
2612 window->map_request_y == INT_MIN)
2613 weston_log("XWM warning: win %d did not see map request\n",
2614 window->id);
2615
2616 return window->map_request_x != 0 || window->map_request_y != 0;
2617}
2618
2619static bool
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002620weston_wm_window_type_inactive(struct weston_wm_window *window)
2621{
2622 struct weston_wm *wm = window->wm;
2623
2624 return window->type == wm->atom.net_wm_window_type_tooltip ||
2625 window->type == wm->atom.net_wm_window_type_dropdown ||
2626 window->type == wm->atom.net_wm_window_type_dnd ||
2627 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002628 window->type == wm->atom.net_wm_window_type_popup ||
2629 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002630}
2631
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002632static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002633xserver_map_shell_surface(struct weston_wm_window *window,
2634 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002635{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002636 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002637 struct weston_desktop_xwayland *xwayland =
2638 wm->server->compositor->xwayland;
2639 const struct weston_desktop_xwayland_interface *xwayland_interface =
2640 wm->server->compositor->xwayland_interface;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002641 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002642
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002643 weston_wm_window_read_properties(window);
2644
2645 /* A weston_wm_window may have many different surfaces assigned
2646 * throughout its life, so we must make sure to remove the listener
2647 * from the old surface signal list. */
2648 if (window->surface)
2649 wl_list_remove(&window->surface_destroy_listener.link);
2650
2651 window->surface = surface;
2652 window->surface_destroy_listener.notify = surface_destroy;
2653 wl_signal_add(&window->surface->destroy_signal,
2654 &window->surface_destroy_listener);
2655
2656 weston_wm_window_schedule_repaint(window);
2657
Quentin Glidic955cec02016-08-12 10:41:35 +02002658 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002659 return;
2660
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002661 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002662 weston_log("warning, unexpected in %s: "
2663 "surface's configure hook is already set.\n",
2664 __func__);
2665 return;
2666 }
2667
Murray Calavera883ac022015-06-06 13:02:22 +00002668 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002669 xwayland_interface->create_surface(xwayland,
2670 window->surface,
2671 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002672
Pekka Paalanena04eacc2016-11-28 16:42:25 +02002673 wm_log("XWM: map shell surface, win %d, xwayland surface %p\n",
2674 window->id, window->shsurf);
2675
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002676 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002677 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002678 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002679 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002680
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002681 if (window->fullscreen) {
2682 window->saved_width = window->width;
2683 window->saved_height = window->height;
Pekka Paalanen505237e2016-12-01 15:41:11 +02002684 xwayland_interface->set_fullscreen(window->shsurf,
2685 window->legacy_fullscreen_output.output);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002686 return;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002687 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002688 xwayland_interface->set_xwayland(window->shsurf,
2689 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002690 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002691 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002692 if (weston_wm_window_type_inactive(window)) {
2693 xwayland_interface->set_transient(window->shsurf,
2694 parent->surface,
2695 window->x - parent->x,
2696 window->y - parent->y);
2697 } else {
2698 xwayland_interface->set_toplevel(window->shsurf);
2699 xwayland_interface->set_parent(window->shsurf,
2700 parent->surface);
2701 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002702 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002703 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002704 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002705 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002706 xwayland_interface->set_xwayland(window->shsurf,
2707 window->x,
2708 window->y);
Pekka Paalanen882aff02016-11-16 14:15:18 +02002709 } else if (weston_wm_window_is_positioned(window)) {
2710 xwayland_interface->set_toplevel_with_position(window->shsurf,
2711 window->map_request_x,
2712 window->map_request_y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002713 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002714 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002715 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002716 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002717}
Quentin Glidic955cec02016-08-12 10:41:35 +02002718
2719const struct weston_xwayland_surface_api surface_api = {
2720 is_wm_window,
2721 send_position,
2722};