blob: ea28e71f36167d0e3cd463bc01d24bef651a80f3 [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
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001122weston_wm_window_draw_decoration(void *data)
1123{
1124 struct weston_wm_window *window = data;
1125 struct weston_wm *wm = window->wm;
1126 struct theme *t = wm->theme;
1127 cairo_t *cr;
1128 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001129 int32_t input_x, input_y, input_w, input_h;
Quentin Glidic955cec02016-08-12 10:41:35 +02001130 const struct weston_desktop_xwayland_interface *xwayland_interface =
1131 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001132
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001133 wm_log("XWM: start draw decoration, win %d\n", window->id);
1134
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001135 weston_wm_window_read_properties(window);
1136
1137 window->repaint_source = NULL;
1138
1139 weston_wm_window_get_frame_size(window, &width, &height);
1140 weston_wm_window_get_child_position(window, &x, &y);
1141
1142 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1143 cr = cairo_create(window->cairo_surface);
1144
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001145 if (window->fullscreen) {
1146 /* nothing */
1147 } else if (window->decorate) {
Pekka Paalanen9a330e12016-12-15 15:37:24 +02001148 frame_set_title(window->frame, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001149 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001150 } else {
1151 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1152 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1153 cairo_paint(cr);
1154
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001155 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001156 }
1157
1158 cairo_destroy(cr);
Pekka Paalanendb7b9f32017-01-16 17:01:11 +02001159 cairo_surface_flush(window->cairo_surface);
1160 xcb_flush(window->wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001161
1162 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001163 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001164 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001165 pixman_region32_init(&window->surface->pending.opaque);
1166 } else {
1167 /* We leave an extra pixel around the X window area to
1168 * make sure we don't sample from the undefined alpha
1169 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001170 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001171 x - 1, y - 1,
1172 window->width + 2,
1173 window->height + 2);
1174 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001175
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001176 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001177
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001178 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001179 frame_input_rect(window->frame, &input_x, &input_y,
1180 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001181 } else {
1182 input_x = x;
1183 input_y = y;
1184 input_w = width;
1185 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001186 }
1187
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001188 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001189 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001190
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001191 wm_log("XWM: draw decoration, win %d geometry: %d,%d %dx%d\n",
1192 window->id, input_x, input_y, input_w, input_h);
1193
Quentin Glidic955cec02016-08-12 10:41:35 +02001194 xwayland_interface->set_window_geometry(window->shsurf,
1195 input_x, input_y, input_w, input_h);
Pekka Paalanenc9ca2c02016-12-15 16:04:14 +02001196 if (window->name)
1197 xwayland_interface->set_title(window->shsurf, window->name);
1198 if (window->pid > 0)
1199 xwayland_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001200 }
1201}
1202
1203static void
1204weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1205{
1206 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001207 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001208
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001209 if (window->frame_id == XCB_WINDOW_NONE) {
1210 if (window->surface != NULL) {
Pekka Paalanen73428a82016-12-19 15:10:01 +02001211 /* Override-redirect windows go through here, but we
1212 * cannot assert(window->override_redirect); because
1213 * we do not deal with changing OR flag yet.
1214 * XXX: handle OR flag changes in message handlers
1215 */
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001216 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001217 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001218 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001219 pixman_region32_init(&window->surface->pending.opaque);
1220 } else {
1221 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1222 width, height);
1223 }
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001224 }
1225 return;
1226 }
1227
1228 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001229 return;
1230
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001231 wm_log("XWM: schedule repaint, win %d\n", window->id);
1232
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001233 window->repaint_source =
1234 wl_event_loop_add_idle(wm->server->loop,
1235 weston_wm_window_draw_decoration,
1236 window);
1237}
1238
1239static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001240weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1241{
1242 xcb_property_notify_event_t *property_notify =
1243 (xcb_property_notify_event_t *) event;
1244 struct weston_wm_window *window;
1245
Derek Foreman49372142015-04-09 10:51:22 -05001246 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001247 return;
1248
1249 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001250
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001251 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001252 if (property_notify->state == XCB_PROPERTY_DELETE)
Pekka Paalanen8cc153b2016-12-19 15:18:45 +02001253 wm_log_continue("deleted %s\n",
1254 get_atom_name(wm->conn, property_notify->atom));
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001255 else
1256 read_and_dump_property(wm, property_notify->window,
1257 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001258
1259 if (property_notify->atom == wm->atom.net_wm_name ||
1260 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001261 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001262}
1263
1264static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001265weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001266 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001267{
1268 struct weston_wm_window *window;
1269 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001270 xcb_get_geometry_cookie_t geometry_cookie;
1271 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001272
Peter Huttererf3d62272013-08-08 11:57:05 +10001273 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001274 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001275 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001276 return;
1277 }
1278
MoD384a11a2013-06-22 11:04:21 -05001279 geometry_cookie = xcb_get_geometry(wm->conn, id);
1280
Giulio Camuffob18f7882015-03-29 14:20:23 +03001281 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1282 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001283 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1284
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001285 window->wm = wm;
1286 window->id = id;
1287 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001288 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001289 window->width = width;
1290 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001291 window->x = x;
1292 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001293 window->pos_dirty = false;
Pekka Paalanen882aff02016-11-16 14:15:18 +02001294 window->map_request_x = INT_MIN; /* out of range for valid positions */
1295 window->map_request_y = INT_MIN; /* out of range for valid positions */
Pekka Paalanen505237e2016-12-01 15:41:11 +02001296 weston_output_weak_ref_init(&window->legacy_fullscreen_output);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001297
MoD384a11a2013-06-22 11:04:21 -05001298 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1299 /* technically we should use XRender and check the visual format's
1300 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001301 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001302 window->has_alpha = geometry_reply->depth == 32;
1303 free(geometry_reply);
1304
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001305 hash_table_insert(wm->window_hash, id, window);
1306}
1307
1308static void
1309weston_wm_window_destroy(struct weston_wm_window *window)
1310{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001311 struct weston_wm *wm = window->wm;
1312
Pekka Paalanen505237e2016-12-01 15:41:11 +02001313 weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1314
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001315 if (window->repaint_source)
1316 wl_event_source_remove(window->repaint_source);
1317 if (window->cairo_surface)
1318 cairo_surface_destroy(window->cairo_surface);
1319
1320 if (window->frame_id) {
1321 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1322 xcb_destroy_window(wm->conn, window->frame_id);
1323 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001324 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001325 hash_table_remove(wm->window_hash, window->frame_id);
1326 window->frame_id = XCB_WINDOW_NONE;
1327 }
1328
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001329 if (window->surface_id)
1330 wl_list_remove(&window->link);
1331
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001332 if (window->surface)
1333 wl_list_remove(&window->surface_destroy_listener.link);
1334
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001335 hash_table_remove(window->wm->window_hash, window->id);
1336 free(window);
1337}
1338
1339static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001340weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1341{
1342 xcb_create_notify_event_t *create_notify =
1343 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001344
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001345 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 -04001346 create_notify->window,
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001347 create_notify->x, create_notify->y,
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001348 create_notify->width, create_notify->height,
1349 create_notify->override_redirect ? ", override" : "",
1350 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001351
1352 if (our_resource(wm, create_notify->window))
1353 return;
1354
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001355 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001356 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001357 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001358 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001359}
1360
1361static void
1362weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1363{
1364 xcb_destroy_notify_event_t *destroy_notify =
1365 (xcb_destroy_notify_event_t *) event;
1366 struct weston_wm_window *window;
1367
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001368 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1369 destroy_notify->window,
1370 destroy_notify->event,
1371 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001372
1373 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001374 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001375
Derek Foreman49372142015-04-09 10:51:22 -05001376 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1377 return;
1378
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001379 weston_wm_window_destroy(window);
1380}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001381
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001382static void
1383weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1384{
1385 xcb_reparent_notify_event_t *reparent_notify =
1386 (xcb_reparent_notify_event_t *) event;
1387 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001388
Pekka Paalanen73428a82016-12-19 15:10:01 +02001389 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d%s)\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001390 reparent_notify->window,
1391 reparent_notify->parent,
Pekka Paalanen73428a82016-12-19 15:10:01 +02001392 reparent_notify->event,
1393 reparent_notify->override_redirect ? ", override" : "");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001394
1395 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001396 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001397 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001398 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001399 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001400 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1401 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001402 weston_wm_window_destroy(window);
1403 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001404}
1405
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001406struct weston_seat *
1407weston_wm_pick_seat(struct weston_wm *wm)
1408{
Tom Hochsteine7fff212016-11-01 14:14:00 -05001409 struct wl_list *seats = wm->server->compositor->seat_list.next;
1410 if (wl_list_empty(seats))
1411 return NULL;
1412 return container_of(seats, struct weston_seat, link);
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001413}
1414
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001415static struct weston_seat *
1416weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1417{
1418 struct weston_wm *wm = window->wm;
1419 struct weston_seat *seat, *s;
1420
1421 seat = NULL;
1422 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001423 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1424 struct weston_pointer *old_pointer =
1425 weston_seat_get_pointer(seat);
1426
1427 if (pointer && pointer->focus &&
1428 pointer->focus->surface == window->surface &&
1429 pointer->button_count > 0 &&
1430 (!seat ||
1431 pointer->grab_serial -
1432 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001433 seat = s;
1434 }
1435
1436 return seat;
1437}
1438
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001439static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001440weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1441 xcb_client_message_event_t *client_message)
1442{
1443 static const int map[] = {
1444 THEME_LOCATION_RESIZING_TOP_LEFT,
1445 THEME_LOCATION_RESIZING_TOP,
1446 THEME_LOCATION_RESIZING_TOP_RIGHT,
1447 THEME_LOCATION_RESIZING_RIGHT,
1448 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1449 THEME_LOCATION_RESIZING_BOTTOM,
1450 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1451 THEME_LOCATION_RESIZING_LEFT
1452 };
1453
1454 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001455 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001456 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001457 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001458 const struct weston_desktop_xwayland_interface *xwayland_interface =
1459 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001460
Derek Foreman1281a362015-07-31 16:55:32 -05001461 if (!pointer || pointer->button_count != 1
1462 || !pointer->focus
1463 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001464 return;
1465
1466 detail = client_message->data.data32[2];
1467 switch (detail) {
1468 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001469 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001470 break;
1471 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1472 case _NET_WM_MOVERESIZE_SIZE_TOP:
1473 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1474 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1475 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1476 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1477 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1478 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001479 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001480 break;
1481 case _NET_WM_MOVERESIZE_CANCEL:
1482 break;
1483 }
1484}
1485
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001486#define _NET_WM_STATE_REMOVE 0
1487#define _NET_WM_STATE_ADD 1
1488#define _NET_WM_STATE_TOGGLE 2
1489
1490static int
1491update_state(int action, int *state)
1492{
1493 int new_state, changed;
1494
1495 switch (action) {
1496 case _NET_WM_STATE_REMOVE:
1497 new_state = 0;
1498 break;
1499 case _NET_WM_STATE_ADD:
1500 new_state = 1;
1501 break;
1502 case _NET_WM_STATE_TOGGLE:
1503 new_state = !*state;
1504 break;
1505 default:
1506 return 0;
1507 }
1508
1509 changed = (*state != new_state);
1510 *state = new_state;
1511
1512 return changed;
1513}
1514
1515static void
1516weston_wm_window_configure(void *data);
1517
1518static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001519weston_wm_window_set_toplevel(struct weston_wm_window *window)
1520{
Quentin Glidic955cec02016-08-12 10:41:35 +02001521 const struct weston_desktop_xwayland_interface *xwayland_interface =
1522 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001523
Quentin Glidic955cec02016-08-12 10:41:35 +02001524 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001525 window->width = window->saved_width;
1526 window->height = window->saved_height;
1527 if (window->frame)
1528 frame_resize_inside(window->frame,
1529 window->width,
1530 window->height);
1531 weston_wm_window_configure(window);
1532}
1533
1534static inline bool
1535weston_wm_window_is_maximized(struct weston_wm_window *window)
1536{
1537 return window->maximized_horz && window->maximized_vert;
1538}
1539
1540static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001541weston_wm_window_handle_state(struct weston_wm_window *window,
1542 xcb_client_message_event_t *client_message)
1543{
1544 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001545 const struct weston_desktop_xwayland_interface *xwayland_interface =
1546 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001547 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001548 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001549
1550 action = client_message->data.data32[0];
1551 property = client_message->data.data32[1];
1552
1553 if (property == wm->atom.net_wm_state_fullscreen &&
1554 update_state(action, &window->fullscreen)) {
1555 weston_wm_window_set_net_wm_state(window);
1556 if (window->fullscreen) {
1557 window->saved_width = window->width;
1558 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001559
1560 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001561 xwayland_interface->set_fullscreen(window->shsurf,
1562 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001563 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001564 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001565 weston_wm_window_set_toplevel(window);
1566 }
1567 } else {
1568 if (property == wm->atom.net_wm_state_maximized_vert &&
1569 update_state(action, &window->maximized_vert))
1570 weston_wm_window_set_net_wm_state(window);
1571 if (property == wm->atom.net_wm_state_maximized_horz &&
1572 update_state(action, &window->maximized_horz))
1573 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001574
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001575 if (maximized != weston_wm_window_is_maximized(window)) {
1576 if (weston_wm_window_is_maximized(window)) {
1577 window->saved_width = window->width;
1578 window->saved_height = window->height;
1579
1580 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001581 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001582 } else if (window->shsurf) {
1583 weston_wm_window_set_toplevel(window);
1584 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001585 }
1586 }
1587}
1588
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001589static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001590surface_destroy(struct wl_listener *listener, void *data)
1591{
1592 struct weston_wm_window *window =
1593 container_of(listener,
1594 struct weston_wm_window, surface_destroy_listener);
1595
1596 wm_log("surface for xid %d destroyed\n", window->id);
1597
1598 /* This should have been freed by the shell.
1599 * Don't try to use it later. */
1600 window->shsurf = NULL;
1601 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001602}
1603
1604static void
1605weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1606 xcb_client_message_event_t *client_message)
1607{
1608 struct weston_wm *wm = window->wm;
1609 struct wl_resource *resource;
1610
1611 if (window->surface_id != 0) {
1612 wm_log("already have surface id for window %d\n", window->id);
1613 return;
1614 }
1615
1616 /* Xwayland will send the wayland requests to create the
1617 * wl_surface before sending this client message. Even so, we
1618 * can end up handling the X event before the wayland requests
1619 * and thus when we try to look up the surface ID, the surface
1620 * hasn't been created yet. In that case put the window on
1621 * the unpaired window list and continue when the surface gets
1622 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001623 uint32_t id = client_message->data.data32[0];
1624 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001625 if (resource) {
1626 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001627 xserver_map_shell_surface(window,
1628 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001629 }
1630 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001631 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001632 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001633 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001634}
1635
1636static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001637weston_wm_handle_client_message(struct weston_wm *wm,
1638 xcb_generic_event_t *event)
1639{
1640 xcb_client_message_event_t *client_message =
1641 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001642 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001643
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001644 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1645 get_atom_name(wm->conn, client_message->type),
1646 client_message->data.data32[0],
1647 client_message->data.data32[1],
1648 client_message->data.data32[2],
1649 client_message->data.data32[3],
1650 client_message->data.data32[4],
1651 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001652
Jason Ekstrand0250a742014-07-24 13:17:47 -07001653 /* The window may get created and destroyed before we actually
1654 * handle the message. If it doesn't exist, bail.
1655 */
Derek Foreman49372142015-04-09 10:51:22 -05001656 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001657 return;
1658
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001659 if (client_message->type == wm->atom.net_wm_moveresize)
1660 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001661 else if (client_message->type == wm->atom.net_wm_state)
1662 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001663 else if (client_message->type == wm->atom.wl_surface_id)
1664 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001665}
1666
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001667enum cursor_type {
1668 XWM_CURSOR_TOP,
1669 XWM_CURSOR_BOTTOM,
1670 XWM_CURSOR_LEFT,
1671 XWM_CURSOR_RIGHT,
1672 XWM_CURSOR_TOP_LEFT,
1673 XWM_CURSOR_TOP_RIGHT,
1674 XWM_CURSOR_BOTTOM_LEFT,
1675 XWM_CURSOR_BOTTOM_RIGHT,
1676 XWM_CURSOR_LEFT_PTR,
1677};
1678
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001679/*
1680 * The following correspondences between file names and cursors was copied
1681 * from: https://bugs.kde.org/attachment.cgi?id=67313
1682 */
1683
1684static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001685 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001686 "sw-resize",
1687 "size_bdiag"
1688};
1689
1690static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001691 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001692 "se-resize",
1693 "size_fdiag"
1694};
1695
1696static const char *bottom_sides[] = {
1697 "bottom_side",
1698 "s-resize",
1699 "size_ver"
1700};
1701
1702static const char *left_ptrs[] = {
1703 "left_ptr",
1704 "default",
1705 "top_left_arrow",
1706 "left-arrow"
1707};
1708
1709static const char *left_sides[] = {
1710 "left_side",
1711 "w-resize",
1712 "size_hor"
1713};
1714
1715static const char *right_sides[] = {
1716 "right_side",
1717 "e-resize",
1718 "size_hor"
1719};
1720
1721static const char *top_left_corners[] = {
1722 "top_left_corner",
1723 "nw-resize",
1724 "size_fdiag"
1725};
1726
1727static const char *top_right_corners[] = {
1728 "top_right_corner",
1729 "ne-resize",
1730 "size_bdiag"
1731};
1732
1733static const char *top_sides[] = {
1734 "top_side",
1735 "n-resize",
1736 "size_ver"
1737};
1738
1739struct cursor_alternatives {
1740 const char **names;
1741 size_t count;
1742};
1743
1744static const struct cursor_alternatives cursors[] = {
1745 {top_sides, ARRAY_LENGTH(top_sides)},
1746 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1747 {left_sides, ARRAY_LENGTH(left_sides)},
1748 {right_sides, ARRAY_LENGTH(right_sides)},
1749 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1750 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1751 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1752 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1753 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001754};
1755
1756static void
1757weston_wm_create_cursors(struct weston_wm *wm)
1758{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001759 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001760 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001761 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001762
1763 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1764 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001765 for (j = 0; j < cursors[i].count; j++) {
1766 name = cursors[i].names[j];
1767 wm->cursors[i] =
1768 xcb_cursor_library_load_cursor(wm, name);
1769 if (wm->cursors[i] != (xcb_cursor_t)-1)
1770 break;
1771 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001772 }
1773
1774 wm->last_cursor = -1;
1775}
1776
1777static void
1778weston_wm_destroy_cursors(struct weston_wm *wm)
1779{
1780 uint8_t i;
1781
1782 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1783 xcb_free_cursor(wm->conn, wm->cursors[i]);
1784
1785 free(wm->cursors);
1786}
1787
1788static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001789get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001790{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001791 switch (location) {
1792 case THEME_LOCATION_RESIZING_TOP:
1793 return XWM_CURSOR_TOP;
1794 case THEME_LOCATION_RESIZING_BOTTOM:
1795 return XWM_CURSOR_BOTTOM;
1796 case THEME_LOCATION_RESIZING_LEFT:
1797 return XWM_CURSOR_LEFT;
1798 case THEME_LOCATION_RESIZING_RIGHT:
1799 return XWM_CURSOR_RIGHT;
1800 case THEME_LOCATION_RESIZING_TOP_LEFT:
1801 return XWM_CURSOR_TOP_LEFT;
1802 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1803 return XWM_CURSOR_TOP_RIGHT;
1804 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1805 return XWM_CURSOR_BOTTOM_LEFT;
1806 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1807 return XWM_CURSOR_BOTTOM_RIGHT;
1808 case THEME_LOCATION_EXTERIOR:
1809 case THEME_LOCATION_TITLEBAR:
1810 default:
1811 return XWM_CURSOR_LEFT_PTR;
1812 }
1813}
1814
1815static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001816weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1817 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001818{
1819 uint32_t cursor_value_list;
1820
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001821 if (wm->last_cursor == cursor)
1822 return;
1823
1824 wm->last_cursor = cursor;
1825
1826 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001827 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001828 XCB_CW_CURSOR, &cursor_value_list);
1829 xcb_flush(wm->conn);
1830}
1831
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001832static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001833weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001834{
1835 xcb_client_message_event_t client_message;
1836
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001837 if (window->delete_window) {
1838 client_message.response_type = XCB_CLIENT_MESSAGE;
1839 client_message.format = 32;
1840 client_message.window = window->id;
1841 client_message.type = window->wm->atom.wm_protocols;
1842 client_message.data.data32[0] =
1843 window->wm->atom.wm_delete_window;
1844 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001845
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001846 xcb_send_event(window->wm->conn, 0, window->id,
1847 XCB_EVENT_MASK_NO_EVENT,
1848 (char *) &client_message);
1849 } else {
1850 xcb_kill_client(window->wm->conn, window->id);
1851 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001852}
1853
1854static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001855weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1856{
1857 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001858 const struct weston_desktop_xwayland_interface *xwayland_interface =
1859 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001860 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001861 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001862 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001863 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001864 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001865 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001866
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001867 wm_log("XCB_BUTTON_%s (detail %d)\n",
1868 button->response_type == XCB_BUTTON_PRESS ?
1869 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001870
Derek Foreman49372142015-04-09 10:51:22 -05001871 if (!wm_lookup_window(wm, button->event, &window) ||
1872 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001873 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001874
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001875 if (button->detail != 1 && button->detail != 2)
1876 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001877
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001878 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001879 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001880
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001881 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001882 WL_POINTER_BUTTON_STATE_PRESSED :
1883 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001884 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1885
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001886 /* Make sure we're looking at the right location. The frame
1887 * could have received a motion event from a pointer from a
1888 * different wl_seat, but under X it looks like our core
1889 * pointer moved. Move the frame pointer to the button press
1890 * location before deciding what to do. */
1891 location = frame_pointer_motion(window->frame, NULL,
1892 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001893 location = frame_pointer_button(window->frame, NULL,
1894 button_id, button_state);
1895 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1896 weston_wm_window_schedule_repaint(window);
1897
1898 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001899 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001900 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001901 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1902 }
1903
1904 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001905 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001906 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001907 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1908 }
1909
1910 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001911 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001912 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001913 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001914
1915 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1916 window->maximized_horz = !window->maximized_horz;
1917 window->maximized_vert = !window->maximized_vert;
1918 if (weston_wm_window_is_maximized(window)) {
1919 window->saved_width = window->width;
1920 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001921 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001922 } else {
1923 weston_wm_window_set_toplevel(window);
1924 }
1925 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1926 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001927}
1928
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001929static void
1930weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1931{
1932 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1933 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001934 enum theme_location location;
1935 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001936
Derek Foreman49372142015-04-09 10:51:22 -05001937 if (!wm_lookup_window(wm, motion->event, &window) ||
1938 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001939 return;
1940
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001941 location = frame_pointer_motion(window->frame, NULL,
1942 motion->event_x, motion->event_y);
1943 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1944 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001945
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001946 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001947 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001948}
1949
1950static void
1951weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1952{
1953 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1954 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001955 enum theme_location location;
1956 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001957
Derek Foreman49372142015-04-09 10:51:22 -05001958 if (!wm_lookup_window(wm, enter->event, &window) ||
1959 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001960 return;
1961
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001962 location = frame_pointer_enter(window->frame, NULL,
1963 enter->event_x, enter->event_y);
1964 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1965 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001966
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001967 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001968 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001969}
1970
1971static void
1972weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1973{
1974 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1975 struct weston_wm_window *window;
1976
Derek Foreman49372142015-04-09 10:51:22 -05001977 if (!wm_lookup_window(wm, leave->event, &window) ||
1978 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001979 return;
1980
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001981 frame_pointer_leave(window->frame, NULL);
1982 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1983 weston_wm_window_schedule_repaint(window);
1984
Tiago Vignattic1903232012-07-16 12:15:37 -04001985 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001986}
1987
Giulio Camuffob18f7882015-03-29 14:20:23 +03001988static void
1989weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1990{
1991 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1992 /* Do not let X clients change the focus behind the compositor's
1993 * back. Reset the focus to the old one if it changed. */
1994 if (!wm->focus_window || focus->event != wm->focus_window->id)
1995 weston_wm_send_focus_window(wm, wm->focus_window);
1996}
1997
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001998static int
1999weston_wm_handle_event(int fd, uint32_t mask, void *data)
2000{
2001 struct weston_wm *wm = data;
2002 xcb_generic_event_t *event;
2003 int count = 0;
2004
2005 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
2006 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002007 free(event);
2008 count++;
2009 continue;
2010 }
2011
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002012 if (weston_wm_handle_dnd_event(wm, event)) {
2013 free(event);
2014 count++;
2015 continue;
2016 }
2017
MoD31700122013-06-11 19:58:55 -05002018 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002019 case XCB_BUTTON_PRESS:
2020 case XCB_BUTTON_RELEASE:
2021 weston_wm_handle_button(wm, event);
2022 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002023 case XCB_ENTER_NOTIFY:
2024 weston_wm_handle_enter(wm, event);
2025 break;
2026 case XCB_LEAVE_NOTIFY:
2027 weston_wm_handle_leave(wm, event);
2028 break;
2029 case XCB_MOTION_NOTIFY:
2030 weston_wm_handle_motion(wm, event);
2031 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002032 case XCB_CREATE_NOTIFY:
2033 weston_wm_handle_create_notify(wm, event);
2034 break;
2035 case XCB_MAP_REQUEST:
2036 weston_wm_handle_map_request(wm, event);
2037 break;
2038 case XCB_MAP_NOTIFY:
2039 weston_wm_handle_map_notify(wm, event);
2040 break;
2041 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04002042 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002043 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04002044 case XCB_REPARENT_NOTIFY:
2045 weston_wm_handle_reparent_notify(wm, event);
2046 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002047 case XCB_CONFIGURE_REQUEST:
2048 weston_wm_handle_configure_request(wm, event);
2049 break;
2050 case XCB_CONFIGURE_NOTIFY:
2051 weston_wm_handle_configure_notify(wm, event);
2052 break;
2053 case XCB_DESTROY_NOTIFY:
2054 weston_wm_handle_destroy_notify(wm, event);
2055 break;
2056 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002057 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002058 break;
2059 case XCB_PROPERTY_NOTIFY:
2060 weston_wm_handle_property_notify(wm, event);
2061 break;
2062 case XCB_CLIENT_MESSAGE:
2063 weston_wm_handle_client_message(wm, event);
2064 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03002065 case XCB_FOCUS_IN:
2066 weston_wm_handle_focus_in(wm, event);
2067 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002068 }
2069
2070 free(event);
2071 count++;
2072 }
2073
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002074 if (count != 0)
2075 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002076
2077 return count;
2078}
2079
2080static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002081weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2082 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2083 wm->screen->root, wm->atom.net_active_window,
2084 wm->atom.window, 32, 1, &window);
2085}
2086
2087static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002088weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2089{
2090 xcb_depth_iterator_t d_iter;
2091 xcb_visualtype_iterator_t vt_iter;
2092 xcb_visualtype_t *visualtype;
2093
2094 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2095 visualtype = NULL;
2096 while (d_iter.rem > 0) {
2097 if (d_iter.data->depth == 32) {
2098 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2099 visualtype = vt_iter.data;
2100 break;
2101 }
2102
2103 xcb_depth_next(&d_iter);
2104 }
2105
2106 if (visualtype == NULL) {
2107 weston_log("no 32 bit visualtype\n");
2108 return;
2109 }
2110
2111 wm->visual_id = visualtype->visual_id;
2112 wm->colormap = xcb_generate_id(wm->conn);
2113 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2114 wm->colormap, wm->screen->root, wm->visual_id);
2115}
2116
2117static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002118weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002119{
2120
2121#define F(field) offsetof(struct weston_wm, field)
2122
2123 static const struct { const char *name; int offset; } atoms[] = {
2124 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002125 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002126 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2127 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002128 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002129 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002130 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002131 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002132 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002133 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002134 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2135 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002136 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2137 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002138 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2139 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2140 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002141 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002142 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2143
2144 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2145 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2146 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2147 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2148 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2149 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2150 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002151 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2152 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002153 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2154 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2155 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2156 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2157 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2158
2159 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2160 { "_NET_SUPPORTING_WM_CHECK",
2161 F(atom.net_supporting_wm_check) },
2162 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002163 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002164 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2165 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002166 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002167 { "TARGETS", F(atom.targets) },
2168 { "UTF8_STRING", F(atom.utf8_string) },
2169 { "_WL_SELECTION", F(atom.wl_selection) },
2170 { "INCR", F(atom.incr) },
2171 { "TIMESTAMP", F(atom.timestamp) },
2172 { "MULTIPLE", F(atom.multiple) },
2173 { "UTF8_STRING" , F(atom.utf8_string) },
2174 { "COMPOUND_TEXT", F(atom.compound_text) },
2175 { "TEXT", F(atom.text) },
2176 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002177 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002178 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2179 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002180 { "XdndSelection", F(atom.xdnd_selection) },
2181 { "XdndAware", F(atom.xdnd_aware) },
2182 { "XdndEnter", F(atom.xdnd_enter) },
2183 { "XdndLeave", F(atom.xdnd_leave) },
2184 { "XdndDrop", F(atom.xdnd_drop) },
2185 { "XdndStatus", F(atom.xdnd_status) },
2186 { "XdndFinished", F(atom.xdnd_finished) },
2187 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002188 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2189 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002190 };
2191#undef F
2192
2193 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2194 xcb_xfixes_query_version_reply_t *xfixes_reply;
2195 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2196 xcb_intern_atom_reply_t *reply;
2197 xcb_render_query_pict_formats_reply_t *formats_reply;
2198 xcb_render_query_pict_formats_cookie_t formats_cookie;
2199 xcb_render_pictforminfo_t *formats;
2200 uint32_t i;
2201
2202 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002203 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002204
2205 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2206
2207 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2208 cookies[i] = xcb_intern_atom (wm->conn, 0,
2209 strlen(atoms[i].name),
2210 atoms[i].name);
2211
2212 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2213 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2214 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2215 free(reply);
2216 }
2217
2218 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2219 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002220 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002221
2222 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2223 XCB_XFIXES_MAJOR_VERSION,
2224 XCB_XFIXES_MINOR_VERSION);
2225 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2226 xfixes_cookie, NULL);
2227
Martin Minarik6d118362012-06-07 18:01:59 +02002228 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002229 xfixes_reply->major_version, xfixes_reply->minor_version);
2230
2231 free(xfixes_reply);
2232
2233 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2234 formats_cookie, 0);
2235 if (formats_reply == NULL)
2236 return;
2237
2238 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002239 for (i = 0; i < formats_reply->num_formats; i++) {
2240 if (formats[i].direct.red_mask != 0xff &&
2241 formats[i].direct.red_shift != 16)
2242 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002243 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2244 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002245 wm->format_rgb = formats[i];
2246 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2247 formats[i].depth == 32 &&
2248 formats[i].direct.alpha_mask == 0xff &&
2249 formats[i].direct.alpha_shift == 24)
2250 wm->format_rgba = formats[i];
2251 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002252
2253 free(formats_reply);
2254}
2255
2256static void
2257weston_wm_create_wm_window(struct weston_wm *wm)
2258{
2259 static const char name[] = "Weston WM";
2260
2261 wm->wm_window = xcb_generate_id(wm->conn);
2262 xcb_create_window(wm->conn,
2263 XCB_COPY_FROM_PARENT,
2264 wm->wm_window,
2265 wm->screen->root,
2266 0, 0,
2267 10, 10,
2268 0,
2269 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2270 wm->screen->root_visual,
2271 0, NULL);
2272
2273 xcb_change_property(wm->conn,
2274 XCB_PROP_MODE_REPLACE,
2275 wm->wm_window,
2276 wm->atom.net_supporting_wm_check,
2277 XCB_ATOM_WINDOW,
2278 32, /* format */
2279 1, &wm->wm_window);
2280
2281 xcb_change_property(wm->conn,
2282 XCB_PROP_MODE_REPLACE,
2283 wm->wm_window,
2284 wm->atom.net_wm_name,
2285 wm->atom.utf8_string,
2286 8, /* format */
2287 strlen(name), name);
2288
2289 xcb_change_property(wm->conn,
2290 XCB_PROP_MODE_REPLACE,
2291 wm->screen->root,
2292 wm->atom.net_supporting_wm_check,
2293 XCB_ATOM_WINDOW,
2294 32, /* format */
2295 1, &wm->wm_window);
2296
Abdur Rehmanb8b150b2017-01-01 19:46:46 +05002297 /* Claim the WM_S0 selection even though we don't support
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002298 * the --replace functionality. */
2299 xcb_set_selection_owner(wm->conn,
2300 wm->wm_window,
2301 wm->atom.wm_s0,
2302 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002303
2304 xcb_set_selection_owner(wm->conn,
2305 wm->wm_window,
2306 wm->atom.net_wm_cm_s0,
2307 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002308}
2309
2310struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002311weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002312{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002313 struct weston_wm *wm;
2314 struct wl_event_loop *loop;
2315 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002316 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002317 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002318
Peter Huttererf3d62272013-08-08 11:57:05 +10002319 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002320 if (wm == NULL)
2321 return NULL;
2322
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002323 wm->server = wxs;
2324 wm->window_hash = hash_table_create();
2325 if (wm->window_hash == NULL) {
2326 free(wm);
2327 return NULL;
2328 }
2329
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002330 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002331 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002332 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002333 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002334 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002335 hash_table_destroy(wm->window_hash);
2336 free(wm);
2337 return NULL;
2338 }
2339
2340 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2341 wm->screen = s.data;
2342
2343 loop = wl_display_get_event_loop(wxs->wl_display);
2344 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002345 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002346 WL_EVENT_READABLE,
2347 weston_wm_handle_event, wm);
2348 wl_event_source_check(wm->source);
2349
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002350 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002351 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002352
2353 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002354 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2355 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2356 XCB_EVENT_MASK_PROPERTY_CHANGE;
2357 xcb_change_window_attributes(wm->conn, wm->screen->root,
2358 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002359
2360 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2361 XCB_COMPOSITE_REDIRECT_MANUAL);
2362
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002363 wm->theme = theme_create();
2364
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002365 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002366 supported[1] = wm->atom.net_wm_state;
2367 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002368 supported[3] = wm->atom.net_wm_state_maximized_vert;
2369 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002370 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002371 xcb_change_property(wm->conn,
2372 XCB_PROP_MODE_REPLACE,
2373 wm->screen->root,
2374 wm->atom.net_supported,
2375 XCB_ATOM_ATOM,
2376 32, /* format */
2377 ARRAY_LENGTH(supported), supported);
2378
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002379 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2380
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002381 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002382
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002383 weston_wm_dnd_init(wm);
2384
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002385 xcb_flush(wm->conn);
2386
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002387 wm->create_surface_listener.notify = weston_wm_create_surface;
2388 wl_signal_add(&wxs->compositor->create_surface_signal,
2389 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002390 wm->activate_listener.notify = weston_wm_window_activate;
2391 wl_signal_add(&wxs->compositor->activate_signal,
2392 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002393 wm->kill_listener.notify = weston_wm_kill_client;
2394 wl_signal_add(&wxs->compositor->kill_signal,
2395 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002396 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002397
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002398 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002399 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002400
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002401 /* Create wm window and take WM_S0 selection last, which
2402 * signals to Xwayland that we're done with setup. */
2403 weston_wm_create_wm_window(wm);
2404
2405 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002406
2407 return wm;
2408}
2409
2410void
2411weston_wm_destroy(struct weston_wm *wm)
2412{
2413 /* FIXME: Free windows in hash. */
2414 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002415 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002416 xcb_disconnect(wm->conn);
2417 wl_event_source_remove(wm->source);
2418 wl_list_remove(&wm->selection_listener.link);
2419 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002420 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002421 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002422
2423 free(wm);
2424}
2425
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002426static struct weston_wm_window *
2427get_wm_window(struct weston_surface *surface)
2428{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002429 struct wl_listener *listener;
2430
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002431 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002432 if (listener)
2433 return container_of(listener, struct weston_wm_window,
2434 surface_destroy_listener);
2435
2436 return NULL;
2437}
2438
Quentin Glidic955cec02016-08-12 10:41:35 +02002439static bool
2440is_wm_window(struct weston_surface *surface)
2441{
2442 return get_wm_window(surface) != NULL;
2443}
2444
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002445static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002446weston_wm_window_configure(void *data)
2447{
2448 struct weston_wm_window *window = data;
2449 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002450 uint32_t values[4];
2451 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002452
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002453 weston_wm_window_get_child_position(window, &x, &y);
2454 values[0] = x;
2455 values[1] = y;
2456 values[2] = window->width;
2457 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002458 xcb_configure_window(wm->conn,
2459 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002460 XCB_CONFIG_WINDOW_X |
2461 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002462 XCB_CONFIG_WINDOW_WIDTH |
2463 XCB_CONFIG_WINDOW_HEIGHT,
2464 values);
2465
2466 weston_wm_window_get_frame_size(window, &width, &height);
2467 values[0] = width;
2468 values[1] = height;
2469 xcb_configure_window(wm->conn,
2470 window->frame_id,
2471 XCB_CONFIG_WINDOW_WIDTH |
2472 XCB_CONFIG_WINDOW_HEIGHT,
2473 values);
2474
2475 window->configure_source = NULL;
2476
2477 weston_wm_window_schedule_repaint(window);
2478}
2479
2480static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002481send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002482{
2483 struct weston_wm_window *window = get_wm_window(surface);
2484 struct weston_wm *wm = window->wm;
2485 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002486 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002487
Marek Chalupae9fe4672014-10-29 13:44:44 +01002488 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002489 hborder = 2 * t->width;
2490 vborder = t->titlebar_height + t->width;
2491 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002492 hborder = 0;
2493 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002494 }
2495
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002496 if (width > hborder)
2497 window->width = width - hborder;
2498 else
2499 window->width = 1;
2500
2501 if (height > vborder)
2502 window->height = height - vborder;
2503 else
2504 window->height = 1;
2505
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002506 if (window->frame)
2507 frame_resize_inside(window->frame, window->width, window->height);
2508
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002509 if (window->configure_source)
2510 return;
2511
2512 window->configure_source =
2513 wl_event_loop_add_idle(wm->server->loop,
2514 weston_wm_window_configure, window);
2515}
2516
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002517static void
2518send_position(struct weston_surface *surface, int32_t x, int32_t y)
2519{
2520 struct weston_wm_window *window = get_wm_window(surface);
2521 struct weston_wm *wm;
2522 uint32_t mask, values[2];
2523
2524 if (!window || !window->wm)
2525 return;
2526
2527 wm = window->wm;
2528 /* We use pos_dirty to tell whether a configure message is in flight.
2529 * This is needed in case we send two configure events in a very
2530 * short time, since window->x/y is set in after a roundtrip, hence
2531 * we cannot just check if the current x and y are different. */
2532 if (window->x != x || window->y != y || window->pos_dirty) {
2533 window->pos_dirty = true;
2534 values[0] = x;
2535 values[1] = y;
2536 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2537
2538 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2539 xcb_flush(wm->conn);
2540 }
2541}
2542
Quentin Glidic955cec02016-08-12 10:41:35 +02002543static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002544 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002545};
2546
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002547static int
2548legacy_fullscreen(struct weston_wm *wm,
2549 struct weston_wm_window *window,
2550 struct weston_output **output_ret)
2551{
2552 struct weston_compositor *compositor = wm->server->compositor;
2553 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002554 uint32_t minmax = PMinSize | PMaxSize;
2555 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002556
2557 /* Heuristics for detecting legacy fullscreen windows... */
2558
2559 wl_list_for_each(output, &compositor->output_list, link) {
2560 if (output->x == window->x &&
2561 output->y == window->y &&
2562 output->width == window->width &&
2563 output->height == window->height &&
2564 window->override_redirect) {
2565 *output_ret = output;
2566 return 1;
2567 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002568
2569 matching_size = 0;
2570 if ((window->size_hints.flags & (USSize |PSize)) &&
2571 window->size_hints.width == output->width &&
2572 window->size_hints.height == output->height)
2573 matching_size = 1;
2574 if ((window->size_hints.flags & minmax) == minmax &&
2575 window->size_hints.min_width == output->width &&
2576 window->size_hints.min_height == output->height &&
2577 window->size_hints.max_width == output->width &&
2578 window->size_hints.max_height == output->height)
2579 matching_size = 1;
2580
2581 if (matching_size && !window->decorate &&
2582 (window->size_hints.flags & (USPosition | PPosition)) &&
2583 window->size_hints.x == output->x &&
2584 window->size_hints.y == output->y) {
2585 *output_ret = output;
2586 return 1;
2587 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002588 }
2589
2590 return 0;
2591}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002592
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002593static bool
Pekka Paalanen882aff02016-11-16 14:15:18 +02002594weston_wm_window_is_positioned(struct weston_wm_window *window)
2595{
2596 if (window->map_request_x == INT_MIN ||
2597 window->map_request_y == INT_MIN)
2598 weston_log("XWM warning: win %d did not see map request\n",
2599 window->id);
2600
2601 return window->map_request_x != 0 || window->map_request_y != 0;
2602}
2603
2604static bool
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002605weston_wm_window_type_inactive(struct weston_wm_window *window)
2606{
2607 struct weston_wm *wm = window->wm;
2608
2609 return window->type == wm->atom.net_wm_window_type_tooltip ||
2610 window->type == wm->atom.net_wm_window_type_dropdown ||
2611 window->type == wm->atom.net_wm_window_type_dnd ||
2612 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002613 window->type == wm->atom.net_wm_window_type_popup ||
2614 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002615}
2616
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002617static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002618xserver_map_shell_surface(struct weston_wm_window *window,
2619 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002620{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002621 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002622 struct weston_desktop_xwayland *xwayland =
2623 wm->server->compositor->xwayland;
2624 const struct weston_desktop_xwayland_interface *xwayland_interface =
2625 wm->server->compositor->xwayland_interface;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002626 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002627
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002628 weston_wm_window_read_properties(window);
2629
2630 /* A weston_wm_window may have many different surfaces assigned
2631 * throughout its life, so we must make sure to remove the listener
2632 * from the old surface signal list. */
2633 if (window->surface)
2634 wl_list_remove(&window->surface_destroy_listener.link);
2635
2636 window->surface = surface;
2637 window->surface_destroy_listener.notify = surface_destroy;
2638 wl_signal_add(&window->surface->destroy_signal,
2639 &window->surface_destroy_listener);
2640
2641 weston_wm_window_schedule_repaint(window);
2642
Quentin Glidic955cec02016-08-12 10:41:35 +02002643 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002644 return;
2645
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002646 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002647 weston_log("warning, unexpected in %s: "
2648 "surface's configure hook is already set.\n",
2649 __func__);
2650 return;
2651 }
2652
Murray Calavera883ac022015-06-06 13:02:22 +00002653 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002654 xwayland_interface->create_surface(xwayland,
2655 window->surface,
2656 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002657
Pekka Paalanena04eacc2016-11-28 16:42:25 +02002658 wm_log("XWM: map shell surface, win %d, xwayland surface %p\n",
2659 window->id, window->shsurf);
2660
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002661 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002662 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002663 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002664 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002665
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002666 if (window->fullscreen) {
2667 window->saved_width = window->width;
2668 window->saved_height = window->height;
Pekka Paalanen505237e2016-12-01 15:41:11 +02002669 xwayland_interface->set_fullscreen(window->shsurf,
2670 window->legacy_fullscreen_output.output);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002671 return;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002672 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002673 xwayland_interface->set_xwayland(window->shsurf,
2674 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002675 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002676 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002677 if (weston_wm_window_type_inactive(window)) {
2678 xwayland_interface->set_transient(window->shsurf,
2679 parent->surface,
2680 window->x - parent->x,
2681 window->y - parent->y);
2682 } else {
2683 xwayland_interface->set_toplevel(window->shsurf);
2684 xwayland_interface->set_parent(window->shsurf,
2685 parent->surface);
2686 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002687 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002688 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002689 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002690 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002691 xwayland_interface->set_xwayland(window->shsurf,
2692 window->x,
2693 window->y);
Pekka Paalanen882aff02016-11-16 14:15:18 +02002694 } else if (weston_wm_window_is_positioned(window)) {
2695 xwayland_interface->set_toplevel_with_position(window->shsurf,
2696 window->map_request_x,
2697 window->map_request_y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002698 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002699 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002700 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002701 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002702}
Quentin Glidic955cec02016-08-12 10:41:35 +02002703
2704const struct weston_xwayland_surface_api surface_api = {
2705 is_wm_window,
2706 send_position,
2707};