blob: a2ce7821f800eb45307e30939549405862317778 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg380deee2012-05-21 17:12:41 -040011 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040027
28#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040030#include <stdio.h>
31#include <string.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <unistd.h>
37#include <signal.h>
Pekka Paalanen882aff02016-11-16 14:15:18 +020038#include <limits.h>
Pekka Paalanen505237e2016-12-01 15:41:11 +020039#include <assert.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040040#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050041#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040042
Daniel Stone67fe3db2016-10-31 14:51:18 +000043#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040044#include "xwayland.h"
Quentin Glidic955cec02016-08-12 10:41:35 +020045#include "xwayland-internal-interface.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040046
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080047#include "cairo-util.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040048#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070049#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040050
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070051struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070052 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070053 int32_t x, y;
54 int32_t width, height; /* should set so old wm's don't mess up */
55 int32_t min_width, min_height;
56 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070057 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070058 struct {
59 int32_t x;
60 int32_t y;
61 } min_aspect, max_aspect;
62 int32_t base_width, base_height;
63 int32_t win_gravity;
64};
65
66#define USPosition (1L << 0)
67#define USSize (1L << 1)
68#define PPosition (1L << 2)
69#define PSize (1L << 3)
70#define PMinSize (1L << 4)
71#define PMaxSize (1L << 5)
72#define PResizeInc (1L << 6)
73#define PAspect (1L << 7)
74#define PBaseSize (1L << 8)
75#define PWinGravity (1L << 9)
76
Kristian Høgsberg380deee2012-05-21 17:12:41 -040077struct motif_wm_hints {
78 uint32_t flags;
79 uint32_t functions;
80 uint32_t decorations;
81 int32_t input_mode;
82 uint32_t status;
83};
84
85#define MWM_HINTS_FUNCTIONS (1L << 0)
86#define MWM_HINTS_DECORATIONS (1L << 1)
87#define MWM_HINTS_INPUT_MODE (1L << 2)
88#define MWM_HINTS_STATUS (1L << 3)
89
90#define MWM_FUNC_ALL (1L << 0)
91#define MWM_FUNC_RESIZE (1L << 1)
92#define MWM_FUNC_MOVE (1L << 2)
93#define MWM_FUNC_MINIMIZE (1L << 3)
94#define MWM_FUNC_MAXIMIZE (1L << 4)
95#define MWM_FUNC_CLOSE (1L << 5)
96
97#define MWM_DECOR_ALL (1L << 0)
98#define MWM_DECOR_BORDER (1L << 1)
99#define MWM_DECOR_RESIZEH (1L << 2)
100#define MWM_DECOR_TITLE (1L << 3)
101#define MWM_DECOR_MENU (1L << 4)
102#define MWM_DECOR_MINIMIZE (1L << 5)
103#define MWM_DECOR_MAXIMIZE (1L << 6)
104
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700105#define MWM_DECOR_EVERYTHING \
106 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
107 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
108
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400109#define MWM_INPUT_MODELESS 0
110#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
111#define MWM_INPUT_SYSTEM_MODAL 2
112#define MWM_INPUT_FULL_APPLICATION_MODAL 3
113#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
114
115#define MWM_TEAROFF_WINDOW (1L<<0)
116
117#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
118#define _NET_WM_MOVERESIZE_SIZE_TOP 1
119#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
120#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
121#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
122#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
123#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
124#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
125#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
126#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
127#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
128#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
129
Pekka Paalanen505237e2016-12-01 15:41:11 +0200130struct weston_output_weak_ref {
131 struct weston_output *output;
132 struct wl_listener destroy_listener;
133};
134
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400135struct weston_wm_window {
136 struct weston_wm *wm;
137 xcb_window_t id;
138 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500139 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400140 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700141 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 struct weston_surface *surface;
Quentin Glidic955cec02016-08-12 10:41:35 +0200143 struct weston_desktop_xwayland_surface *shsurf;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400144 struct wl_listener surface_destroy_listener;
145 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400146 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400147 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300148 int pid;
149 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400150 char *class;
151 char *name;
152 struct weston_wm_window *transient_for;
153 uint32_t protocols;
154 xcb_atom_t type;
155 int width, height;
Pekka Paalanen882aff02016-11-16 14:15:18 +0200156 int x;
157 int y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200158 bool pos_dirty;
Pekka Paalanen882aff02016-11-16 14:15:18 +0200159 int map_request_x;
160 int map_request_y;
Pekka Paalanen505237e2016-12-01 15:41:11 +0200161 struct weston_output_weak_ref legacy_fullscreen_output;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500162 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400163 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300164 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500165 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500166 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700167 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200168 int maximized_vert;
169 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700170 struct wm_size_hints size_hints;
171 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700172 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400173};
174
175static struct weston_wm_window *
176get_wm_window(struct weston_surface *surface);
177
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400178static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200179weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
180
181static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400182weston_wm_window_schedule_repaint(struct weston_wm_window *window);
183
Pekka Paalanen505237e2016-12-01 15:41:11 +0200184static int
185legacy_fullscreen(struct weston_wm *wm,
186 struct weston_wm_window *window,
187 struct weston_output **output_ret);
188
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700189static void
190xserver_map_shell_surface(struct weston_wm_window *window,
191 struct weston_surface *surface);
192
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400193static int __attribute__ ((format (printf, 1, 2)))
194wm_log(const char *fmt, ...)
195{
196#ifdef WM_DEBUG
197 int l;
198 va_list argp;
199
200 va_start(argp, fmt);
201 l = weston_vlog(fmt, argp);
202 va_end(argp);
203
204 return l;
205#else
206 return 0;
207#endif
208}
209
210static int __attribute__ ((format (printf, 1, 2)))
211wm_log_continue(const char *fmt, ...)
212{
213#ifdef WM_DEBUG
214 int l;
215 va_list argp;
216
217 va_start(argp, fmt);
218 l = weston_vlog_continue(fmt, argp);
219 va_end(argp);
220
221 return l;
222#else
223 return 0;
224#endif
225}
226
Pekka Paalanen505237e2016-12-01 15:41:11 +0200227static void
228weston_output_weak_ref_init(struct weston_output_weak_ref *ref)
229{
230 ref->output = NULL;
231}
232
233static void
234weston_output_weak_ref_clear(struct weston_output_weak_ref *ref)
235{
236 if (!ref->output)
237 return;
238
239 wl_list_remove(&ref->destroy_listener.link);
240 ref->output = NULL;
241}
242
243static void
244weston_output_weak_ref_handle_destroy(struct wl_listener *listener, void *data)
245{
246 struct weston_output_weak_ref *ref;
247
248 ref = wl_container_of(listener, ref, destroy_listener);
249 assert(ref->output == data);
250
251 weston_output_weak_ref_clear(ref);
252}
253
254static void
255weston_output_weak_ref_set(struct weston_output_weak_ref *ref,
256 struct weston_output *output)
257{
258 weston_output_weak_ref_clear(ref);
259
260 if (!output)
261 return;
262
263 ref->destroy_listener.notify = weston_output_weak_ref_handle_destroy;
264 wl_signal_add(&output->destroy_signal, &ref->destroy_listener);
265 ref->output = output;
266}
267
Derek Foreman49372142015-04-09 10:51:22 -0500268static bool __attribute__ ((warn_unused_result))
269wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
270 struct weston_wm_window **window)
271{
272 *window = hash_table_lookup(wm->window_hash, hash);
273 if (*window)
274 return true;
275 return false;
276}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400277
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400278const char *
279get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
280{
281 xcb_get_atom_name_cookie_t cookie;
282 xcb_get_atom_name_reply_t *reply;
283 xcb_generic_error_t *e;
284 static char buffer[64];
285
286 if (atom == XCB_ATOM_NONE)
287 return "None";
288
289 cookie = xcb_get_atom_name (c, atom);
290 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500291
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300292 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500293 snprintf(buffer, sizeof buffer, "%.*s",
294 xcb_get_atom_name_name_length (reply),
295 xcb_get_atom_name_name (reply));
296 } else {
297 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
298 }
299
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400300 free(reply);
301
302 return buffer;
303}
304
Tiago Vignatti90fada42012-07-16 12:02:08 -0400305static xcb_cursor_t
306xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
307{
308 xcb_connection_t *c = wm->conn;
309 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
310 xcb_screen_t *screen = s.data;
311 xcb_gcontext_t gc;
312 xcb_pixmap_t pix;
313 xcb_render_picture_t pic;
314 xcb_cursor_t cursor;
315 int stride = img->width * 4;
316
317 pix = xcb_generate_id(c);
318 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
319
320 pic = xcb_generate_id(c);
321 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
322
323 gc = xcb_generate_id(c);
324 xcb_create_gc(c, gc, pix, 0, 0);
325
326 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
327 img->width, img->height, 0, 0, 0, 32,
328 stride * img->height, (uint8_t *) img->pixels);
329 xcb_free_gc(c, gc);
330
331 cursor = xcb_generate_id(c);
332 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
333
334 xcb_render_free_picture(c, pic);
335 xcb_free_pixmap(c, pix);
336
337 return cursor;
338}
339
340static xcb_cursor_t
341xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
342{
343 /* TODO: treat animated cursors as well */
344 if (images->nimage != 1)
345 return -1;
346
347 return xcb_cursor_image_load_cursor(wm, images->images[0]);
348}
349
350static xcb_cursor_t
351xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
352{
353 xcb_cursor_t cursor;
354 XcursorImages *images;
355 char *v = NULL;
356 int size = 0;
357
358 if (!file)
359 return 0;
360
361 v = getenv ("XCURSOR_SIZE");
362 if (v)
363 size = atoi(v);
364
365 if (!size)
366 size = 32;
367
368 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300369 if (!images)
370 return -1;
371
Tiago Vignatti90fada42012-07-16 12:02:08 -0400372 cursor = xcb_cursor_images_load_cursor (wm, images);
373 XcursorImagesDestroy (images);
374
375 return cursor;
376}
377
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400378void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400379dump_property(struct weston_wm *wm,
380 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381{
382 int32_t *incr_value;
383 const char *text_value, *name;
384 xcb_atom_t *atom_value;
385 int width, len;
386 uint32_t i;
387
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400388 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400389 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400390 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400391 return;
392 }
393
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400394 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
395 get_atom_name(wm->conn, reply->type),
396 reply->format,
397 xcb_get_property_value_length(reply),
398 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399
400 if (reply->type == wm->atom.incr) {
401 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400402 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400403 } else if (reply->type == wm->atom.utf8_string ||
404 reply->type == wm->atom.string) {
405 text_value = xcb_get_property_value(reply);
406 if (reply->value_len > 40)
407 len = 40;
408 else
409 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400410 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 } else if (reply->type == XCB_ATOM_ATOM) {
412 atom_value = xcb_get_property_value(reply);
413 for (i = 0; i < reply->value_len; i++) {
414 name = get_atom_name(wm->conn, atom_value[i]);
415 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400416 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400417 width = 4;
418 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400419 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400420 }
421
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400422 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400424 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400425 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400426 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400427 }
428}
429
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200430static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400431read_and_dump_property(struct weston_wm *wm,
432 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400433{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400434 xcb_get_property_reply_t *reply;
435 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400436
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400437 cookie = xcb_get_property(wm->conn, 0, window,
438 property, XCB_ATOM_ANY, 0, 2048);
439 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400440
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400441 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400442
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400443 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400444}
445
Pekka Paalanen20111d52016-12-01 17:07:12 +0200446/* We reuse some predefined, but otherwise useles atoms
447 * as local type placeholders that never touch the X11 server,
448 * to make weston_wm_window_read_properties() less exceptional.
449 */
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400450#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
451#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500452#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700453#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400454
455static void
456weston_wm_window_read_properties(struct weston_wm_window *window)
457{
458 struct weston_wm *wm = window->wm;
459
Pekka Paalanen20111d52016-12-01 17:07:12 +0200460#define F(field) (&window->field)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400461 const struct {
462 xcb_atom_t atom;
463 xcb_atom_t type;
Pekka Paalanen20111d52016-12-01 17:07:12 +0200464 void *ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400465 } props[] = {
Pekka Paalanen20111d52016-12-01 17:07:12 +0200466 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
467 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
468 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
469 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, NULL },
470 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, NULL },
471 { wm->atom.net_wm_state, TYPE_NET_WM_STATE, NULL },
472 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
473 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
474 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
475 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, NULL },
476 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400477 };
478#undef F
479
480 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
481 xcb_get_property_reply_t *reply;
482 void *p;
483 uint32_t *xid;
484 xcb_atom_t *atom;
485 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200486 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400487
488 if (!window->properties_dirty)
489 return;
490 window->properties_dirty = 0;
491
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400492 for (i = 0; i < ARRAY_LENGTH(props); i++)
493 cookie[i] = xcb_get_property(wm->conn,
494 0, /* delete */
495 window->id,
496 props[i].atom,
497 XCB_ATOM_ANY, 0, 2048);
498
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700499 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700500 window->size_hints.flags = 0;
501 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700502 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700503
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400504 for (i = 0; i < ARRAY_LENGTH(props); i++) {
505 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
506 if (!reply)
507 /* Bad window, typically */
508 continue;
509 if (reply->type == XCB_ATOM_NONE) {
510 /* No such property */
511 free(reply);
512 continue;
513 }
514
Pekka Paalanen20111d52016-12-01 17:07:12 +0200515 p = props[i].ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400516
517 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300518 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400519 case XCB_ATOM_STRING:
520 /* FIXME: We're using this for both string and
521 utf8_string */
522 if (*(char **) p)
523 free(*(char **) p);
524
525 *(char **) p =
526 strndup(xcb_get_property_value(reply),
527 xcb_get_property_value_length(reply));
528 break;
529 case XCB_ATOM_WINDOW:
530 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500531 if (!wm_lookup_window(wm, *xid, p))
532 weston_log("XCB_ATOM_WINDOW contains window"
533 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400534 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300535 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400536 case XCB_ATOM_ATOM:
537 atom = xcb_get_property_value(reply);
538 *(xcb_atom_t *) p = *atom;
539 break;
540 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700541 atom = xcb_get_property_value(reply);
542 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500543 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700544 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500545 break;
546 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400547 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700548 case TYPE_WM_NORMAL_HINTS:
549 memcpy(&window->size_hints,
550 xcb_get_property_value(reply),
551 sizeof window->size_hints);
552 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500553 case TYPE_NET_WM_STATE:
554 window->fullscreen = 0;
555 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900556 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500557 if (atom[i] == wm->atom.net_wm_state_fullscreen)
558 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200559 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
560 window->maximized_vert = 1;
561 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
562 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900563 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500564 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400565 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700566 memcpy(&window->motif_hints,
567 xcb_get_property_value(reply),
568 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700569 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
570 if (window->motif_hints.decorations & MWM_DECOR_ALL)
571 /* MWM_DECOR_ALL means all except the other values listed. */
572 window->decorate =
573 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
574 else
575 window->decorate =
576 window->motif_hints.decorations;
577 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400578 break;
579 default:
580 break;
581 }
582 free(reply);
583 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200584
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200585 if (window->pid > 0) {
586 gethostname(name, sizeof(name));
587 for (i = 0; i < sizeof(name); i++) {
588 if (name[i] == '\0')
589 break;
590 }
591 if (i == sizeof(name))
592 name[0] = '\0'; /* ignore stupid hostnames */
593
594 /* this is only one heuristic to guess the PID of a client is
595 * valid, assuming it's compliant with icccm and ewmh.
596 * Non-compliants and remote applications of course fail. */
597 if (!window->machine || strcmp(window->machine, name))
598 window->pid = 0;
599 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400600}
601
Pekka Paalanen20111d52016-12-01 17:07:12 +0200602#undef TYPE_WM_PROTOCOLS
603#undef TYPE_MOTIF_WM_HINTS
604#undef TYPE_NET_WM_STATE
605#undef TYPE_WM_NORMAL_HINTS
606
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400607static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400608weston_wm_window_get_frame_size(struct weston_wm_window *window,
609 int *width, int *height)
610{
611 struct theme *t = window->wm->theme;
612
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500613 if (window->fullscreen) {
614 *width = window->width;
615 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800616 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500617 *width = frame_width(window->frame);
618 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400619 } else {
620 *width = window->width + t->margin * 2;
621 *height = window->height + t->margin * 2;
622 }
623}
624
625static void
626weston_wm_window_get_child_position(struct weston_wm_window *window,
627 int *x, int *y)
628{
629 struct theme *t = window->wm->theme;
630
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500631 if (window->fullscreen) {
632 *x = 0;
633 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800634 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500635 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400636 } else {
637 *x = t->margin;
638 *y = t->margin;
639 }
640}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400641
642static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500643weston_wm_window_send_configure_notify(struct weston_wm_window *window)
644{
645 xcb_configure_notify_event_t configure_notify;
646 struct weston_wm *wm = window->wm;
647 int x, y;
648
649 weston_wm_window_get_child_position(window, &x, &y);
650 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
651 configure_notify.pad0 = 0;
652 configure_notify.event = window->id;
653 configure_notify.window = window->id;
654 configure_notify.above_sibling = XCB_WINDOW_NONE;
655 configure_notify.x = x;
656 configure_notify.y = y;
657 configure_notify.width = window->width;
658 configure_notify.height = window->height;
659 configure_notify.border_width = 0;
660 configure_notify.override_redirect = 0;
661 configure_notify.pad1 = 0;
662
Murray Calavera883ac022015-06-06 13:02:22 +0000663 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500664 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
665 (char *) &configure_notify);
666}
667
668static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400669weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
670{
Murray Calavera883ac022015-06-06 13:02:22 +0000671 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400672 (xcb_configure_request_event_t *) event;
673 struct weston_wm_window *window;
674 uint32_t mask, values[16];
675 int x, y, width, height, i = 0;
676
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400677 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
678 configure_request->window,
679 configure_request->x, configure_request->y,
680 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400681
Derek Foreman49372142015-04-09 10:51:22 -0500682 if (!wm_lookup_window(wm, configure_request->window, &window))
683 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400684
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500685 if (window->fullscreen) {
686 weston_wm_window_send_configure_notify(window);
687 return;
688 }
689
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400690 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
691 window->width = configure_request->width;
692 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
693 window->height = configure_request->height;
694
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500695 if (window->frame)
696 frame_resize_inside(window->frame, window->width, window->height);
697
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400698 weston_wm_window_get_child_position(window, &x, &y);
699 values[i++] = x;
700 values[i++] = y;
701 values[i++] = window->width;
702 values[i++] = window->height;
703 values[i++] = 0;
704 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
705 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
706 XCB_CONFIG_WINDOW_BORDER_WIDTH;
707 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
708 values[i++] = configure_request->sibling;
709 mask |= XCB_CONFIG_WINDOW_SIBLING;
710 }
711 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
712 values[i++] = configure_request->stack_mode;
713 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
714 }
715
716 xcb_configure_window(wm->conn, window->id, mask, values);
717
718 weston_wm_window_get_frame_size(window, &width, &height);
719 values[0] = width;
720 values[1] = height;
721 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
722 xcb_configure_window(wm->conn, window->frame_id, mask, values);
723
724 weston_wm_window_schedule_repaint(window);
725}
726
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400727static int
728our_resource(struct weston_wm *wm, uint32_t id)
729{
730 const xcb_setup_t *setup;
731
732 setup = xcb_get_setup(wm->conn);
733
734 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
735}
736
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737static void
738weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
739{
Murray Calavera883ac022015-06-06 13:02:22 +0000740 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400741 (xcb_configure_notify_event_t *) event;
742 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743
Pekka Paalanen73428a82016-12-19 15:10:01 +0200744 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d%s\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400745 configure_notify->window,
746 configure_notify->x, configure_notify->y,
Pekka Paalanen73428a82016-12-19 15:10:01 +0200747 configure_notify->width, configure_notify->height,
748 configure_notify->override_redirect ? ", override" : "");
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300749
Derek Foreman49372142015-04-09 10:51:22 -0500750 if (!wm_lookup_window(wm, configure_notify->window, &window))
751 return;
752
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700753 window->x = configure_notify->x;
754 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200755 window->pos_dirty = false;
756
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700757 if (window->override_redirect) {
758 window->width = configure_notify->width;
759 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500760 if (window->frame)
761 frame_resize_inside(window->frame,
762 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700763 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764}
765
766static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300767weston_wm_kill_client(struct wl_listener *listener, void *data)
768{
769 struct weston_surface *surface = data;
770 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300771 if (!window)
772 return;
773
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200774 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300775 kill(window->pid, SIGKILL);
776}
777
778static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700779weston_wm_create_surface(struct wl_listener *listener, void *data)
780{
781 struct weston_surface *surface = data;
782 struct weston_wm *wm =
783 container_of(listener,
784 struct weston_wm, create_surface_listener);
785 struct weston_wm_window *window;
786
787 if (wl_resource_get_client(surface->resource) != wm->server->client)
788 return;
789
790 wl_list_for_each(window, &wm->unpaired_window_list, link)
791 if (window->surface_id ==
792 wl_resource_get_id(surface->resource)) {
793 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700794 window->surface_id = 0;
795 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700796 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000797 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700798}
799
800static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300801weston_wm_send_focus_window(struct weston_wm *wm,
802 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400803{
Scott Moreau85ecac02012-05-21 15:49:14 -0600804 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400805
Scott Moreau85ecac02012-05-21 15:49:14 -0600806 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700807 uint32_t values[1];
808
Boyan Dingb9f863c2014-08-30 10:33:23 +0800809 if (window->override_redirect)
810 return;
811
Scott Moreau85ecac02012-05-21 15:49:14 -0600812 client_message.response_type = XCB_CLIENT_MESSAGE;
813 client_message.format = 32;
814 client_message.window = window->id;
815 client_message.type = wm->atom.wm_protocols;
816 client_message.data.data32[0] = wm->atom.wm_take_focus;
817 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
818
Murray Calavera883ac022015-06-06 13:02:22 +0000819 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600820 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
821 (char *) &client_message);
822
823 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
824 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700825
826 values[0] = XCB_STACK_MODE_ABOVE;
827 xcb_configure_window (wm->conn, window->frame_id,
828 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600829 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400830 xcb_set_input_focus (wm->conn,
831 XCB_INPUT_FOCUS_POINTER_ROOT,
832 XCB_NONE,
833 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600834 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300835}
836
837static void
838weston_wm_window_activate(struct wl_listener *listener, void *data)
839{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800840 struct weston_surface_activation_data *activation_data = data;
841 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300842 struct weston_wm_window *window = NULL;
843 struct weston_wm *wm =
844 container_of(listener, struct weston_wm, activate_listener);
845
846 if (surface) {
847 window = get_wm_window(surface);
848 }
849
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200850 if (window) {
851 weston_wm_set_net_active_window(wm, window->id);
852 } else {
853 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
854 }
855
Giulio Camuffob18f7882015-03-29 14:20:23 +0300856 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400857
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500858 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800859 if (wm->focus_window->frame)
860 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400861 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500862 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400863 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500864 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800865 if (wm->focus_window->frame)
866 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500868 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200869
870 xcb_flush(wm->conn);
871
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400872}
873
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400874#define ICCCM_WITHDRAWN_STATE 0
875#define ICCCM_NORMAL_STATE 1
876#define ICCCM_ICONIC_STATE 3
877
878static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500879weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400880{
881 struct weston_wm *wm = window->wm;
882 uint32_t property[2];
883
884 property[0] = state;
885 property[1] = XCB_WINDOW_NONE;
886
887 xcb_change_property(wm->conn,
888 XCB_PROP_MODE_REPLACE,
889 window->id,
890 wm->atom.wm_state,
891 wm->atom.wm_state,
892 32, /* format */
893 2, property);
894}
895
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400896static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500897weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
898{
899 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200900 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500901 int i;
902
903 i = 0;
904 if (window->fullscreen)
905 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200906 if (window->maximized_vert)
907 property[i++] = wm->atom.net_wm_state_maximized_vert;
908 if (window->maximized_horz)
909 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500910
911 xcb_change_property(wm->conn,
912 XCB_PROP_MODE_REPLACE,
913 window->id,
914 wm->atom.net_wm_state,
915 XCB_ATOM_ATOM,
916 32, /* format */
917 i, property);
918}
919
920static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700921weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400922{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700923 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400924 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400925 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200926 int buttons = FRAME_BUTTON_CLOSE;
927
928 if (window->decorate & MWM_DECOR_MAXIMIZE)
929 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400930
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500931 window->frame = frame_create(window->wm->theme,
932 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200933 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500934 frame_resize_inside(window->frame, window->width, window->height);
935
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400936 weston_wm_window_get_frame_size(window, &width, &height);
937 weston_wm_window_get_child_position(window, &x, &y);
938
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400939 values[0] = wm->screen->black_pixel;
940 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400941 XCB_EVENT_MASK_KEY_PRESS |
942 XCB_EVENT_MASK_KEY_RELEASE |
943 XCB_EVENT_MASK_BUTTON_PRESS |
944 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400945 XCB_EVENT_MASK_POINTER_MOTION |
946 XCB_EVENT_MASK_ENTER_WINDOW |
947 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400949 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400950 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400951
952 window->frame_id = xcb_generate_id(wm->conn);
953 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400954 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400955 window->frame_id,
956 wm->screen->root,
957 0, 0,
958 width, height,
959 0,
960 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400961 wm->visual_id,
962 XCB_CW_BORDER_PIXEL |
963 XCB_CW_EVENT_MASK |
964 XCB_CW_COLORMAP, values);
965
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400966 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
967
968 values[0] = 0;
969 xcb_configure_window(wm->conn, window->id,
970 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
971
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400972 window->cairo_surface =
973 cairo_xcb_surface_create_with_xrender_format(wm->conn,
974 wm->screen,
975 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400976 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400977 width, height);
978
979 hash_table_insert(wm->window_hash, window->frame_id, window);
980}
981
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200982/*
983 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
984 * Passing a <0 desktop value deletes the property.
985 */
986static void
987weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700988 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200989{
990 if (desktop >= 0) {
991 xcb_change_property(window->wm->conn,
992 XCB_PROP_MODE_REPLACE,
993 window->id,
994 window->wm->atom.net_wm_desktop,
995 XCB_ATOM_CARDINAL,
996 32, /* format */
997 1, &desktop);
998 } else {
999 xcb_delete_property(window->wm->conn,
1000 window->id,
1001 window->wm->atom.net_wm_desktop);
1002 }
1003}
1004
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001005static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001006weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
1007{
1008 xcb_map_request_event_t *map_request =
1009 (xcb_map_request_event_t *) event;
1010 struct weston_wm_window *window;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001011 struct weston_output *output;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001012
1013 if (our_resource(wm, map_request->window)) {
1014 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
1015 map_request->window);
1016 return;
1017 }
1018
Derek Foreman49372142015-04-09 10:51:22 -05001019 if (!wm_lookup_window(wm, map_request->window, &window))
1020 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001021
1022 weston_wm_window_read_properties(window);
1023
Pekka Paalanenc9ca2c02016-12-15 16:04:14 +02001024 /* For a new Window, MapRequest happens before the Window is realized
1025 * in Xwayland. We do the real xcb_map_window() here as a response to
1026 * MapRequest. The Window will get realized (wl_surface created in
1027 * Wayland and WL_SURFACE_ID sent in X11) when it has been mapped for
1028 * real.
1029 *
1030 * MapRequest only happens for (X11) unmapped Windows. On UnmapNotify,
1031 * we reset shsurf to NULL, so even if X11 connection races far ahead
1032 * of the Wayland connection and the X11 client is repeatedly mapping
1033 * and unmapping, we will never have shsurf set on MapRequest.
1034 */
1035 assert(!window->shsurf);
1036
Pekka Paalanen882aff02016-11-16 14:15:18 +02001037 window->map_request_x = window->x;
1038 window->map_request_y = window->y;
1039
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001040 if (window->frame_id == XCB_WINDOW_NONE)
1041 weston_wm_window_create_frame(window);
1042
Pekka Paalanen44660c32016-11-14 15:38:43 +02001043 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d, %dx%d @ %d,%d)\n",
1044 window->id, window, window->frame_id,
Pekka Paalanen882aff02016-11-16 14:15:18 +02001045 window->width, window->height,
1046 window->map_request_x, window->map_request_y);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001047
1048 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1049 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001050 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001051
Pekka Paalanen505237e2016-12-01 15:41:11 +02001052 if (legacy_fullscreen(wm, window, &output)) {
1053 window->fullscreen = 1;
1054 weston_output_weak_ref_set(&window->legacy_fullscreen_output,
1055 output);
1056 }
1057
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001058 xcb_map_window(wm->conn, map_request->window);
1059 xcb_map_window(wm->conn, window->frame_id);
1060}
1061
1062static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001063weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1064{
1065 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1066
1067 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001068 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1069 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001070 return;
1071 }
1072
Pekka Paalanen73428a82016-12-19 15:10:01 +02001073 wm_log("XCB_MAP_NOTIFY (window %d%s)\n", map_notify->window,
1074 map_notify->override_redirect ? ", override" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001075}
1076
1077static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001078weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1079{
1080 xcb_unmap_notify_event_t *unmap_notify =
1081 (xcb_unmap_notify_event_t *) event;
1082 struct weston_wm_window *window;
1083
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001084 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1085 unmap_notify->window,
1086 unmap_notify->event,
1087 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001088
1089 if (our_resource(wm, unmap_notify->window))
1090 return;
1091
MoD31700122013-06-11 19:58:55 -05001092 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001093 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1094 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001095 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001096
Derek Foreman49372142015-04-09 10:51:22 -05001097 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1098 return;
1099
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001100 if (window->surface_id) {
1101 /* Make sure we're not on the unpaired surface list or we
1102 * could be assigned a surface during surface creation that
1103 * was mapped before this unmap request.
1104 */
1105 wl_list_remove(&window->link);
1106 window->surface_id = 0;
1107 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001108 if (wm->focus_window == window)
1109 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001110 if (window->surface)
1111 wl_list_remove(&window->surface_destroy_listener.link);
1112 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001113 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001114
1115 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1116 weston_wm_window_set_virtual_desktop(window, -1);
1117
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001118 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001119}
1120
1121static void
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001122weston_wm_window_draw_decoration(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001123{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001124 cairo_t *cr;
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001125 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001126
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001127 wm_log("XWM: draw decoration, win %d\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001128
1129 weston_wm_window_get_frame_size(window, &width, &height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001130
1131 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1132 cr = cairo_create(window->cairo_surface);
1133
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001134 if (window->fullscreen) {
1135 /* nothing */
1136 } else if (window->decorate) {
Pekka Paalanen9a330e12016-12-15 15:37:24 +02001137 frame_set_title(window->frame, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001138 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001139 } else {
1140 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1141 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1142 cairo_paint(cr);
1143
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001144 render_shadow(cr, window->wm->theme->shadow,
1145 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001146 }
1147
1148 cairo_destroy(cr);
Pekka Paalanendb7b9f32017-01-16 17:01:11 +02001149 cairo_surface_flush(window->cairo_surface);
1150 xcb_flush(window->wm->conn);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001151}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001152
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001153static void
1154weston_wm_window_set_pending_state(struct weston_wm_window *window)
1155{
1156 int x, y, width, height;
1157 int32_t input_x, input_y, input_w, input_h;
1158 const struct weston_desktop_xwayland_interface *xwayland_interface =
1159 window->wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001160
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001161 if (!window->surface)
1162 return;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001163
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001164 weston_wm_window_get_frame_size(window, &width, &height);
1165 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001166
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001167 pixman_region32_fini(&window->surface->pending.opaque);
1168 if (window->has_alpha) {
1169 pixman_region32_init(&window->surface->pending.opaque);
1170 } else {
1171 /* We leave an extra pixel around the X window area to
1172 * make sure we don't sample from the undefined alpha
1173 * channel when filtering. */
1174 pixman_region32_init_rect(&window->surface->pending.opaque,
1175 x - 1, y - 1,
1176 window->width + 2,
1177 window->height + 2);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001178 }
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001179
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001180 if (window->decorate && !window->fullscreen) {
1181 frame_input_rect(window->frame, &input_x, &input_y,
1182 &input_w, &input_h);
1183 } else {
1184 input_x = x;
1185 input_y = y;
1186 input_w = width;
1187 input_h = height;
1188 }
1189
1190 wm_log("XWM: win %d geometry: %d,%d %dx%d\n",
1191 window->id, input_x, input_y, input_w, input_h);
1192
Pekka Paalanen83626b92016-12-20 14:01:42 +02001193 pixman_region32_fini(&window->surface->pending.input);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001194 pixman_region32_init_rect(&window->surface->pending.input,
1195 input_x, input_y, input_w, input_h);
1196
1197 xwayland_interface->set_window_geometry(window->shsurf,
Pekka Paalanen83626b92016-12-20 14:01:42 +02001198 input_x, input_y,
1199 input_w, input_h);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001200 if (window->name)
1201 xwayland_interface->set_title(window->shsurf, window->name);
1202 if (window->pid > 0)
1203 xwayland_interface->set_pid(window->shsurf, window->pid);
1204}
1205
1206static void
1207weston_wm_window_do_repaint(void *data)
1208{
1209 struct weston_wm_window *window = data;
1210
1211 window->repaint_source = NULL;
1212
1213 weston_wm_window_read_properties(window);
1214
1215 weston_wm_window_draw_decoration(window);
1216 weston_wm_window_set_pending_state(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001217}
1218
1219static void
Pekka Paalanened568832016-12-20 14:05:03 +02001220weston_wm_window_set_pending_state_OR(struct weston_wm_window *window)
1221{
1222 int width, height;
1223
1224 /* for override-redirect windows */
1225 assert(window->frame_id == XCB_WINDOW_NONE);
1226
1227 if (!window->surface)
1228 return;
1229
1230 weston_wm_window_get_frame_size(window, &width, &height);
1231 pixman_region32_fini(&window->surface->pending.opaque);
1232 if (window->has_alpha) {
1233 pixman_region32_init(&window->surface->pending.opaque);
1234 } else {
1235 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1236 width, height);
1237 }
1238}
1239
1240static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001241weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1242{
1243 struct weston_wm *wm = window->wm;
1244
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001245 if (window->frame_id == XCB_WINDOW_NONE) {
Pekka Paalanened568832016-12-20 14:05:03 +02001246 /* Override-redirect windows go through here, but we
1247 * cannot assert(window->override_redirect); because
1248 * we do not deal with changing OR flag yet.
1249 * XXX: handle OR flag changes in message handlers
1250 */
1251 weston_wm_window_set_pending_state_OR(window);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001252 return;
1253 }
1254
1255 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001256 return;
1257
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001258 wm_log("XWM: schedule repaint, win %d\n", window->id);
1259
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001260 window->repaint_source =
1261 wl_event_loop_add_idle(wm->server->loop,
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001262 weston_wm_window_do_repaint, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001263}
1264
1265static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001266weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1267{
1268 xcb_property_notify_event_t *property_notify =
1269 (xcb_property_notify_event_t *) event;
1270 struct weston_wm_window *window;
1271
Derek Foreman49372142015-04-09 10:51:22 -05001272 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001273 return;
1274
1275 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001276
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001277 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001278 if (property_notify->state == XCB_PROPERTY_DELETE)
Pekka Paalanen8cc153b2016-12-19 15:18:45 +02001279 wm_log_continue("deleted %s\n",
1280 get_atom_name(wm->conn, property_notify->atom));
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001281 else
1282 read_and_dump_property(wm, property_notify->window,
1283 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001284
1285 if (property_notify->atom == wm->atom.net_wm_name ||
1286 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001287 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001288}
1289
1290static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001291weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001292 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001293{
1294 struct weston_wm_window *window;
1295 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001296 xcb_get_geometry_cookie_t geometry_cookie;
1297 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001298
Peter Huttererf3d62272013-08-08 11:57:05 +10001299 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001300 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001301 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001302 return;
1303 }
1304
MoD384a11a2013-06-22 11:04:21 -05001305 geometry_cookie = xcb_get_geometry(wm->conn, id);
1306
Giulio Camuffob18f7882015-03-29 14:20:23 +03001307 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1308 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001309 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1310
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001311 window->wm = wm;
1312 window->id = id;
1313 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001314 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001315 window->width = width;
1316 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001317 window->x = x;
1318 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001319 window->pos_dirty = false;
Pekka Paalanen882aff02016-11-16 14:15:18 +02001320 window->map_request_x = INT_MIN; /* out of range for valid positions */
1321 window->map_request_y = INT_MIN; /* out of range for valid positions */
Pekka Paalanen505237e2016-12-01 15:41:11 +02001322 weston_output_weak_ref_init(&window->legacy_fullscreen_output);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001323
MoD384a11a2013-06-22 11:04:21 -05001324 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1325 /* technically we should use XRender and check the visual format's
1326 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001327 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001328 window->has_alpha = geometry_reply->depth == 32;
1329 free(geometry_reply);
1330
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001331 hash_table_insert(wm->window_hash, id, window);
1332}
1333
1334static void
1335weston_wm_window_destroy(struct weston_wm_window *window)
1336{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001337 struct weston_wm *wm = window->wm;
1338
Pekka Paalanen505237e2016-12-01 15:41:11 +02001339 weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1340
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001341 if (window->repaint_source)
1342 wl_event_source_remove(window->repaint_source);
1343 if (window->cairo_surface)
1344 cairo_surface_destroy(window->cairo_surface);
1345
1346 if (window->frame_id) {
1347 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1348 xcb_destroy_window(wm->conn, window->frame_id);
1349 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001350 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001351 hash_table_remove(wm->window_hash, window->frame_id);
1352 window->frame_id = XCB_WINDOW_NONE;
1353 }
1354
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001355 if (window->surface_id)
1356 wl_list_remove(&window->link);
1357
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001358 if (window->surface)
1359 wl_list_remove(&window->surface_destroy_listener.link);
1360
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001361 hash_table_remove(window->wm->window_hash, window->id);
1362 free(window);
1363}
1364
1365static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001366weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1367{
1368 xcb_create_notify_event_t *create_notify =
1369 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001370
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001371 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 -04001372 create_notify->window,
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001373 create_notify->x, create_notify->y,
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001374 create_notify->width, create_notify->height,
1375 create_notify->override_redirect ? ", override" : "",
1376 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001377
1378 if (our_resource(wm, create_notify->window))
1379 return;
1380
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001381 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001382 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001383 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001384 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001385}
1386
1387static void
1388weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1389{
1390 xcb_destroy_notify_event_t *destroy_notify =
1391 (xcb_destroy_notify_event_t *) event;
1392 struct weston_wm_window *window;
1393
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001394 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1395 destroy_notify->window,
1396 destroy_notify->event,
1397 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001398
1399 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001400 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001401
Derek Foreman49372142015-04-09 10:51:22 -05001402 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1403 return;
1404
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001405 weston_wm_window_destroy(window);
1406}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001407
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001408static void
1409weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1410{
1411 xcb_reparent_notify_event_t *reparent_notify =
1412 (xcb_reparent_notify_event_t *) event;
1413 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001414
Pekka Paalanen73428a82016-12-19 15:10:01 +02001415 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d%s)\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001416 reparent_notify->window,
1417 reparent_notify->parent,
Pekka Paalanen73428a82016-12-19 15:10:01 +02001418 reparent_notify->event,
1419 reparent_notify->override_redirect ? ", override" : "");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001420
1421 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001422 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001423 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001424 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001425 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001426 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1427 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001428 weston_wm_window_destroy(window);
1429 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001430}
1431
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001432struct weston_seat *
1433weston_wm_pick_seat(struct weston_wm *wm)
1434{
Tom Hochsteine7fff212016-11-01 14:14:00 -05001435 struct wl_list *seats = wm->server->compositor->seat_list.next;
1436 if (wl_list_empty(seats))
1437 return NULL;
1438 return container_of(seats, struct weston_seat, link);
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001439}
1440
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001441static struct weston_seat *
1442weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1443{
1444 struct weston_wm *wm = window->wm;
1445 struct weston_seat *seat, *s;
1446
1447 seat = NULL;
1448 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001449 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1450 struct weston_pointer *old_pointer =
1451 weston_seat_get_pointer(seat);
1452
1453 if (pointer && pointer->focus &&
1454 pointer->focus->surface == window->surface &&
1455 pointer->button_count > 0 &&
1456 (!seat ||
1457 pointer->grab_serial -
1458 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001459 seat = s;
1460 }
1461
1462 return seat;
1463}
1464
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001465static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001466weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1467 xcb_client_message_event_t *client_message)
1468{
1469 static const int map[] = {
1470 THEME_LOCATION_RESIZING_TOP_LEFT,
1471 THEME_LOCATION_RESIZING_TOP,
1472 THEME_LOCATION_RESIZING_TOP_RIGHT,
1473 THEME_LOCATION_RESIZING_RIGHT,
1474 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1475 THEME_LOCATION_RESIZING_BOTTOM,
1476 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1477 THEME_LOCATION_RESIZING_LEFT
1478 };
1479
1480 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001481 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001482 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001483 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001484 const struct weston_desktop_xwayland_interface *xwayland_interface =
1485 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001486
Derek Foreman1281a362015-07-31 16:55:32 -05001487 if (!pointer || pointer->button_count != 1
1488 || !pointer->focus
1489 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001490 return;
1491
1492 detail = client_message->data.data32[2];
1493 switch (detail) {
1494 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001495 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001496 break;
1497 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1498 case _NET_WM_MOVERESIZE_SIZE_TOP:
1499 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1500 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1501 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1502 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1503 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1504 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001505 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001506 break;
1507 case _NET_WM_MOVERESIZE_CANCEL:
1508 break;
1509 }
1510}
1511
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001512#define _NET_WM_STATE_REMOVE 0
1513#define _NET_WM_STATE_ADD 1
1514#define _NET_WM_STATE_TOGGLE 2
1515
1516static int
1517update_state(int action, int *state)
1518{
1519 int new_state, changed;
1520
1521 switch (action) {
1522 case _NET_WM_STATE_REMOVE:
1523 new_state = 0;
1524 break;
1525 case _NET_WM_STATE_ADD:
1526 new_state = 1;
1527 break;
1528 case _NET_WM_STATE_TOGGLE:
1529 new_state = !*state;
1530 break;
1531 default:
1532 return 0;
1533 }
1534
1535 changed = (*state != new_state);
1536 *state = new_state;
1537
1538 return changed;
1539}
1540
1541static void
1542weston_wm_window_configure(void *data);
1543
1544static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001545weston_wm_window_set_toplevel(struct weston_wm_window *window)
1546{
Quentin Glidic955cec02016-08-12 10:41:35 +02001547 const struct weston_desktop_xwayland_interface *xwayland_interface =
1548 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001549
Quentin Glidic955cec02016-08-12 10:41:35 +02001550 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001551 window->width = window->saved_width;
1552 window->height = window->saved_height;
1553 if (window->frame)
1554 frame_resize_inside(window->frame,
1555 window->width,
1556 window->height);
1557 weston_wm_window_configure(window);
1558}
1559
1560static inline bool
1561weston_wm_window_is_maximized(struct weston_wm_window *window)
1562{
1563 return window->maximized_horz && window->maximized_vert;
1564}
1565
1566static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001567weston_wm_window_handle_state(struct weston_wm_window *window,
1568 xcb_client_message_event_t *client_message)
1569{
1570 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001571 const struct weston_desktop_xwayland_interface *xwayland_interface =
1572 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001573 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001574 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001575
1576 action = client_message->data.data32[0];
1577 property = client_message->data.data32[1];
1578
1579 if (property == wm->atom.net_wm_state_fullscreen &&
1580 update_state(action, &window->fullscreen)) {
1581 weston_wm_window_set_net_wm_state(window);
1582 if (window->fullscreen) {
1583 window->saved_width = window->width;
1584 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001585
1586 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001587 xwayland_interface->set_fullscreen(window->shsurf,
1588 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001589 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001590 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001591 weston_wm_window_set_toplevel(window);
1592 }
1593 } else {
1594 if (property == wm->atom.net_wm_state_maximized_vert &&
1595 update_state(action, &window->maximized_vert))
1596 weston_wm_window_set_net_wm_state(window);
1597 if (property == wm->atom.net_wm_state_maximized_horz &&
1598 update_state(action, &window->maximized_horz))
1599 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001600
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001601 if (maximized != weston_wm_window_is_maximized(window)) {
1602 if (weston_wm_window_is_maximized(window)) {
1603 window->saved_width = window->width;
1604 window->saved_height = window->height;
1605
1606 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001607 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001608 } else if (window->shsurf) {
1609 weston_wm_window_set_toplevel(window);
1610 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001611 }
1612 }
1613}
1614
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001615static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001616surface_destroy(struct wl_listener *listener, void *data)
1617{
1618 struct weston_wm_window *window =
1619 container_of(listener,
1620 struct weston_wm_window, surface_destroy_listener);
1621
1622 wm_log("surface for xid %d destroyed\n", window->id);
1623
1624 /* This should have been freed by the shell.
1625 * Don't try to use it later. */
1626 window->shsurf = NULL;
1627 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001628}
1629
1630static void
1631weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1632 xcb_client_message_event_t *client_message)
1633{
1634 struct weston_wm *wm = window->wm;
1635 struct wl_resource *resource;
1636
1637 if (window->surface_id != 0) {
1638 wm_log("already have surface id for window %d\n", window->id);
1639 return;
1640 }
1641
1642 /* Xwayland will send the wayland requests to create the
1643 * wl_surface before sending this client message. Even so, we
1644 * can end up handling the X event before the wayland requests
1645 * and thus when we try to look up the surface ID, the surface
1646 * hasn't been created yet. In that case put the window on
1647 * the unpaired window list and continue when the surface gets
1648 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001649 uint32_t id = client_message->data.data32[0];
1650 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001651 if (resource) {
1652 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001653 xserver_map_shell_surface(window,
1654 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001655 }
1656 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001657 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001658 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001659 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001660}
1661
1662static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001663weston_wm_handle_client_message(struct weston_wm *wm,
1664 xcb_generic_event_t *event)
1665{
1666 xcb_client_message_event_t *client_message =
1667 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001668 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001669
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001670 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1671 get_atom_name(wm->conn, client_message->type),
1672 client_message->data.data32[0],
1673 client_message->data.data32[1],
1674 client_message->data.data32[2],
1675 client_message->data.data32[3],
1676 client_message->data.data32[4],
1677 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001678
Jason Ekstrand0250a742014-07-24 13:17:47 -07001679 /* The window may get created and destroyed before we actually
1680 * handle the message. If it doesn't exist, bail.
1681 */
Derek Foreman49372142015-04-09 10:51:22 -05001682 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001683 return;
1684
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001685 if (client_message->type == wm->atom.net_wm_moveresize)
1686 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001687 else if (client_message->type == wm->atom.net_wm_state)
1688 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001689 else if (client_message->type == wm->atom.wl_surface_id)
1690 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001691}
1692
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001693enum cursor_type {
1694 XWM_CURSOR_TOP,
1695 XWM_CURSOR_BOTTOM,
1696 XWM_CURSOR_LEFT,
1697 XWM_CURSOR_RIGHT,
1698 XWM_CURSOR_TOP_LEFT,
1699 XWM_CURSOR_TOP_RIGHT,
1700 XWM_CURSOR_BOTTOM_LEFT,
1701 XWM_CURSOR_BOTTOM_RIGHT,
1702 XWM_CURSOR_LEFT_PTR,
1703};
1704
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001705/*
1706 * The following correspondences between file names and cursors was copied
1707 * from: https://bugs.kde.org/attachment.cgi?id=67313
1708 */
1709
1710static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001711 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001712 "sw-resize",
1713 "size_bdiag"
1714};
1715
1716static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001717 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001718 "se-resize",
1719 "size_fdiag"
1720};
1721
1722static const char *bottom_sides[] = {
1723 "bottom_side",
1724 "s-resize",
1725 "size_ver"
1726};
1727
1728static const char *left_ptrs[] = {
1729 "left_ptr",
1730 "default",
1731 "top_left_arrow",
1732 "left-arrow"
1733};
1734
1735static const char *left_sides[] = {
1736 "left_side",
1737 "w-resize",
1738 "size_hor"
1739};
1740
1741static const char *right_sides[] = {
1742 "right_side",
1743 "e-resize",
1744 "size_hor"
1745};
1746
1747static const char *top_left_corners[] = {
1748 "top_left_corner",
1749 "nw-resize",
1750 "size_fdiag"
1751};
1752
1753static const char *top_right_corners[] = {
1754 "top_right_corner",
1755 "ne-resize",
1756 "size_bdiag"
1757};
1758
1759static const char *top_sides[] = {
1760 "top_side",
1761 "n-resize",
1762 "size_ver"
1763};
1764
1765struct cursor_alternatives {
1766 const char **names;
1767 size_t count;
1768};
1769
1770static const struct cursor_alternatives cursors[] = {
1771 {top_sides, ARRAY_LENGTH(top_sides)},
1772 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1773 {left_sides, ARRAY_LENGTH(left_sides)},
1774 {right_sides, ARRAY_LENGTH(right_sides)},
1775 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1776 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1777 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1778 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1779 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001780};
1781
1782static void
1783weston_wm_create_cursors(struct weston_wm *wm)
1784{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001785 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001786 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001787 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001788
1789 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1790 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001791 for (j = 0; j < cursors[i].count; j++) {
1792 name = cursors[i].names[j];
1793 wm->cursors[i] =
1794 xcb_cursor_library_load_cursor(wm, name);
1795 if (wm->cursors[i] != (xcb_cursor_t)-1)
1796 break;
1797 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001798 }
1799
1800 wm->last_cursor = -1;
1801}
1802
1803static void
1804weston_wm_destroy_cursors(struct weston_wm *wm)
1805{
1806 uint8_t i;
1807
1808 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1809 xcb_free_cursor(wm->conn, wm->cursors[i]);
1810
1811 free(wm->cursors);
1812}
1813
1814static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001815get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001816{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001817 switch (location) {
1818 case THEME_LOCATION_RESIZING_TOP:
1819 return XWM_CURSOR_TOP;
1820 case THEME_LOCATION_RESIZING_BOTTOM:
1821 return XWM_CURSOR_BOTTOM;
1822 case THEME_LOCATION_RESIZING_LEFT:
1823 return XWM_CURSOR_LEFT;
1824 case THEME_LOCATION_RESIZING_RIGHT:
1825 return XWM_CURSOR_RIGHT;
1826 case THEME_LOCATION_RESIZING_TOP_LEFT:
1827 return XWM_CURSOR_TOP_LEFT;
1828 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1829 return XWM_CURSOR_TOP_RIGHT;
1830 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1831 return XWM_CURSOR_BOTTOM_LEFT;
1832 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1833 return XWM_CURSOR_BOTTOM_RIGHT;
1834 case THEME_LOCATION_EXTERIOR:
1835 case THEME_LOCATION_TITLEBAR:
1836 default:
1837 return XWM_CURSOR_LEFT_PTR;
1838 }
1839}
1840
1841static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001842weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1843 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001844{
1845 uint32_t cursor_value_list;
1846
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001847 if (wm->last_cursor == cursor)
1848 return;
1849
1850 wm->last_cursor = cursor;
1851
1852 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001853 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001854 XCB_CW_CURSOR, &cursor_value_list);
1855 xcb_flush(wm->conn);
1856}
1857
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001858static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001859weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001860{
1861 xcb_client_message_event_t client_message;
1862
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001863 if (window->delete_window) {
1864 client_message.response_type = XCB_CLIENT_MESSAGE;
1865 client_message.format = 32;
1866 client_message.window = window->id;
1867 client_message.type = window->wm->atom.wm_protocols;
1868 client_message.data.data32[0] =
1869 window->wm->atom.wm_delete_window;
1870 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001871
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001872 xcb_send_event(window->wm->conn, 0, window->id,
1873 XCB_EVENT_MASK_NO_EVENT,
1874 (char *) &client_message);
1875 } else {
1876 xcb_kill_client(window->wm->conn, window->id);
1877 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001878}
1879
1880static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001881weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1882{
1883 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001884 const struct weston_desktop_xwayland_interface *xwayland_interface =
1885 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001886 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001887 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001888 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001889 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001890 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001891 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001892
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001893 wm_log("XCB_BUTTON_%s (detail %d)\n",
1894 button->response_type == XCB_BUTTON_PRESS ?
1895 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001896
Derek Foreman49372142015-04-09 10:51:22 -05001897 if (!wm_lookup_window(wm, button->event, &window) ||
1898 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001899 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001900
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001901 if (button->detail != 1 && button->detail != 2)
1902 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001903
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001904 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001905 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001906
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001907 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001908 WL_POINTER_BUTTON_STATE_PRESSED :
1909 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001910 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1911
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001912 /* Make sure we're looking at the right location. The frame
1913 * could have received a motion event from a pointer from a
1914 * different wl_seat, but under X it looks like our core
1915 * pointer moved. Move the frame pointer to the button press
1916 * location before deciding what to do. */
1917 location = frame_pointer_motion(window->frame, NULL,
1918 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001919 location = frame_pointer_button(window->frame, NULL,
1920 button_id, button_state);
1921 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1922 weston_wm_window_schedule_repaint(window);
1923
1924 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001925 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001926 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001927 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1928 }
1929
1930 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001931 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001932 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001933 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1934 }
1935
1936 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001937 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001938 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001939 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001940
1941 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1942 window->maximized_horz = !window->maximized_horz;
1943 window->maximized_vert = !window->maximized_vert;
1944 if (weston_wm_window_is_maximized(window)) {
1945 window->saved_width = window->width;
1946 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001947 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001948 } else {
1949 weston_wm_window_set_toplevel(window);
1950 }
1951 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1952 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001953}
1954
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001955static void
1956weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1957{
1958 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1959 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001960 enum theme_location location;
1961 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001962
Derek Foreman49372142015-04-09 10:51:22 -05001963 if (!wm_lookup_window(wm, motion->event, &window) ||
1964 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001965 return;
1966
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001967 location = frame_pointer_motion(window->frame, NULL,
1968 motion->event_x, motion->event_y);
1969 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1970 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001971
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001972 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001973 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001974}
1975
1976static void
1977weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1978{
1979 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1980 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001981 enum theme_location location;
1982 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001983
Derek Foreman49372142015-04-09 10:51:22 -05001984 if (!wm_lookup_window(wm, enter->event, &window) ||
1985 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001986 return;
1987
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001988 location = frame_pointer_enter(window->frame, NULL,
1989 enter->event_x, enter->event_y);
1990 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1991 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001992
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001993 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001994 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001995}
1996
1997static void
1998weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1999{
2000 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
2001 struct weston_wm_window *window;
2002
Derek Foreman49372142015-04-09 10:51:22 -05002003 if (!wm_lookup_window(wm, leave->event, &window) ||
2004 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002005 return;
2006
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002007 frame_pointer_leave(window->frame, NULL);
2008 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2009 weston_wm_window_schedule_repaint(window);
2010
Tiago Vignattic1903232012-07-16 12:15:37 -04002011 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002012}
2013
Giulio Camuffob18f7882015-03-29 14:20:23 +03002014static void
2015weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
2016{
2017 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
2018 /* Do not let X clients change the focus behind the compositor's
2019 * back. Reset the focus to the old one if it changed. */
2020 if (!wm->focus_window || focus->event != wm->focus_window->id)
2021 weston_wm_send_focus_window(wm, wm->focus_window);
2022}
2023
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002024static int
2025weston_wm_handle_event(int fd, uint32_t mask, void *data)
2026{
2027 struct weston_wm *wm = data;
2028 xcb_generic_event_t *event;
2029 int count = 0;
2030
2031 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
2032 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002033 free(event);
2034 count++;
2035 continue;
2036 }
2037
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002038 if (weston_wm_handle_dnd_event(wm, event)) {
2039 free(event);
2040 count++;
2041 continue;
2042 }
2043
MoD31700122013-06-11 19:58:55 -05002044 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002045 case XCB_BUTTON_PRESS:
2046 case XCB_BUTTON_RELEASE:
2047 weston_wm_handle_button(wm, event);
2048 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002049 case XCB_ENTER_NOTIFY:
2050 weston_wm_handle_enter(wm, event);
2051 break;
2052 case XCB_LEAVE_NOTIFY:
2053 weston_wm_handle_leave(wm, event);
2054 break;
2055 case XCB_MOTION_NOTIFY:
2056 weston_wm_handle_motion(wm, event);
2057 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002058 case XCB_CREATE_NOTIFY:
2059 weston_wm_handle_create_notify(wm, event);
2060 break;
2061 case XCB_MAP_REQUEST:
2062 weston_wm_handle_map_request(wm, event);
2063 break;
2064 case XCB_MAP_NOTIFY:
2065 weston_wm_handle_map_notify(wm, event);
2066 break;
2067 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04002068 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002069 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04002070 case XCB_REPARENT_NOTIFY:
2071 weston_wm_handle_reparent_notify(wm, event);
2072 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002073 case XCB_CONFIGURE_REQUEST:
2074 weston_wm_handle_configure_request(wm, event);
2075 break;
2076 case XCB_CONFIGURE_NOTIFY:
2077 weston_wm_handle_configure_notify(wm, event);
2078 break;
2079 case XCB_DESTROY_NOTIFY:
2080 weston_wm_handle_destroy_notify(wm, event);
2081 break;
2082 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002083 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002084 break;
2085 case XCB_PROPERTY_NOTIFY:
2086 weston_wm_handle_property_notify(wm, event);
2087 break;
2088 case XCB_CLIENT_MESSAGE:
2089 weston_wm_handle_client_message(wm, event);
2090 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03002091 case XCB_FOCUS_IN:
2092 weston_wm_handle_focus_in(wm, event);
2093 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002094 }
2095
2096 free(event);
2097 count++;
2098 }
2099
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002100 if (count != 0)
2101 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002102
2103 return count;
2104}
2105
2106static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002107weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2108 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2109 wm->screen->root, wm->atom.net_active_window,
2110 wm->atom.window, 32, 1, &window);
2111}
2112
2113static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002114weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2115{
2116 xcb_depth_iterator_t d_iter;
2117 xcb_visualtype_iterator_t vt_iter;
2118 xcb_visualtype_t *visualtype;
2119
2120 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2121 visualtype = NULL;
2122 while (d_iter.rem > 0) {
2123 if (d_iter.data->depth == 32) {
2124 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2125 visualtype = vt_iter.data;
2126 break;
2127 }
2128
2129 xcb_depth_next(&d_iter);
2130 }
2131
2132 if (visualtype == NULL) {
2133 weston_log("no 32 bit visualtype\n");
2134 return;
2135 }
2136
2137 wm->visual_id = visualtype->visual_id;
2138 wm->colormap = xcb_generate_id(wm->conn);
2139 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2140 wm->colormap, wm->screen->root, wm->visual_id);
2141}
2142
2143static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002144weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002145{
2146
2147#define F(field) offsetof(struct weston_wm, field)
2148
2149 static const struct { const char *name; int offset; } atoms[] = {
2150 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002151 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002152 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2153 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002154 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002155 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002156 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002157 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002158 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002159 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002160 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2161 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002162 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2163 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002164 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2165 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2166 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002167 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002168 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2169
2170 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2171 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2172 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2173 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2174 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2175 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2176 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002177 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2178 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002179 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2180 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2181 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2182 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2183 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2184
2185 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2186 { "_NET_SUPPORTING_WM_CHECK",
2187 F(atom.net_supporting_wm_check) },
2188 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002189 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002190 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2191 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002192 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002193 { "TARGETS", F(atom.targets) },
2194 { "UTF8_STRING", F(atom.utf8_string) },
2195 { "_WL_SELECTION", F(atom.wl_selection) },
2196 { "INCR", F(atom.incr) },
2197 { "TIMESTAMP", F(atom.timestamp) },
2198 { "MULTIPLE", F(atom.multiple) },
2199 { "UTF8_STRING" , F(atom.utf8_string) },
2200 { "COMPOUND_TEXT", F(atom.compound_text) },
2201 { "TEXT", F(atom.text) },
2202 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002203 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002204 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2205 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002206 { "XdndSelection", F(atom.xdnd_selection) },
2207 { "XdndAware", F(atom.xdnd_aware) },
2208 { "XdndEnter", F(atom.xdnd_enter) },
2209 { "XdndLeave", F(atom.xdnd_leave) },
2210 { "XdndDrop", F(atom.xdnd_drop) },
2211 { "XdndStatus", F(atom.xdnd_status) },
2212 { "XdndFinished", F(atom.xdnd_finished) },
2213 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002214 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2215 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002216 };
2217#undef F
2218
2219 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2220 xcb_xfixes_query_version_reply_t *xfixes_reply;
2221 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2222 xcb_intern_atom_reply_t *reply;
2223 xcb_render_query_pict_formats_reply_t *formats_reply;
2224 xcb_render_query_pict_formats_cookie_t formats_cookie;
2225 xcb_render_pictforminfo_t *formats;
2226 uint32_t i;
2227
2228 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002229 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002230
2231 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2232
2233 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2234 cookies[i] = xcb_intern_atom (wm->conn, 0,
2235 strlen(atoms[i].name),
2236 atoms[i].name);
2237
2238 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2239 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2240 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2241 free(reply);
2242 }
2243
2244 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2245 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002246 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002247
2248 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2249 XCB_XFIXES_MAJOR_VERSION,
2250 XCB_XFIXES_MINOR_VERSION);
2251 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2252 xfixes_cookie, NULL);
2253
Martin Minarik6d118362012-06-07 18:01:59 +02002254 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002255 xfixes_reply->major_version, xfixes_reply->minor_version);
2256
2257 free(xfixes_reply);
2258
2259 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2260 formats_cookie, 0);
2261 if (formats_reply == NULL)
2262 return;
2263
2264 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002265 for (i = 0; i < formats_reply->num_formats; i++) {
2266 if (formats[i].direct.red_mask != 0xff &&
2267 formats[i].direct.red_shift != 16)
2268 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002269 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2270 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002271 wm->format_rgb = formats[i];
2272 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2273 formats[i].depth == 32 &&
2274 formats[i].direct.alpha_mask == 0xff &&
2275 formats[i].direct.alpha_shift == 24)
2276 wm->format_rgba = formats[i];
2277 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002278
2279 free(formats_reply);
2280}
2281
2282static void
2283weston_wm_create_wm_window(struct weston_wm *wm)
2284{
2285 static const char name[] = "Weston WM";
2286
2287 wm->wm_window = xcb_generate_id(wm->conn);
2288 xcb_create_window(wm->conn,
2289 XCB_COPY_FROM_PARENT,
2290 wm->wm_window,
2291 wm->screen->root,
2292 0, 0,
2293 10, 10,
2294 0,
2295 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2296 wm->screen->root_visual,
2297 0, NULL);
2298
2299 xcb_change_property(wm->conn,
2300 XCB_PROP_MODE_REPLACE,
2301 wm->wm_window,
2302 wm->atom.net_supporting_wm_check,
2303 XCB_ATOM_WINDOW,
2304 32, /* format */
2305 1, &wm->wm_window);
2306
2307 xcb_change_property(wm->conn,
2308 XCB_PROP_MODE_REPLACE,
2309 wm->wm_window,
2310 wm->atom.net_wm_name,
2311 wm->atom.utf8_string,
2312 8, /* format */
2313 strlen(name), name);
2314
2315 xcb_change_property(wm->conn,
2316 XCB_PROP_MODE_REPLACE,
2317 wm->screen->root,
2318 wm->atom.net_supporting_wm_check,
2319 XCB_ATOM_WINDOW,
2320 32, /* format */
2321 1, &wm->wm_window);
2322
Abdur Rehmanb8b150b2017-01-01 19:46:46 +05002323 /* Claim the WM_S0 selection even though we don't support
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002324 * the --replace functionality. */
2325 xcb_set_selection_owner(wm->conn,
2326 wm->wm_window,
2327 wm->atom.wm_s0,
2328 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002329
2330 xcb_set_selection_owner(wm->conn,
2331 wm->wm_window,
2332 wm->atom.net_wm_cm_s0,
2333 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002334}
2335
2336struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002337weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002338{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002339 struct weston_wm *wm;
2340 struct wl_event_loop *loop;
2341 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002342 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002343 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002344
Peter Huttererf3d62272013-08-08 11:57:05 +10002345 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002346 if (wm == NULL)
2347 return NULL;
2348
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002349 wm->server = wxs;
2350 wm->window_hash = hash_table_create();
2351 if (wm->window_hash == NULL) {
2352 free(wm);
2353 return NULL;
2354 }
2355
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002356 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002357 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002358 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002359 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002360 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002361 hash_table_destroy(wm->window_hash);
2362 free(wm);
2363 return NULL;
2364 }
2365
2366 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2367 wm->screen = s.data;
2368
2369 loop = wl_display_get_event_loop(wxs->wl_display);
2370 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002371 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002372 WL_EVENT_READABLE,
2373 weston_wm_handle_event, wm);
2374 wl_event_source_check(wm->source);
2375
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002376 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002377 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002378
2379 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002380 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2381 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2382 XCB_EVENT_MASK_PROPERTY_CHANGE;
2383 xcb_change_window_attributes(wm->conn, wm->screen->root,
2384 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002385
2386 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2387 XCB_COMPOSITE_REDIRECT_MANUAL);
2388
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002389 wm->theme = theme_create();
2390
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002391 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002392 supported[1] = wm->atom.net_wm_state;
2393 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002394 supported[3] = wm->atom.net_wm_state_maximized_vert;
2395 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002396 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002397 xcb_change_property(wm->conn,
2398 XCB_PROP_MODE_REPLACE,
2399 wm->screen->root,
2400 wm->atom.net_supported,
2401 XCB_ATOM_ATOM,
2402 32, /* format */
2403 ARRAY_LENGTH(supported), supported);
2404
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002405 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2406
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002407 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002408
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002409 weston_wm_dnd_init(wm);
2410
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002411 xcb_flush(wm->conn);
2412
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002413 wm->create_surface_listener.notify = weston_wm_create_surface;
2414 wl_signal_add(&wxs->compositor->create_surface_signal,
2415 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002416 wm->activate_listener.notify = weston_wm_window_activate;
2417 wl_signal_add(&wxs->compositor->activate_signal,
2418 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002419 wm->kill_listener.notify = weston_wm_kill_client;
2420 wl_signal_add(&wxs->compositor->kill_signal,
2421 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002422 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002423
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002424 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002425 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002426
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002427 /* Create wm window and take WM_S0 selection last, which
2428 * signals to Xwayland that we're done with setup. */
2429 weston_wm_create_wm_window(wm);
2430
2431 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002432
2433 return wm;
2434}
2435
2436void
2437weston_wm_destroy(struct weston_wm *wm)
2438{
2439 /* FIXME: Free windows in hash. */
2440 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002441 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002442 xcb_disconnect(wm->conn);
2443 wl_event_source_remove(wm->source);
2444 wl_list_remove(&wm->selection_listener.link);
2445 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002446 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002447 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002448
2449 free(wm);
2450}
2451
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002452static struct weston_wm_window *
2453get_wm_window(struct weston_surface *surface)
2454{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002455 struct wl_listener *listener;
2456
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002457 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002458 if (listener)
2459 return container_of(listener, struct weston_wm_window,
2460 surface_destroy_listener);
2461
2462 return NULL;
2463}
2464
Quentin Glidic955cec02016-08-12 10:41:35 +02002465static bool
2466is_wm_window(struct weston_surface *surface)
2467{
2468 return get_wm_window(surface) != NULL;
2469}
2470
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002471static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002472weston_wm_window_configure(void *data)
2473{
2474 struct weston_wm_window *window = data;
2475 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002476 uint32_t values[4];
2477 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002478
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002479 weston_wm_window_get_child_position(window, &x, &y);
2480 values[0] = x;
2481 values[1] = y;
2482 values[2] = window->width;
2483 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002484 xcb_configure_window(wm->conn,
2485 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002486 XCB_CONFIG_WINDOW_X |
2487 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002488 XCB_CONFIG_WINDOW_WIDTH |
2489 XCB_CONFIG_WINDOW_HEIGHT,
2490 values);
2491
2492 weston_wm_window_get_frame_size(window, &width, &height);
2493 values[0] = width;
2494 values[1] = height;
2495 xcb_configure_window(wm->conn,
2496 window->frame_id,
2497 XCB_CONFIG_WINDOW_WIDTH |
2498 XCB_CONFIG_WINDOW_HEIGHT,
2499 values);
2500
2501 window->configure_source = NULL;
2502
2503 weston_wm_window_schedule_repaint(window);
2504}
2505
2506static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002507send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002508{
2509 struct weston_wm_window *window = get_wm_window(surface);
2510 struct weston_wm *wm = window->wm;
2511 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002512 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002513
Marek Chalupae9fe4672014-10-29 13:44:44 +01002514 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002515 hborder = 2 * t->width;
2516 vborder = t->titlebar_height + t->width;
2517 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002518 hborder = 0;
2519 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002520 }
2521
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002522 if (width > hborder)
2523 window->width = width - hborder;
2524 else
2525 window->width = 1;
2526
2527 if (height > vborder)
2528 window->height = height - vborder;
2529 else
2530 window->height = 1;
2531
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002532 if (window->frame)
2533 frame_resize_inside(window->frame, window->width, window->height);
2534
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002535 if (window->configure_source)
2536 return;
2537
2538 window->configure_source =
2539 wl_event_loop_add_idle(wm->server->loop,
2540 weston_wm_window_configure, window);
2541}
2542
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002543static void
2544send_position(struct weston_surface *surface, int32_t x, int32_t y)
2545{
2546 struct weston_wm_window *window = get_wm_window(surface);
2547 struct weston_wm *wm;
2548 uint32_t mask, values[2];
2549
2550 if (!window || !window->wm)
2551 return;
2552
2553 wm = window->wm;
2554 /* We use pos_dirty to tell whether a configure message is in flight.
2555 * This is needed in case we send two configure events in a very
2556 * short time, since window->x/y is set in after a roundtrip, hence
2557 * we cannot just check if the current x and y are different. */
2558 if (window->x != x || window->y != y || window->pos_dirty) {
2559 window->pos_dirty = true;
2560 values[0] = x;
2561 values[1] = y;
2562 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2563
2564 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2565 xcb_flush(wm->conn);
2566 }
2567}
2568
Quentin Glidic955cec02016-08-12 10:41:35 +02002569static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002570 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002571};
2572
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002573static int
2574legacy_fullscreen(struct weston_wm *wm,
2575 struct weston_wm_window *window,
2576 struct weston_output **output_ret)
2577{
2578 struct weston_compositor *compositor = wm->server->compositor;
2579 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002580 uint32_t minmax = PMinSize | PMaxSize;
2581 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002582
2583 /* Heuristics for detecting legacy fullscreen windows... */
2584
2585 wl_list_for_each(output, &compositor->output_list, link) {
2586 if (output->x == window->x &&
2587 output->y == window->y &&
2588 output->width == window->width &&
2589 output->height == window->height &&
2590 window->override_redirect) {
2591 *output_ret = output;
2592 return 1;
2593 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002594
2595 matching_size = 0;
2596 if ((window->size_hints.flags & (USSize |PSize)) &&
2597 window->size_hints.width == output->width &&
2598 window->size_hints.height == output->height)
2599 matching_size = 1;
2600 if ((window->size_hints.flags & minmax) == minmax &&
2601 window->size_hints.min_width == output->width &&
2602 window->size_hints.min_height == output->height &&
2603 window->size_hints.max_width == output->width &&
2604 window->size_hints.max_height == output->height)
2605 matching_size = 1;
2606
2607 if (matching_size && !window->decorate &&
2608 (window->size_hints.flags & (USPosition | PPosition)) &&
2609 window->size_hints.x == output->x &&
2610 window->size_hints.y == output->y) {
2611 *output_ret = output;
2612 return 1;
2613 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002614 }
2615
2616 return 0;
2617}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002618
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002619static bool
Pekka Paalanen882aff02016-11-16 14:15:18 +02002620weston_wm_window_is_positioned(struct weston_wm_window *window)
2621{
2622 if (window->map_request_x == INT_MIN ||
2623 window->map_request_y == INT_MIN)
2624 weston_log("XWM warning: win %d did not see map request\n",
2625 window->id);
2626
2627 return window->map_request_x != 0 || window->map_request_y != 0;
2628}
2629
2630static bool
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002631weston_wm_window_type_inactive(struct weston_wm_window *window)
2632{
2633 struct weston_wm *wm = window->wm;
2634
2635 return window->type == wm->atom.net_wm_window_type_tooltip ||
2636 window->type == wm->atom.net_wm_window_type_dropdown ||
2637 window->type == wm->atom.net_wm_window_type_dnd ||
2638 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002639 window->type == wm->atom.net_wm_window_type_popup ||
2640 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002641}
2642
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002643static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002644xserver_map_shell_surface(struct weston_wm_window *window,
2645 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002646{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002647 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002648 struct weston_desktop_xwayland *xwayland =
2649 wm->server->compositor->xwayland;
2650 const struct weston_desktop_xwayland_interface *xwayland_interface =
2651 wm->server->compositor->xwayland_interface;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002652 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002653
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002654 weston_wm_window_read_properties(window);
2655
2656 /* A weston_wm_window may have many different surfaces assigned
2657 * throughout its life, so we must make sure to remove the listener
2658 * from the old surface signal list. */
2659 if (window->surface)
2660 wl_list_remove(&window->surface_destroy_listener.link);
2661
2662 window->surface = surface;
2663 window->surface_destroy_listener.notify = surface_destroy;
2664 wl_signal_add(&window->surface->destroy_signal,
2665 &window->surface_destroy_listener);
2666
2667 weston_wm_window_schedule_repaint(window);
2668
Quentin Glidic955cec02016-08-12 10:41:35 +02002669 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002670 return;
2671
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002672 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002673 weston_log("warning, unexpected in %s: "
2674 "surface's configure hook is already set.\n",
2675 __func__);
2676 return;
2677 }
2678
Murray Calavera883ac022015-06-06 13:02:22 +00002679 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002680 xwayland_interface->create_surface(xwayland,
2681 window->surface,
2682 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002683
Pekka Paalanena04eacc2016-11-28 16:42:25 +02002684 wm_log("XWM: map shell surface, win %d, xwayland surface %p\n",
2685 window->id, window->shsurf);
2686
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002687 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002688 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002689 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002690 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002691
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002692 if (window->fullscreen) {
2693 window->saved_width = window->width;
2694 window->saved_height = window->height;
Pekka Paalanen505237e2016-12-01 15:41:11 +02002695 xwayland_interface->set_fullscreen(window->shsurf,
2696 window->legacy_fullscreen_output.output);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002697 return;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002698 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002699 xwayland_interface->set_xwayland(window->shsurf,
2700 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002701 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002702 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002703 if (weston_wm_window_type_inactive(window)) {
2704 xwayland_interface->set_transient(window->shsurf,
2705 parent->surface,
2706 window->x - parent->x,
2707 window->y - parent->y);
2708 } else {
2709 xwayland_interface->set_toplevel(window->shsurf);
2710 xwayland_interface->set_parent(window->shsurf,
2711 parent->surface);
2712 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002713 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002714 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002715 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002716 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002717 xwayland_interface->set_xwayland(window->shsurf,
2718 window->x,
2719 window->y);
Pekka Paalanen882aff02016-11-16 14:15:18 +02002720 } else if (weston_wm_window_is_positioned(window)) {
2721 xwayland_interface->set_toplevel_with_position(window->shsurf,
2722 window->map_request_x,
2723 window->map_request_y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002724 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002725 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002726 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002727 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002728}
Quentin Glidic955cec02016-08-12 10:41:35 +02002729
2730const struct weston_xwayland_surface_api surface_api = {
2731 is_wm_window,
2732 send_position,
2733};