blob: 13cbe53f67b7048e1fe6ad14cbec26dc8ac907a9 [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>
29#include <stdio.h>
30#include <string.h>
31#include <sys/socket.h>
32#include <sys/un.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <unistd.h>
36#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040037#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050038#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040039
40#include "xwayland.h"
41
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080042#include "cairo-util.h"
43#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040044#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070045#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040046
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070047struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070048 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070049 int32_t x, y;
50 int32_t width, height; /* should set so old wm's don't mess up */
51 int32_t min_width, min_height;
52 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070053 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070054 struct {
55 int32_t x;
56 int32_t y;
57 } min_aspect, max_aspect;
58 int32_t base_width, base_height;
59 int32_t win_gravity;
60};
61
62#define USPosition (1L << 0)
63#define USSize (1L << 1)
64#define PPosition (1L << 2)
65#define PSize (1L << 3)
66#define PMinSize (1L << 4)
67#define PMaxSize (1L << 5)
68#define PResizeInc (1L << 6)
69#define PAspect (1L << 7)
70#define PBaseSize (1L << 8)
71#define PWinGravity (1L << 9)
72
Kristian Høgsberg380deee2012-05-21 17:12:41 -040073struct motif_wm_hints {
74 uint32_t flags;
75 uint32_t functions;
76 uint32_t decorations;
77 int32_t input_mode;
78 uint32_t status;
79};
80
81#define MWM_HINTS_FUNCTIONS (1L << 0)
82#define MWM_HINTS_DECORATIONS (1L << 1)
83#define MWM_HINTS_INPUT_MODE (1L << 2)
84#define MWM_HINTS_STATUS (1L << 3)
85
86#define MWM_FUNC_ALL (1L << 0)
87#define MWM_FUNC_RESIZE (1L << 1)
88#define MWM_FUNC_MOVE (1L << 2)
89#define MWM_FUNC_MINIMIZE (1L << 3)
90#define MWM_FUNC_MAXIMIZE (1L << 4)
91#define MWM_FUNC_CLOSE (1L << 5)
92
93#define MWM_DECOR_ALL (1L << 0)
94#define MWM_DECOR_BORDER (1L << 1)
95#define MWM_DECOR_RESIZEH (1L << 2)
96#define MWM_DECOR_TITLE (1L << 3)
97#define MWM_DECOR_MENU (1L << 4)
98#define MWM_DECOR_MINIMIZE (1L << 5)
99#define MWM_DECOR_MAXIMIZE (1L << 6)
100
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700101#define MWM_DECOR_EVERYTHING \
102 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
103 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
104
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400105#define MWM_INPUT_MODELESS 0
106#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
107#define MWM_INPUT_SYSTEM_MODAL 2
108#define MWM_INPUT_FULL_APPLICATION_MODAL 3
109#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
110
111#define MWM_TEAROFF_WINDOW (1L<<0)
112
113#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
114#define _NET_WM_MOVERESIZE_SIZE_TOP 1
115#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
116#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
117#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
118#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
119#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
120#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
121#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
122#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
123#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
124#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
125
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400126struct weston_wm_window {
127 struct weston_wm *wm;
128 xcb_window_t id;
129 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500130 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700132 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400133 struct weston_surface *surface;
134 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500135 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400136 struct wl_listener surface_destroy_listener;
137 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400138 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400139 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300140 int pid;
141 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 char *class;
143 char *name;
144 struct weston_wm_window *transient_for;
145 uint32_t protocols;
146 xcb_atom_t type;
147 int width, height;
148 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500149 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400150 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300151 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500152 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500153 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700154 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200155 int maximized_vert;
156 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700157 struct wm_size_hints size_hints;
158 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700159 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400160};
161
162static struct weston_wm_window *
163get_wm_window(struct weston_surface *surface);
164
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400165static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200166weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
167
168static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400169weston_wm_window_schedule_repaint(struct weston_wm_window *window);
170
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700171static void
172xserver_map_shell_surface(struct weston_wm_window *window,
173 struct weston_surface *surface);
174
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400175static int __attribute__ ((format (printf, 1, 2)))
176wm_log(const char *fmt, ...)
177{
178#ifdef WM_DEBUG
179 int l;
180 va_list argp;
181
182 va_start(argp, fmt);
183 l = weston_vlog(fmt, argp);
184 va_end(argp);
185
186 return l;
187#else
188 return 0;
189#endif
190}
191
192static int __attribute__ ((format (printf, 1, 2)))
193wm_log_continue(const char *fmt, ...)
194{
195#ifdef WM_DEBUG
196 int l;
197 va_list argp;
198
199 va_start(argp, fmt);
200 l = weston_vlog_continue(fmt, argp);
201 va_end(argp);
202
203 return l;
204#else
205 return 0;
206#endif
207}
208
Derek Foreman49372142015-04-09 10:51:22 -0500209static bool __attribute__ ((warn_unused_result))
210wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
211 struct weston_wm_window **window)
212{
213 *window = hash_table_lookup(wm->window_hash, hash);
214 if (*window)
215 return true;
216 return false;
217}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400218
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400219const char *
220get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
221{
222 xcb_get_atom_name_cookie_t cookie;
223 xcb_get_atom_name_reply_t *reply;
224 xcb_generic_error_t *e;
225 static char buffer[64];
226
227 if (atom == XCB_ATOM_NONE)
228 return "None";
229
230 cookie = xcb_get_atom_name (c, atom);
231 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500232
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300233 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500234 snprintf(buffer, sizeof buffer, "%.*s",
235 xcb_get_atom_name_name_length (reply),
236 xcb_get_atom_name_name (reply));
237 } else {
238 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
239 }
240
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400241 free(reply);
242
243 return buffer;
244}
245
Tiago Vignatti90fada42012-07-16 12:02:08 -0400246static xcb_cursor_t
247xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
248{
249 xcb_connection_t *c = wm->conn;
250 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
251 xcb_screen_t *screen = s.data;
252 xcb_gcontext_t gc;
253 xcb_pixmap_t pix;
254 xcb_render_picture_t pic;
255 xcb_cursor_t cursor;
256 int stride = img->width * 4;
257
258 pix = xcb_generate_id(c);
259 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
260
261 pic = xcb_generate_id(c);
262 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
263
264 gc = xcb_generate_id(c);
265 xcb_create_gc(c, gc, pix, 0, 0);
266
267 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
268 img->width, img->height, 0, 0, 0, 32,
269 stride * img->height, (uint8_t *) img->pixels);
270 xcb_free_gc(c, gc);
271
272 cursor = xcb_generate_id(c);
273 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
274
275 xcb_render_free_picture(c, pic);
276 xcb_free_pixmap(c, pix);
277
278 return cursor;
279}
280
281static xcb_cursor_t
282xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
283{
284 /* TODO: treat animated cursors as well */
285 if (images->nimage != 1)
286 return -1;
287
288 return xcb_cursor_image_load_cursor(wm, images->images[0]);
289}
290
291static xcb_cursor_t
292xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
293{
294 xcb_cursor_t cursor;
295 XcursorImages *images;
296 char *v = NULL;
297 int size = 0;
298
299 if (!file)
300 return 0;
301
302 v = getenv ("XCURSOR_SIZE");
303 if (v)
304 size = atoi(v);
305
306 if (!size)
307 size = 32;
308
309 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300310 if (!images)
311 return -1;
312
Tiago Vignatti90fada42012-07-16 12:02:08 -0400313 cursor = xcb_cursor_images_load_cursor (wm, images);
314 XcursorImagesDestroy (images);
315
316 return cursor;
317}
318
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400319void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400320dump_property(struct weston_wm *wm,
321 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400322{
323 int32_t *incr_value;
324 const char *text_value, *name;
325 xcb_atom_t *atom_value;
326 int width, len;
327 uint32_t i;
328
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400329 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400330 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400331 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400332 return;
333 }
334
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400335 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
336 get_atom_name(wm->conn, reply->type),
337 reply->format,
338 xcb_get_property_value_length(reply),
339 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400340
341 if (reply->type == wm->atom.incr) {
342 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400343 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400344 } else if (reply->type == wm->atom.utf8_string ||
345 reply->type == wm->atom.string) {
346 text_value = xcb_get_property_value(reply);
347 if (reply->value_len > 40)
348 len = 40;
349 else
350 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400351 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400352 } else if (reply->type == XCB_ATOM_ATOM) {
353 atom_value = xcb_get_property_value(reply);
354 for (i = 0; i < reply->value_len; i++) {
355 name = get_atom_name(wm->conn, atom_value[i]);
356 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400357 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400358 width = 4;
359 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400360 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361 }
362
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400363 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400364 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400365 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400366 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400367 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400368 }
369}
370
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200371static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400372read_and_dump_property(struct weston_wm *wm,
373 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400374{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400375 xcb_get_property_reply_t *reply;
376 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400377
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400378 cookie = xcb_get_property(wm->conn, 0, window,
379 property, XCB_ATOM_ANY, 0, 2048);
380 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400382 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400383
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400384 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385}
386
387/* We reuse some predefined, but otherwise useles atoms */
388#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
389#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500390#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700391#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400392
393static void
394weston_wm_window_read_properties(struct weston_wm_window *window)
395{
396 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200397 struct weston_shell_interface *shell_interface =
398 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399
400#define F(field) offsetof(struct weston_wm_window, field)
401 const struct {
402 xcb_atom_t atom;
403 xcb_atom_t type;
404 int offset;
405 } props[] = {
406 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
407 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
408 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
409 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700410 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500411 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400412 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
413 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300414 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400415 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300416 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400417 };
418#undef F
419
420 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
421 xcb_get_property_reply_t *reply;
422 void *p;
423 uint32_t *xid;
424 xcb_atom_t *atom;
425 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200426 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400427
428 if (!window->properties_dirty)
429 return;
430 window->properties_dirty = 0;
431
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400432 for (i = 0; i < ARRAY_LENGTH(props); i++)
433 cookie[i] = xcb_get_property(wm->conn,
434 0, /* delete */
435 window->id,
436 props[i].atom,
437 XCB_ATOM_ANY, 0, 2048);
438
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700439 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700440 window->size_hints.flags = 0;
441 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700442 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700443
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400444 for (i = 0; i < ARRAY_LENGTH(props); i++) {
445 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
446 if (!reply)
447 /* Bad window, typically */
448 continue;
449 if (reply->type == XCB_ATOM_NONE) {
450 /* No such property */
451 free(reply);
452 continue;
453 }
454
455 p = ((char *) window + props[i].offset);
456
457 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300458 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400459 case XCB_ATOM_STRING:
460 /* FIXME: We're using this for both string and
461 utf8_string */
462 if (*(char **) p)
463 free(*(char **) p);
464
465 *(char **) p =
466 strndup(xcb_get_property_value(reply),
467 xcb_get_property_value_length(reply));
468 break;
469 case XCB_ATOM_WINDOW:
470 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500471 if (!wm_lookup_window(wm, *xid, p))
472 weston_log("XCB_ATOM_WINDOW contains window"
473 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400474 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300475 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400476 case XCB_ATOM_ATOM:
477 atom = xcb_get_property_value(reply);
478 *(xcb_atom_t *) p = *atom;
479 break;
480 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700481 atom = xcb_get_property_value(reply);
482 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500483 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700484 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500485 break;
486 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400487 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700488 case TYPE_WM_NORMAL_HINTS:
489 memcpy(&window->size_hints,
490 xcb_get_property_value(reply),
491 sizeof window->size_hints);
492 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500493 case TYPE_NET_WM_STATE:
494 window->fullscreen = 0;
495 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900496 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500497 if (atom[i] == wm->atom.net_wm_state_fullscreen)
498 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200499 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
500 window->maximized_vert = 1;
501 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
502 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900503 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500504 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400505 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700506 memcpy(&window->motif_hints,
507 xcb_get_property_value(reply),
508 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700509 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
510 if (window->motif_hints.decorations & MWM_DECOR_ALL)
511 /* MWM_DECOR_ALL means all except the other values listed. */
512 window->decorate =
513 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
514 else
515 window->decorate =
516 window->motif_hints.decorations;
517 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400518 break;
519 default:
520 break;
521 }
522 free(reply);
523 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200524
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200525 if (window->pid > 0) {
526 gethostname(name, sizeof(name));
527 for (i = 0; i < sizeof(name); i++) {
528 if (name[i] == '\0')
529 break;
530 }
531 if (i == sizeof(name))
532 name[0] = '\0'; /* ignore stupid hostnames */
533
534 /* this is only one heuristic to guess the PID of a client is
535 * valid, assuming it's compliant with icccm and ewmh.
536 * Non-compliants and remote applications of course fail. */
537 if (!window->machine || strcmp(window->machine, name))
538 window->pid = 0;
539 }
540
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200541 if (window->shsurf && window->name)
542 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500543 if (window->frame && window->name)
544 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200545 if (window->shsurf && window->pid > 0)
546 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400547}
548
549static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400550weston_wm_window_get_frame_size(struct weston_wm_window *window,
551 int *width, int *height)
552{
553 struct theme *t = window->wm->theme;
554
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500555 if (window->fullscreen) {
556 *width = window->width;
557 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800558 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500559 *width = frame_width(window->frame);
560 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400561 } else {
562 *width = window->width + t->margin * 2;
563 *height = window->height + t->margin * 2;
564 }
565}
566
567static void
568weston_wm_window_get_child_position(struct weston_wm_window *window,
569 int *x, int *y)
570{
571 struct theme *t = window->wm->theme;
572
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500573 if (window->fullscreen) {
574 *x = 0;
575 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800576 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500577 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400578 } else {
579 *x = t->margin;
580 *y = t->margin;
581 }
582}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400583
584static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500585weston_wm_window_send_configure_notify(struct weston_wm_window *window)
586{
587 xcb_configure_notify_event_t configure_notify;
588 struct weston_wm *wm = window->wm;
589 int x, y;
590
591 weston_wm_window_get_child_position(window, &x, &y);
592 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
593 configure_notify.pad0 = 0;
594 configure_notify.event = window->id;
595 configure_notify.window = window->id;
596 configure_notify.above_sibling = XCB_WINDOW_NONE;
597 configure_notify.x = x;
598 configure_notify.y = y;
599 configure_notify.width = window->width;
600 configure_notify.height = window->height;
601 configure_notify.border_width = 0;
602 configure_notify.override_redirect = 0;
603 configure_notify.pad1 = 0;
604
Murray Calavera883ac022015-06-06 13:02:22 +0000605 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500606 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
607 (char *) &configure_notify);
608}
609
610static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400611weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
612{
Murray Calavera883ac022015-06-06 13:02:22 +0000613 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400614 (xcb_configure_request_event_t *) event;
615 struct weston_wm_window *window;
616 uint32_t mask, values[16];
617 int x, y, width, height, i = 0;
618
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400619 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
620 configure_request->window,
621 configure_request->x, configure_request->y,
622 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400623
Derek Foreman49372142015-04-09 10:51:22 -0500624 if (!wm_lookup_window(wm, configure_request->window, &window))
625 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400626
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500627 if (window->fullscreen) {
628 weston_wm_window_send_configure_notify(window);
629 return;
630 }
631
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400632 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
633 window->width = configure_request->width;
634 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
635 window->height = configure_request->height;
636
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500637 if (window->frame)
638 frame_resize_inside(window->frame, window->width, window->height);
639
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400640 weston_wm_window_get_child_position(window, &x, &y);
641 values[i++] = x;
642 values[i++] = y;
643 values[i++] = window->width;
644 values[i++] = window->height;
645 values[i++] = 0;
646 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
647 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
648 XCB_CONFIG_WINDOW_BORDER_WIDTH;
649 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
650 values[i++] = configure_request->sibling;
651 mask |= XCB_CONFIG_WINDOW_SIBLING;
652 }
653 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
654 values[i++] = configure_request->stack_mode;
655 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
656 }
657
658 xcb_configure_window(wm->conn, window->id, mask, values);
659
660 weston_wm_window_get_frame_size(window, &width, &height);
661 values[0] = width;
662 values[1] = height;
663 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
664 xcb_configure_window(wm->conn, window->frame_id, mask, values);
665
666 weston_wm_window_schedule_repaint(window);
667}
668
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400669static int
670our_resource(struct weston_wm *wm, uint32_t id)
671{
672 const xcb_setup_t *setup;
673
674 setup = xcb_get_setup(wm->conn);
675
676 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
677}
678
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400679static void
680weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
681{
Murray Calavera883ac022015-06-06 13:02:22 +0000682 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400683 (xcb_configure_notify_event_t *) event;
684 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400685
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400686 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400687 configure_notify->window,
688 configure_notify->x, configure_notify->y,
689 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300690
Derek Foreman49372142015-04-09 10:51:22 -0500691 if (!wm_lookup_window(wm, configure_notify->window, &window))
692 return;
693
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700694 window->x = configure_notify->x;
695 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700696 if (window->override_redirect) {
697 window->width = configure_notify->width;
698 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500699 if (window->frame)
700 frame_resize_inside(window->frame,
701 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700702 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400703}
704
705static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300706weston_wm_kill_client(struct wl_listener *listener, void *data)
707{
708 struct weston_surface *surface = data;
709 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300710 if (!window)
711 return;
712
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200713 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300714 kill(window->pid, SIGKILL);
715}
716
717static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700718weston_wm_create_surface(struct wl_listener *listener, void *data)
719{
720 struct weston_surface *surface = data;
721 struct weston_wm *wm =
722 container_of(listener,
723 struct weston_wm, create_surface_listener);
724 struct weston_wm_window *window;
725
726 if (wl_resource_get_client(surface->resource) != wm->server->client)
727 return;
728
729 wl_list_for_each(window, &wm->unpaired_window_list, link)
730 if (window->surface_id ==
731 wl_resource_get_id(surface->resource)) {
732 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700733 window->surface_id = 0;
734 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700735 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000736 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700737}
738
739static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300740weston_wm_send_focus_window(struct weston_wm *wm,
741 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400742{
Scott Moreau85ecac02012-05-21 15:49:14 -0600743 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400744
Scott Moreau85ecac02012-05-21 15:49:14 -0600745 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700746 uint32_t values[1];
747
Boyan Dingb9f863c2014-08-30 10:33:23 +0800748 if (window->override_redirect)
749 return;
750
Scott Moreau85ecac02012-05-21 15:49:14 -0600751 client_message.response_type = XCB_CLIENT_MESSAGE;
752 client_message.format = 32;
753 client_message.window = window->id;
754 client_message.type = wm->atom.wm_protocols;
755 client_message.data.data32[0] = wm->atom.wm_take_focus;
756 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
757
Murray Calavera883ac022015-06-06 13:02:22 +0000758 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600759 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
760 (char *) &client_message);
761
762 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
763 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700764
765 values[0] = XCB_STACK_MODE_ABOVE;
766 xcb_configure_window (wm->conn, window->frame_id,
767 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600768 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400769 xcb_set_input_focus (wm->conn,
770 XCB_INPUT_FOCUS_POINTER_ROOT,
771 XCB_NONE,
772 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600773 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300774}
775
776static void
777weston_wm_window_activate(struct wl_listener *listener, void *data)
778{
779 struct weston_surface *surface = data;
780 struct weston_wm_window *window = NULL;
781 struct weston_wm *wm =
782 container_of(listener, struct weston_wm, activate_listener);
783
784 if (surface) {
785 window = get_wm_window(surface);
786 }
787
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200788 if (window) {
789 weston_wm_set_net_active_window(wm, window->id);
790 } else {
791 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
792 }
793
Giulio Camuffob18f7882015-03-29 14:20:23 +0300794 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400795
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500796 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800797 if (wm->focus_window->frame)
798 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400799 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500800 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400801 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500802 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800803 if (wm->focus_window->frame)
804 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400805 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500806 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200807
808 xcb_flush(wm->conn);
809
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400810}
811
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300812static void
813weston_wm_window_transform(struct wl_listener *listener, void *data)
814{
815 struct weston_surface *surface = data;
816 struct weston_wm_window *window = get_wm_window(surface);
817 struct weston_wm *wm =
818 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300819 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300820
821 if (!window || !wm)
822 return;
823
Jason Ekstranda7af7042013-10-12 22:38:11 -0500824 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300825 return;
826
Jason Ekstranda7af7042013-10-12 22:38:11 -0500827 if (window->x != window->view->geometry.x ||
828 window->y != window->view->geometry.y) {
829 values[0] = window->view->geometry.x;
830 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700831 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300832
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700833 xcb_configure_window(wm->conn, window->frame_id, mask, values);
834 xcb_flush(wm->conn);
835 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300836}
837
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400838#define ICCCM_WITHDRAWN_STATE 0
839#define ICCCM_NORMAL_STATE 1
840#define ICCCM_ICONIC_STATE 3
841
842static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500843weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400844{
845 struct weston_wm *wm = window->wm;
846 uint32_t property[2];
847
848 property[0] = state;
849 property[1] = XCB_WINDOW_NONE;
850
851 xcb_change_property(wm->conn,
852 XCB_PROP_MODE_REPLACE,
853 window->id,
854 wm->atom.wm_state,
855 wm->atom.wm_state,
856 32, /* format */
857 2, property);
858}
859
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400860static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500861weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
862{
863 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200864 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500865 int i;
866
867 i = 0;
868 if (window->fullscreen)
869 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200870 if (window->maximized_vert)
871 property[i++] = wm->atom.net_wm_state_maximized_vert;
872 if (window->maximized_horz)
873 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500874
875 xcb_change_property(wm->conn,
876 XCB_PROP_MODE_REPLACE,
877 window->id,
878 wm->atom.net_wm_state,
879 XCB_ATOM_ATOM,
880 32, /* format */
881 i, property);
882}
883
884static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700885weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400886{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700887 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400888 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400889 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200890 int buttons = FRAME_BUTTON_CLOSE;
891
892 if (window->decorate & MWM_DECOR_MAXIMIZE)
893 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400894
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500895 window->frame = frame_create(window->wm->theme,
896 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200897 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500898 frame_resize_inside(window->frame, window->width, window->height);
899
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400900 weston_wm_window_get_frame_size(window, &width, &height);
901 weston_wm_window_get_child_position(window, &x, &y);
902
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400903 values[0] = wm->screen->black_pixel;
904 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400905 XCB_EVENT_MASK_KEY_PRESS |
906 XCB_EVENT_MASK_KEY_RELEASE |
907 XCB_EVENT_MASK_BUTTON_PRESS |
908 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400909 XCB_EVENT_MASK_POINTER_MOTION |
910 XCB_EVENT_MASK_ENTER_WINDOW |
911 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400912 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400913 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400914 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400915
916 window->frame_id = xcb_generate_id(wm->conn);
917 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400918 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400919 window->frame_id,
920 wm->screen->root,
921 0, 0,
922 width, height,
923 0,
924 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400925 wm->visual_id,
926 XCB_CW_BORDER_PIXEL |
927 XCB_CW_EVENT_MASK |
928 XCB_CW_COLORMAP, values);
929
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400930 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
931
932 values[0] = 0;
933 xcb_configure_window(wm->conn, window->id,
934 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
935
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400936 window->cairo_surface =
937 cairo_xcb_surface_create_with_xrender_format(wm->conn,
938 wm->screen,
939 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400940 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400941 width, height);
942
943 hash_table_insert(wm->window_hash, window->frame_id, window);
944}
945
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200946/*
947 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
948 * Passing a <0 desktop value deletes the property.
949 */
950static void
951weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700952 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200953{
954 if (desktop >= 0) {
955 xcb_change_property(window->wm->conn,
956 XCB_PROP_MODE_REPLACE,
957 window->id,
958 window->wm->atom.net_wm_desktop,
959 XCB_ATOM_CARDINAL,
960 32, /* format */
961 1, &desktop);
962 } else {
963 xcb_delete_property(window->wm->conn,
964 window->id,
965 window->wm->atom.net_wm_desktop);
966 }
967}
968
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400969static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700970weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
971{
972 xcb_map_request_event_t *map_request =
973 (xcb_map_request_event_t *) event;
974 struct weston_wm_window *window;
975
976 if (our_resource(wm, map_request->window)) {
977 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
978 map_request->window);
979 return;
980 }
981
Derek Foreman49372142015-04-09 10:51:22 -0500982 if (!wm_lookup_window(wm, map_request->window, &window))
983 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700984
985 weston_wm_window_read_properties(window);
986
987 if (window->frame_id == XCB_WINDOW_NONE)
988 weston_wm_window_create_frame(window);
989
990 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
991 window->id, window, window->frame_id);
992
993 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
994 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200995 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700996
997 xcb_map_window(wm->conn, map_request->window);
998 xcb_map_window(wm->conn, window->frame_id);
999}
1000
1001static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001002weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1003{
1004 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1005
1006 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001007 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1008 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001009 return;
1010 }
1011
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001012 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001013}
1014
1015static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001016weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1017{
1018 xcb_unmap_notify_event_t *unmap_notify =
1019 (xcb_unmap_notify_event_t *) event;
1020 struct weston_wm_window *window;
1021
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001022 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1023 unmap_notify->window,
1024 unmap_notify->event,
1025 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001026
1027 if (our_resource(wm, unmap_notify->window))
1028 return;
1029
MoD31700122013-06-11 19:58:55 -05001030 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001031 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1032 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001033 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001034
Derek Foreman49372142015-04-09 10:51:22 -05001035 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1036 return;
1037
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001038 if (window->surface_id) {
1039 /* Make sure we're not on the unpaired surface list or we
1040 * could be assigned a surface during surface creation that
1041 * was mapped before this unmap request.
1042 */
1043 wl_list_remove(&window->link);
1044 window->surface_id = 0;
1045 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001046 if (wm->focus_window == window)
1047 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001048 if (window->surface)
1049 wl_list_remove(&window->surface_destroy_listener.link);
1050 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001051 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -08001052 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001053
1054 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1055 weston_wm_window_set_virtual_desktop(window, -1);
1056
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001057 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001058}
1059
1060static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001061weston_wm_window_draw_decoration(void *data)
1062{
1063 struct weston_wm_window *window = data;
1064 struct weston_wm *wm = window->wm;
1065 struct theme *t = wm->theme;
1066 cairo_t *cr;
1067 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001068 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001069 struct weston_shell_interface *shell_interface =
1070 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001071 uint32_t flags = 0;
1072
1073 weston_wm_window_read_properties(window);
1074
1075 window->repaint_source = NULL;
1076
1077 weston_wm_window_get_frame_size(window, &width, &height);
1078 weston_wm_window_get_child_position(window, &x, &y);
1079
1080 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1081 cr = cairo_create(window->cairo_surface);
1082
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001083 if (window->fullscreen) {
1084 /* nothing */
1085 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001086 if (wm->focus_window == window)
1087 flags |= THEME_FRAME_ACTIVE;
1088
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001089 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001090 } else {
1091 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1092 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1093 cairo_paint(cr);
1094
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001095 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001096 }
1097
1098 cairo_destroy(cr);
1099
1100 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001101 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001102 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001103 pixman_region32_init(&window->surface->pending.opaque);
1104 } else {
1105 /* We leave an extra pixel around the X window area to
1106 * make sure we don't sample from the undefined alpha
1107 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001108 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001109 x - 1, y - 1,
1110 window->width + 2,
1111 window->height + 2);
1112 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 if (window->view)
1114 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001115
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001116 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001117
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001118 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001119 frame_input_rect(window->frame, &input_x, &input_y,
1120 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001121 } else {
1122 input_x = x;
1123 input_y = y;
1124 input_w = width;
1125 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001126 }
1127
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001128 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001129 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001130
1131 shell_interface->set_window_geometry(window->shsurf,
1132 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001133 }
1134}
1135
1136static void
1137weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1138{
1139 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001140 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001141
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001142 if (window->frame_id == XCB_WINDOW_NONE) {
1143 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001144 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001145 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001146 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001147 pixman_region32_init(&window->surface->pending.opaque);
1148 } else {
1149 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1150 width, height);
1151 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 if (window->view)
1153 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001154 }
1155 return;
1156 }
1157
1158 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001159 return;
1160
1161 window->repaint_source =
1162 wl_event_loop_add_idle(wm->server->loop,
1163 weston_wm_window_draw_decoration,
1164 window);
1165}
1166
1167static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001168weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1169{
1170 xcb_property_notify_event_t *property_notify =
1171 (xcb_property_notify_event_t *) event;
1172 struct weston_wm_window *window;
1173
Derek Foreman49372142015-04-09 10:51:22 -05001174 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001175 return;
1176
1177 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001178
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001179 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001180 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001181 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001182 else
1183 read_and_dump_property(wm, property_notify->window,
1184 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001185
1186 if (property_notify->atom == wm->atom.net_wm_name ||
1187 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001188 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001189}
1190
1191static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001192weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001193 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001194{
1195 struct weston_wm_window *window;
1196 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001197 xcb_get_geometry_cookie_t geometry_cookie;
1198 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001199
Peter Huttererf3d62272013-08-08 11:57:05 +10001200 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001201 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001202 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001203 return;
1204 }
1205
MoD384a11a2013-06-22 11:04:21 -05001206 geometry_cookie = xcb_get_geometry(wm->conn, id);
1207
Giulio Camuffob18f7882015-03-29 14:20:23 +03001208 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1209 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001210 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1211
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001212 window->wm = wm;
1213 window->id = id;
1214 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001215 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001216 window->width = width;
1217 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001218 window->x = x;
1219 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001220
MoD384a11a2013-06-22 11:04:21 -05001221 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1222 /* technically we should use XRender and check the visual format's
1223 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001224 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001225 window->has_alpha = geometry_reply->depth == 32;
1226 free(geometry_reply);
1227
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001228 hash_table_insert(wm->window_hash, id, window);
1229}
1230
1231static void
1232weston_wm_window_destroy(struct weston_wm_window *window)
1233{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001234 struct weston_wm *wm = window->wm;
1235
1236 if (window->repaint_source)
1237 wl_event_source_remove(window->repaint_source);
1238 if (window->cairo_surface)
1239 cairo_surface_destroy(window->cairo_surface);
1240
1241 if (window->frame_id) {
1242 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1243 xcb_destroy_window(wm->conn, window->frame_id);
1244 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001245 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001246 hash_table_remove(wm->window_hash, window->frame_id);
1247 window->frame_id = XCB_WINDOW_NONE;
1248 }
1249
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001250 if (window->surface_id)
1251 wl_list_remove(&window->link);
1252
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001253 if (window->surface)
1254 wl_list_remove(&window->surface_destroy_listener.link);
1255
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001256 hash_table_remove(window->wm->window_hash, window->id);
1257 free(window);
1258}
1259
1260static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001261weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1262{
1263 xcb_create_notify_event_t *create_notify =
1264 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001265
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001266 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1267 create_notify->window,
1268 create_notify->width, create_notify->height,
1269 create_notify->override_redirect ? ", override" : "",
1270 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001271
1272 if (our_resource(wm, create_notify->window))
1273 return;
1274
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001275 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001276 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001277 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001278 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001279}
1280
1281static void
1282weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1283{
1284 xcb_destroy_notify_event_t *destroy_notify =
1285 (xcb_destroy_notify_event_t *) event;
1286 struct weston_wm_window *window;
1287
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001288 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1289 destroy_notify->window,
1290 destroy_notify->event,
1291 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001292
1293 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001294 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001295
Derek Foreman49372142015-04-09 10:51:22 -05001296 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1297 return;
1298
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001299 weston_wm_window_destroy(window);
1300}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001301
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001302static void
1303weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1304{
1305 xcb_reparent_notify_event_t *reparent_notify =
1306 (xcb_reparent_notify_event_t *) event;
1307 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001308
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001309 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1310 reparent_notify->window,
1311 reparent_notify->parent,
1312 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001313
1314 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001315 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001316 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001317 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001318 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001319 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1320 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001321 weston_wm_window_destroy(window);
1322 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001323}
1324
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001325struct weston_seat *
1326weston_wm_pick_seat(struct weston_wm *wm)
1327{
1328 return container_of(wm->server->compositor->seat_list.next,
1329 struct weston_seat, link);
1330}
1331
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001332static struct weston_seat *
1333weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1334{
1335 struct weston_wm *wm = window->wm;
1336 struct weston_seat *seat, *s;
1337
1338 seat = NULL;
1339 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001340 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1341 struct weston_pointer *old_pointer =
1342 weston_seat_get_pointer(seat);
1343
1344 if (pointer && pointer->focus &&
1345 pointer->focus->surface == window->surface &&
1346 pointer->button_count > 0 &&
1347 (!seat ||
1348 pointer->grab_serial -
1349 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001350 seat = s;
1351 }
1352
1353 return seat;
1354}
1355
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001356static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001357weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1358 xcb_client_message_event_t *client_message)
1359{
1360 static const int map[] = {
1361 THEME_LOCATION_RESIZING_TOP_LEFT,
1362 THEME_LOCATION_RESIZING_TOP,
1363 THEME_LOCATION_RESIZING_TOP_RIGHT,
1364 THEME_LOCATION_RESIZING_RIGHT,
1365 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1366 THEME_LOCATION_RESIZING_BOTTOM,
1367 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1368 THEME_LOCATION_RESIZING_LEFT
1369 };
1370
1371 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001372 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001373 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001374 int detail;
1375 struct weston_shell_interface *shell_interface =
1376 &wm->server->compositor->shell_interface;
1377
Derek Foreman1281a362015-07-31 16:55:32 -05001378 if (!pointer || pointer->button_count != 1
1379 || !pointer->focus
1380 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001381 return;
1382
1383 detail = client_message->data.data32[2];
1384 switch (detail) {
1385 case _NET_WM_MOVERESIZE_MOVE:
Derek Foremane4d6c832015-08-05 14:48:11 -05001386 shell_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001387 break;
1388 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1389 case _NET_WM_MOVERESIZE_SIZE_TOP:
1390 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1391 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1392 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1393 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1394 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1395 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Derek Foremane4d6c832015-08-05 14:48:11 -05001396 shell_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001397 break;
1398 case _NET_WM_MOVERESIZE_CANCEL:
1399 break;
1400 }
1401}
1402
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001403#define _NET_WM_STATE_REMOVE 0
1404#define _NET_WM_STATE_ADD 1
1405#define _NET_WM_STATE_TOGGLE 2
1406
1407static int
1408update_state(int action, int *state)
1409{
1410 int new_state, changed;
1411
1412 switch (action) {
1413 case _NET_WM_STATE_REMOVE:
1414 new_state = 0;
1415 break;
1416 case _NET_WM_STATE_ADD:
1417 new_state = 1;
1418 break;
1419 case _NET_WM_STATE_TOGGLE:
1420 new_state = !*state;
1421 break;
1422 default:
1423 return 0;
1424 }
1425
1426 changed = (*state != new_state);
1427 *state = new_state;
1428
1429 return changed;
1430}
1431
1432static void
1433weston_wm_window_configure(void *data);
1434
1435static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001436weston_wm_window_set_toplevel(struct weston_wm_window *window)
1437{
1438 struct weston_shell_interface *shell_interface =
1439 &window->wm->server->compositor->shell_interface;
1440
1441 shell_interface->set_toplevel(window->shsurf);
1442 window->width = window->saved_width;
1443 window->height = window->saved_height;
1444 if (window->frame)
1445 frame_resize_inside(window->frame,
1446 window->width,
1447 window->height);
1448 weston_wm_window_configure(window);
1449}
1450
1451static inline bool
1452weston_wm_window_is_maximized(struct weston_wm_window *window)
1453{
1454 return window->maximized_horz && window->maximized_vert;
1455}
1456
1457static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001458weston_wm_window_handle_state(struct weston_wm_window *window,
1459 xcb_client_message_event_t *client_message)
1460{
1461 struct weston_wm *wm = window->wm;
1462 struct weston_shell_interface *shell_interface =
1463 &wm->server->compositor->shell_interface;
1464 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001465 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001466
1467 action = client_message->data.data32[0];
1468 property = client_message->data.data32[1];
1469
1470 if (property == wm->atom.net_wm_state_fullscreen &&
1471 update_state(action, &window->fullscreen)) {
1472 weston_wm_window_set_net_wm_state(window);
1473 if (window->fullscreen) {
1474 window->saved_width = window->width;
1475 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001476
1477 if (window->shsurf)
1478 shell_interface->set_fullscreen(window->shsurf,
1479 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1480 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001481 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001482 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001483 weston_wm_window_set_toplevel(window);
1484 }
1485 } else {
1486 if (property == wm->atom.net_wm_state_maximized_vert &&
1487 update_state(action, &window->maximized_vert))
1488 weston_wm_window_set_net_wm_state(window);
1489 if (property == wm->atom.net_wm_state_maximized_horz &&
1490 update_state(action, &window->maximized_horz))
1491 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001492
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001493 if (maximized != weston_wm_window_is_maximized(window)) {
1494 if (weston_wm_window_is_maximized(window)) {
1495 window->saved_width = window->width;
1496 window->saved_height = window->height;
1497
1498 if (window->shsurf)
1499 shell_interface->set_maximized(window->shsurf);
1500 } else if (window->shsurf) {
1501 weston_wm_window_set_toplevel(window);
1502 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001503 }
1504 }
1505}
1506
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001507static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001508surface_destroy(struct wl_listener *listener, void *data)
1509{
1510 struct weston_wm_window *window =
1511 container_of(listener,
1512 struct weston_wm_window, surface_destroy_listener);
1513
1514 wm_log("surface for xid %d destroyed\n", window->id);
1515
1516 /* This should have been freed by the shell.
1517 * Don't try to use it later. */
1518 window->shsurf = NULL;
1519 window->surface = NULL;
1520 window->view = NULL;
1521}
1522
1523static void
1524weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1525 xcb_client_message_event_t *client_message)
1526{
1527 struct weston_wm *wm = window->wm;
1528 struct wl_resource *resource;
1529
1530 if (window->surface_id != 0) {
1531 wm_log("already have surface id for window %d\n", window->id);
1532 return;
1533 }
1534
1535 /* Xwayland will send the wayland requests to create the
1536 * wl_surface before sending this client message. Even so, we
1537 * can end up handling the X event before the wayland requests
1538 * and thus when we try to look up the surface ID, the surface
1539 * hasn't been created yet. In that case put the window on
1540 * the unpaired window list and continue when the surface gets
1541 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001542 uint32_t id = client_message->data.data32[0];
1543 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001544 if (resource) {
1545 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001546 xserver_map_shell_surface(window,
1547 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001548 }
1549 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001550 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001551 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001552 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001553}
1554
1555static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001556weston_wm_handle_client_message(struct weston_wm *wm,
1557 xcb_generic_event_t *event)
1558{
1559 xcb_client_message_event_t *client_message =
1560 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001561 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001562
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001563 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1564 get_atom_name(wm->conn, client_message->type),
1565 client_message->data.data32[0],
1566 client_message->data.data32[1],
1567 client_message->data.data32[2],
1568 client_message->data.data32[3],
1569 client_message->data.data32[4],
1570 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001571
Jason Ekstrand0250a742014-07-24 13:17:47 -07001572 /* The window may get created and destroyed before we actually
1573 * handle the message. If it doesn't exist, bail.
1574 */
Derek Foreman49372142015-04-09 10:51:22 -05001575 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001576 return;
1577
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001578 if (client_message->type == wm->atom.net_wm_moveresize)
1579 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001580 else if (client_message->type == wm->atom.net_wm_state)
1581 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001582 else if (client_message->type == wm->atom.wl_surface_id)
1583 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001584}
1585
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001586enum cursor_type {
1587 XWM_CURSOR_TOP,
1588 XWM_CURSOR_BOTTOM,
1589 XWM_CURSOR_LEFT,
1590 XWM_CURSOR_RIGHT,
1591 XWM_CURSOR_TOP_LEFT,
1592 XWM_CURSOR_TOP_RIGHT,
1593 XWM_CURSOR_BOTTOM_LEFT,
1594 XWM_CURSOR_BOTTOM_RIGHT,
1595 XWM_CURSOR_LEFT_PTR,
1596};
1597
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001598/*
1599 * The following correspondences between file names and cursors was copied
1600 * from: https://bugs.kde.org/attachment.cgi?id=67313
1601 */
1602
1603static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001604 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001605 "sw-resize",
1606 "size_bdiag"
1607};
1608
1609static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001610 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001611 "se-resize",
1612 "size_fdiag"
1613};
1614
1615static const char *bottom_sides[] = {
1616 "bottom_side",
1617 "s-resize",
1618 "size_ver"
1619};
1620
1621static const char *left_ptrs[] = {
1622 "left_ptr",
1623 "default",
1624 "top_left_arrow",
1625 "left-arrow"
1626};
1627
1628static const char *left_sides[] = {
1629 "left_side",
1630 "w-resize",
1631 "size_hor"
1632};
1633
1634static const char *right_sides[] = {
1635 "right_side",
1636 "e-resize",
1637 "size_hor"
1638};
1639
1640static const char *top_left_corners[] = {
1641 "top_left_corner",
1642 "nw-resize",
1643 "size_fdiag"
1644};
1645
1646static const char *top_right_corners[] = {
1647 "top_right_corner",
1648 "ne-resize",
1649 "size_bdiag"
1650};
1651
1652static const char *top_sides[] = {
1653 "top_side",
1654 "n-resize",
1655 "size_ver"
1656};
1657
1658struct cursor_alternatives {
1659 const char **names;
1660 size_t count;
1661};
1662
1663static const struct cursor_alternatives cursors[] = {
1664 {top_sides, ARRAY_LENGTH(top_sides)},
1665 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1666 {left_sides, ARRAY_LENGTH(left_sides)},
1667 {right_sides, ARRAY_LENGTH(right_sides)},
1668 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1669 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1670 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1671 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1672 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001673};
1674
1675static void
1676weston_wm_create_cursors(struct weston_wm *wm)
1677{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001678 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001679 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001680 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001681
1682 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1683 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001684 for (j = 0; j < cursors[i].count; j++) {
1685 name = cursors[i].names[j];
1686 wm->cursors[i] =
1687 xcb_cursor_library_load_cursor(wm, name);
1688 if (wm->cursors[i] != (xcb_cursor_t)-1)
1689 break;
1690 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001691 }
1692
1693 wm->last_cursor = -1;
1694}
1695
1696static void
1697weston_wm_destroy_cursors(struct weston_wm *wm)
1698{
1699 uint8_t i;
1700
1701 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1702 xcb_free_cursor(wm->conn, wm->cursors[i]);
1703
1704 free(wm->cursors);
1705}
1706
1707static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001708get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001709{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001710 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001711
1712 switch (location) {
1713 case THEME_LOCATION_RESIZING_TOP:
1714 return XWM_CURSOR_TOP;
1715 case THEME_LOCATION_RESIZING_BOTTOM:
1716 return XWM_CURSOR_BOTTOM;
1717 case THEME_LOCATION_RESIZING_LEFT:
1718 return XWM_CURSOR_LEFT;
1719 case THEME_LOCATION_RESIZING_RIGHT:
1720 return XWM_CURSOR_RIGHT;
1721 case THEME_LOCATION_RESIZING_TOP_LEFT:
1722 return XWM_CURSOR_TOP_LEFT;
1723 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1724 return XWM_CURSOR_TOP_RIGHT;
1725 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1726 return XWM_CURSOR_BOTTOM_LEFT;
1727 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1728 return XWM_CURSOR_BOTTOM_RIGHT;
1729 case THEME_LOCATION_EXTERIOR:
1730 case THEME_LOCATION_TITLEBAR:
1731 default:
1732 return XWM_CURSOR_LEFT_PTR;
1733 }
1734}
1735
1736static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001737weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1738 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001739{
1740 uint32_t cursor_value_list;
1741
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001742 if (wm->last_cursor == cursor)
1743 return;
1744
1745 wm->last_cursor = cursor;
1746
1747 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001748 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001749 XCB_CW_CURSOR, &cursor_value_list);
1750 xcb_flush(wm->conn);
1751}
1752
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001753static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001754weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001755{
1756 xcb_client_message_event_t client_message;
1757
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001758 if (window->delete_window) {
1759 client_message.response_type = XCB_CLIENT_MESSAGE;
1760 client_message.format = 32;
1761 client_message.window = window->id;
1762 client_message.type = window->wm->atom.wm_protocols;
1763 client_message.data.data32[0] =
1764 window->wm->atom.wm_delete_window;
1765 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001766
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001767 xcb_send_event(window->wm->conn, 0, window->id,
1768 XCB_EVENT_MASK_NO_EVENT,
1769 (char *) &client_message);
1770 } else {
1771 xcb_kill_client(window->wm->conn, window->id);
1772 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001773}
1774
1775static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001776weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1777{
1778 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1779 struct weston_shell_interface *shell_interface =
1780 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001781 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001782 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001783 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001784 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001785 enum frame_button_state button_state;
1786 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001787
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001788 wm_log("XCB_BUTTON_%s (detail %d)\n",
1789 button->response_type == XCB_BUTTON_PRESS ?
1790 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001791
Derek Foreman49372142015-04-09 10:51:22 -05001792 if (!wm_lookup_window(wm, button->event, &window) ||
1793 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001794 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001795
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001796 if (button->detail != 1 && button->detail != 2)
1797 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001798
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001799 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001800 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001801
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001802 button_state = button->response_type == XCB_BUTTON_PRESS ?
1803 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1804 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1805
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001806 /* Make sure we're looking at the right location. The frame
1807 * could have received a motion event from a pointer from a
1808 * different wl_seat, but under X it looks like our core
1809 * pointer moved. Move the frame pointer to the button press
1810 * location before deciding what to do. */
1811 location = frame_pointer_motion(window->frame, NULL,
1812 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001813 location = frame_pointer_button(window->frame, NULL,
1814 button_id, button_state);
1815 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1816 weston_wm_window_schedule_repaint(window);
1817
1818 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001819 if (pointer)
1820 shell_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001821 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1822 }
1823
1824 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001825 if (pointer)
1826 shell_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001827 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1828 }
1829
1830 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001831 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001832 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001833 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001834
1835 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1836 window->maximized_horz = !window->maximized_horz;
1837 window->maximized_vert = !window->maximized_vert;
1838 if (weston_wm_window_is_maximized(window)) {
1839 window->saved_width = window->width;
1840 window->saved_height = window->height;
1841 shell_interface->set_maximized(window->shsurf);
1842 } else {
1843 weston_wm_window_set_toplevel(window);
1844 }
1845 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1846 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001847}
1848
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001849static void
1850weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1851{
1852 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1853 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001854 enum theme_location location;
1855 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001856
Derek Foreman49372142015-04-09 10:51:22 -05001857 if (!wm_lookup_window(wm, motion->event, &window) ||
1858 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001859 return;
1860
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001861 location = frame_pointer_motion(window->frame, NULL,
1862 motion->event_x, motion->event_y);
1863 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1864 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001865
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001866 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001867 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001868}
1869
1870static void
1871weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1872{
1873 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1874 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001875 enum theme_location location;
1876 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001877
Derek Foreman49372142015-04-09 10:51:22 -05001878 if (!wm_lookup_window(wm, enter->event, &window) ||
1879 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001880 return;
1881
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001882 location = frame_pointer_enter(window->frame, NULL,
1883 enter->event_x, enter->event_y);
1884 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1885 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001886
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001887 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001888 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001889}
1890
1891static void
1892weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1893{
1894 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1895 struct weston_wm_window *window;
1896
Derek Foreman49372142015-04-09 10:51:22 -05001897 if (!wm_lookup_window(wm, leave->event, &window) ||
1898 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001899 return;
1900
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001901 frame_pointer_leave(window->frame, NULL);
1902 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1903 weston_wm_window_schedule_repaint(window);
1904
Tiago Vignattic1903232012-07-16 12:15:37 -04001905 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001906}
1907
Giulio Camuffob18f7882015-03-29 14:20:23 +03001908static void
1909weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1910{
1911 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1912 /* Do not let X clients change the focus behind the compositor's
1913 * back. Reset the focus to the old one if it changed. */
1914 if (!wm->focus_window || focus->event != wm->focus_window->id)
1915 weston_wm_send_focus_window(wm, wm->focus_window);
1916}
1917
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001918static int
1919weston_wm_handle_event(int fd, uint32_t mask, void *data)
1920{
1921 struct weston_wm *wm = data;
1922 xcb_generic_event_t *event;
1923 int count = 0;
1924
1925 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1926 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001927 free(event);
1928 count++;
1929 continue;
1930 }
1931
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001932 if (weston_wm_handle_dnd_event(wm, event)) {
1933 free(event);
1934 count++;
1935 continue;
1936 }
1937
MoD31700122013-06-11 19:58:55 -05001938 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001939 case XCB_BUTTON_PRESS:
1940 case XCB_BUTTON_RELEASE:
1941 weston_wm_handle_button(wm, event);
1942 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001943 case XCB_ENTER_NOTIFY:
1944 weston_wm_handle_enter(wm, event);
1945 break;
1946 case XCB_LEAVE_NOTIFY:
1947 weston_wm_handle_leave(wm, event);
1948 break;
1949 case XCB_MOTION_NOTIFY:
1950 weston_wm_handle_motion(wm, event);
1951 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001952 case XCB_CREATE_NOTIFY:
1953 weston_wm_handle_create_notify(wm, event);
1954 break;
1955 case XCB_MAP_REQUEST:
1956 weston_wm_handle_map_request(wm, event);
1957 break;
1958 case XCB_MAP_NOTIFY:
1959 weston_wm_handle_map_notify(wm, event);
1960 break;
1961 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001962 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001963 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001964 case XCB_REPARENT_NOTIFY:
1965 weston_wm_handle_reparent_notify(wm, event);
1966 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001967 case XCB_CONFIGURE_REQUEST:
1968 weston_wm_handle_configure_request(wm, event);
1969 break;
1970 case XCB_CONFIGURE_NOTIFY:
1971 weston_wm_handle_configure_notify(wm, event);
1972 break;
1973 case XCB_DESTROY_NOTIFY:
1974 weston_wm_handle_destroy_notify(wm, event);
1975 break;
1976 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001977 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001978 break;
1979 case XCB_PROPERTY_NOTIFY:
1980 weston_wm_handle_property_notify(wm, event);
1981 break;
1982 case XCB_CLIENT_MESSAGE:
1983 weston_wm_handle_client_message(wm, event);
1984 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03001985 case XCB_FOCUS_IN:
1986 weston_wm_handle_focus_in(wm, event);
1987 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001988 }
1989
1990 free(event);
1991 count++;
1992 }
1993
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02001994 if (count != 0)
1995 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001996
1997 return count;
1998}
1999
2000static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002001weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2002 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2003 wm->screen->root, wm->atom.net_active_window,
2004 wm->atom.window, 32, 1, &window);
2005}
2006
2007static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002008weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2009{
2010 xcb_depth_iterator_t d_iter;
2011 xcb_visualtype_iterator_t vt_iter;
2012 xcb_visualtype_t *visualtype;
2013
2014 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2015 visualtype = NULL;
2016 while (d_iter.rem > 0) {
2017 if (d_iter.data->depth == 32) {
2018 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2019 visualtype = vt_iter.data;
2020 break;
2021 }
2022
2023 xcb_depth_next(&d_iter);
2024 }
2025
2026 if (visualtype == NULL) {
2027 weston_log("no 32 bit visualtype\n");
2028 return;
2029 }
2030
2031 wm->visual_id = visualtype->visual_id;
2032 wm->colormap = xcb_generate_id(wm->conn);
2033 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2034 wm->colormap, wm->screen->root, wm->visual_id);
2035}
2036
2037static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002038weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002039{
2040
2041#define F(field) offsetof(struct weston_wm, field)
2042
2043 static const struct { const char *name; int offset; } atoms[] = {
2044 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002045 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002046 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2047 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002048 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002049 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002050 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002051 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002052 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002053 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002054 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2055 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002056 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2057 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002058 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2059 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2060 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002061 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002062 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2063
2064 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2065 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2066 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2067 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2068 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2069 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2070 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002071 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2072 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002073 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2074 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2075 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2076 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2077 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2078
2079 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2080 { "_NET_SUPPORTING_WM_CHECK",
2081 F(atom.net_supporting_wm_check) },
2082 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002083 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002084 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2085 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002086 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002087 { "TARGETS", F(atom.targets) },
2088 { "UTF8_STRING", F(atom.utf8_string) },
2089 { "_WL_SELECTION", F(atom.wl_selection) },
2090 { "INCR", F(atom.incr) },
2091 { "TIMESTAMP", F(atom.timestamp) },
2092 { "MULTIPLE", F(atom.multiple) },
2093 { "UTF8_STRING" , F(atom.utf8_string) },
2094 { "COMPOUND_TEXT", F(atom.compound_text) },
2095 { "TEXT", F(atom.text) },
2096 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002097 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002098 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2099 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002100 { "XdndSelection", F(atom.xdnd_selection) },
2101 { "XdndAware", F(atom.xdnd_aware) },
2102 { "XdndEnter", F(atom.xdnd_enter) },
2103 { "XdndLeave", F(atom.xdnd_leave) },
2104 { "XdndDrop", F(atom.xdnd_drop) },
2105 { "XdndStatus", F(atom.xdnd_status) },
2106 { "XdndFinished", F(atom.xdnd_finished) },
2107 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002108 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2109 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002110 };
2111#undef F
2112
2113 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2114 xcb_xfixes_query_version_reply_t *xfixes_reply;
2115 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2116 xcb_intern_atom_reply_t *reply;
2117 xcb_render_query_pict_formats_reply_t *formats_reply;
2118 xcb_render_query_pict_formats_cookie_t formats_cookie;
2119 xcb_render_pictforminfo_t *formats;
2120 uint32_t i;
2121
2122 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002123 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002124
2125 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2126
2127 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2128 cookies[i] = xcb_intern_atom (wm->conn, 0,
2129 strlen(atoms[i].name),
2130 atoms[i].name);
2131
2132 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2133 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2134 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2135 free(reply);
2136 }
2137
2138 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2139 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002140 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002141
2142 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2143 XCB_XFIXES_MAJOR_VERSION,
2144 XCB_XFIXES_MINOR_VERSION);
2145 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2146 xfixes_cookie, NULL);
2147
Martin Minarik6d118362012-06-07 18:01:59 +02002148 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002149 xfixes_reply->major_version, xfixes_reply->minor_version);
2150
2151 free(xfixes_reply);
2152
2153 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2154 formats_cookie, 0);
2155 if (formats_reply == NULL)
2156 return;
2157
2158 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002159 for (i = 0; i < formats_reply->num_formats; i++) {
2160 if (formats[i].direct.red_mask != 0xff &&
2161 formats[i].direct.red_shift != 16)
2162 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002163 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2164 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002165 wm->format_rgb = formats[i];
2166 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2167 formats[i].depth == 32 &&
2168 formats[i].direct.alpha_mask == 0xff &&
2169 formats[i].direct.alpha_shift == 24)
2170 wm->format_rgba = formats[i];
2171 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002172
2173 free(formats_reply);
2174}
2175
2176static void
2177weston_wm_create_wm_window(struct weston_wm *wm)
2178{
2179 static const char name[] = "Weston WM";
2180
2181 wm->wm_window = xcb_generate_id(wm->conn);
2182 xcb_create_window(wm->conn,
2183 XCB_COPY_FROM_PARENT,
2184 wm->wm_window,
2185 wm->screen->root,
2186 0, 0,
2187 10, 10,
2188 0,
2189 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2190 wm->screen->root_visual,
2191 0, NULL);
2192
2193 xcb_change_property(wm->conn,
2194 XCB_PROP_MODE_REPLACE,
2195 wm->wm_window,
2196 wm->atom.net_supporting_wm_check,
2197 XCB_ATOM_WINDOW,
2198 32, /* format */
2199 1, &wm->wm_window);
2200
2201 xcb_change_property(wm->conn,
2202 XCB_PROP_MODE_REPLACE,
2203 wm->wm_window,
2204 wm->atom.net_wm_name,
2205 wm->atom.utf8_string,
2206 8, /* format */
2207 strlen(name), name);
2208
2209 xcb_change_property(wm->conn,
2210 XCB_PROP_MODE_REPLACE,
2211 wm->screen->root,
2212 wm->atom.net_supporting_wm_check,
2213 XCB_ATOM_WINDOW,
2214 32, /* format */
2215 1, &wm->wm_window);
2216
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002217 /* Claim the WM_S0 selection even though we don't suport
2218 * the --replace functionality. */
2219 xcb_set_selection_owner(wm->conn,
2220 wm->wm_window,
2221 wm->atom.wm_s0,
2222 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002223
2224 xcb_set_selection_owner(wm->conn,
2225 wm->wm_window,
2226 wm->atom.net_wm_cm_s0,
2227 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002228}
2229
2230struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002231weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002232{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002233 struct weston_wm *wm;
2234 struct wl_event_loop *loop;
2235 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002236 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002237 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002238
Peter Huttererf3d62272013-08-08 11:57:05 +10002239 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002240 if (wm == NULL)
2241 return NULL;
2242
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002243 wm->server = wxs;
2244 wm->window_hash = hash_table_create();
2245 if (wm->window_hash == NULL) {
2246 free(wm);
2247 return NULL;
2248 }
2249
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002250 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002251 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002252 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002253 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002254 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002255 hash_table_destroy(wm->window_hash);
2256 free(wm);
2257 return NULL;
2258 }
2259
2260 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2261 wm->screen = s.data;
2262
2263 loop = wl_display_get_event_loop(wxs->wl_display);
2264 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002265 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002266 WL_EVENT_READABLE,
2267 weston_wm_handle_event, wm);
2268 wl_event_source_check(wm->source);
2269
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002270 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002271 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002272
2273 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002274 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2275 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2276 XCB_EVENT_MASK_PROPERTY_CHANGE;
2277 xcb_change_window_attributes(wm->conn, wm->screen->root,
2278 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002279
2280 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2281 XCB_COMPOSITE_REDIRECT_MANUAL);
2282
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002283 wm->theme = theme_create();
2284
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002285 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002286 supported[1] = wm->atom.net_wm_state;
2287 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002288 supported[3] = wm->atom.net_wm_state_maximized_vert;
2289 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002290 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002291 xcb_change_property(wm->conn,
2292 XCB_PROP_MODE_REPLACE,
2293 wm->screen->root,
2294 wm->atom.net_supported,
2295 XCB_ATOM_ATOM,
2296 32, /* format */
2297 ARRAY_LENGTH(supported), supported);
2298
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002299 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2300
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002301 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002302
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002303 weston_wm_dnd_init(wm);
2304
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002305 xcb_flush(wm->conn);
2306
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002307 wm->create_surface_listener.notify = weston_wm_create_surface;
2308 wl_signal_add(&wxs->compositor->create_surface_signal,
2309 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002310 wm->activate_listener.notify = weston_wm_window_activate;
2311 wl_signal_add(&wxs->compositor->activate_signal,
2312 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002313 wm->transform_listener.notify = weston_wm_window_transform;
2314 wl_signal_add(&wxs->compositor->transform_signal,
2315 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002316 wm->kill_listener.notify = weston_wm_kill_client;
2317 wl_signal_add(&wxs->compositor->kill_signal,
2318 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002319 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002320
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002321 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002322 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002323
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002324 /* Create wm window and take WM_S0 selection last, which
2325 * signals to Xwayland that we're done with setup. */
2326 weston_wm_create_wm_window(wm);
2327
2328 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002329
2330 return wm;
2331}
2332
2333void
2334weston_wm_destroy(struct weston_wm *wm)
2335{
2336 /* FIXME: Free windows in hash. */
2337 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002338 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002339 xcb_disconnect(wm->conn);
2340 wl_event_source_remove(wm->source);
2341 wl_list_remove(&wm->selection_listener.link);
2342 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002343 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002344 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002345 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002346
2347 free(wm);
2348}
2349
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002350static struct weston_wm_window *
2351get_wm_window(struct weston_surface *surface)
2352{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002353 struct wl_listener *listener;
2354
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002355 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002356 if (listener)
2357 return container_of(listener, struct weston_wm_window,
2358 surface_destroy_listener);
2359
2360 return NULL;
2361}
2362
2363static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002364weston_wm_window_configure(void *data)
2365{
2366 struct weston_wm_window *window = data;
2367 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002368 uint32_t values[4];
2369 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002370
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002371 weston_wm_window_get_child_position(window, &x, &y);
2372 values[0] = x;
2373 values[1] = y;
2374 values[2] = window->width;
2375 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002376 xcb_configure_window(wm->conn,
2377 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002378 XCB_CONFIG_WINDOW_X |
2379 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002380 XCB_CONFIG_WINDOW_WIDTH |
2381 XCB_CONFIG_WINDOW_HEIGHT,
2382 values);
2383
2384 weston_wm_window_get_frame_size(window, &width, &height);
2385 values[0] = width;
2386 values[1] = height;
2387 xcb_configure_window(wm->conn,
2388 window->frame_id,
2389 XCB_CONFIG_WINDOW_WIDTH |
2390 XCB_CONFIG_WINDOW_HEIGHT,
2391 values);
2392
2393 window->configure_source = NULL;
2394
2395 weston_wm_window_schedule_repaint(window);
2396}
2397
2398static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002399send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002400{
2401 struct weston_wm_window *window = get_wm_window(surface);
2402 struct weston_wm *wm = window->wm;
2403 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002404 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002405
Marek Chalupae9fe4672014-10-29 13:44:44 +01002406 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002407 hborder = 2 * t->width;
2408 vborder = t->titlebar_height + t->width;
2409 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002410 hborder = 0;
2411 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002412 }
2413
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002414 if (width > hborder)
2415 window->width = width - hborder;
2416 else
2417 window->width = 1;
2418
2419 if (height > vborder)
2420 window->height = height - vborder;
2421 else
2422 window->height = 1;
2423
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002424 if (window->frame)
2425 frame_resize_inside(window->frame, window->width, window->height);
2426
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002427 if (window->configure_source)
2428 return;
2429
2430 window->configure_source =
2431 wl_event_loop_add_idle(wm->server->loop,
2432 weston_wm_window_configure, window);
2433}
2434
2435static const struct weston_shell_client shell_client = {
2436 send_configure
2437};
2438
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002439static int
2440legacy_fullscreen(struct weston_wm *wm,
2441 struct weston_wm_window *window,
2442 struct weston_output **output_ret)
2443{
2444 struct weston_compositor *compositor = wm->server->compositor;
2445 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002446 uint32_t minmax = PMinSize | PMaxSize;
2447 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002448
2449 /* Heuristics for detecting legacy fullscreen windows... */
2450
2451 wl_list_for_each(output, &compositor->output_list, link) {
2452 if (output->x == window->x &&
2453 output->y == window->y &&
2454 output->width == window->width &&
2455 output->height == window->height &&
2456 window->override_redirect) {
2457 *output_ret = output;
2458 return 1;
2459 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002460
2461 matching_size = 0;
2462 if ((window->size_hints.flags & (USSize |PSize)) &&
2463 window->size_hints.width == output->width &&
2464 window->size_hints.height == output->height)
2465 matching_size = 1;
2466 if ((window->size_hints.flags & minmax) == minmax &&
2467 window->size_hints.min_width == output->width &&
2468 window->size_hints.min_height == output->height &&
2469 window->size_hints.max_width == output->width &&
2470 window->size_hints.max_height == output->height)
2471 matching_size = 1;
2472
2473 if (matching_size && !window->decorate &&
2474 (window->size_hints.flags & (USPosition | PPosition)) &&
2475 window->size_hints.x == output->x &&
2476 window->size_hints.y == output->y) {
2477 *output_ret = output;
2478 return 1;
2479 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002480 }
2481
2482 return 0;
2483}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002484
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002485static bool
2486weston_wm_window_type_inactive(struct weston_wm_window *window)
2487{
2488 struct weston_wm *wm = window->wm;
2489
2490 return window->type == wm->atom.net_wm_window_type_tooltip ||
2491 window->type == wm->atom.net_wm_window_type_dropdown ||
2492 window->type == wm->atom.net_wm_window_type_dnd ||
2493 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002494 window->type == wm->atom.net_wm_window_type_popup ||
2495 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002496}
2497
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002498static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002499xserver_map_shell_surface(struct weston_wm_window *window,
2500 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002501{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002502 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002503 struct weston_shell_interface *shell_interface =
2504 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002505 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002506 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002507 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002508
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002509 weston_wm_window_read_properties(window);
2510
2511 /* A weston_wm_window may have many different surfaces assigned
2512 * throughout its life, so we must make sure to remove the listener
2513 * from the old surface signal list. */
2514 if (window->surface)
2515 wl_list_remove(&window->surface_destroy_listener.link);
2516
2517 window->surface = surface;
2518 window->surface_destroy_listener.notify = surface_destroy;
2519 wl_signal_add(&window->surface->destroy_signal,
2520 &window->surface_destroy_listener);
2521
2522 weston_wm_window_schedule_repaint(window);
2523
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002524 if (!shell_interface->create_shell_surface)
2525 return;
2526
Jason Ekstranda7af7042013-10-12 22:38:11 -05002527 if (!shell_interface->get_primary_view)
2528 return;
2529
Pekka Paalanen50b67472014-10-01 15:02:41 +03002530 if (window->surface->configure) {
2531 weston_log("warning, unexpected in %s: "
2532 "surface's configure hook is already set.\n",
2533 __func__);
2534 return;
2535 }
2536
Murray Calavera883ac022015-06-06 13:02:22 +00002537 window->shsurf =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002538 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002539 window->surface,
2540 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002541 window->view = shell_interface->get_primary_view(shell_interface->shell,
2542 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002543
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002544 if (window->name)
2545 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002546 if (window->pid > 0)
2547 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002548
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002549 if (window->fullscreen) {
2550 window->saved_width = window->width;
2551 window->saved_height = window->height;
2552 shell_interface->set_fullscreen(window->shsurf,
2553 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2554 0, NULL);
2555 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002556 } else if (legacy_fullscreen(wm, window, &output)) {
2557 window->fullscreen = 1;
2558 shell_interface->set_fullscreen(window->shsurf,
2559 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2560 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002561 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002562 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002563 window->x,
2564 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002565 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002566 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002567 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002568 if (weston_wm_window_type_inactive(window))
2569 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002570 shell_interface->set_transient(window->shsurf,
2571 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002572 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002573 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002574 } else if (weston_wm_window_is_maximized(window)) {
2575 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002576 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002577 if (weston_wm_window_type_inactive(window)) {
2578 shell_interface->set_xwayland(window->shsurf,
2579 window->x,
2580 window->y,
2581 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2582 } else {
2583 shell_interface->set_toplevel(window->shsurf);
2584 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002585 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002586}