blob: 49d974e23677aee7bd735a3582b6b8a4f38fa9e1 [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;
135 struct wl_listener surface_destroy_listener;
136 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400137 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400138 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300139 int pid;
140 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400141 char *class;
142 char *name;
143 struct weston_wm_window *transient_for;
144 uint32_t protocols;
145 xcb_atom_t type;
146 int width, height;
147 int x, y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200148 bool pos_dirty;
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;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200696 window->pos_dirty = false;
697
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700698 if (window->override_redirect) {
699 window->width = configure_notify->width;
700 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500701 if (window->frame)
702 frame_resize_inside(window->frame,
703 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700704 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400705}
706
707static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300708weston_wm_kill_client(struct wl_listener *listener, void *data)
709{
710 struct weston_surface *surface = data;
711 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300712 if (!window)
713 return;
714
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200715 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300716 kill(window->pid, SIGKILL);
717}
718
719static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700720weston_wm_create_surface(struct wl_listener *listener, void *data)
721{
722 struct weston_surface *surface = data;
723 struct weston_wm *wm =
724 container_of(listener,
725 struct weston_wm, create_surface_listener);
726 struct weston_wm_window *window;
727
728 if (wl_resource_get_client(surface->resource) != wm->server->client)
729 return;
730
731 wl_list_for_each(window, &wm->unpaired_window_list, link)
732 if (window->surface_id ==
733 wl_resource_get_id(surface->resource)) {
734 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700735 window->surface_id = 0;
736 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700737 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000738 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700739}
740
741static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300742weston_wm_send_focus_window(struct weston_wm *wm,
743 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400744{
Scott Moreau85ecac02012-05-21 15:49:14 -0600745 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400746
Scott Moreau85ecac02012-05-21 15:49:14 -0600747 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700748 uint32_t values[1];
749
Boyan Dingb9f863c2014-08-30 10:33:23 +0800750 if (window->override_redirect)
751 return;
752
Scott Moreau85ecac02012-05-21 15:49:14 -0600753 client_message.response_type = XCB_CLIENT_MESSAGE;
754 client_message.format = 32;
755 client_message.window = window->id;
756 client_message.type = wm->atom.wm_protocols;
757 client_message.data.data32[0] = wm->atom.wm_take_focus;
758 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
759
Murray Calavera883ac022015-06-06 13:02:22 +0000760 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600761 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
762 (char *) &client_message);
763
764 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
765 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700766
767 values[0] = XCB_STACK_MODE_ABOVE;
768 xcb_configure_window (wm->conn, window->frame_id,
769 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600770 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400771 xcb_set_input_focus (wm->conn,
772 XCB_INPUT_FOCUS_POINTER_ROOT,
773 XCB_NONE,
774 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600775 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300776}
777
778static void
779weston_wm_window_activate(struct wl_listener *listener, void *data)
780{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800781 struct weston_surface_activation_data *activation_data = data;
782 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300783 struct weston_wm_window *window = NULL;
784 struct weston_wm *wm =
785 container_of(listener, struct weston_wm, activate_listener);
786
787 if (surface) {
788 window = get_wm_window(surface);
789 }
790
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200791 if (window) {
792 weston_wm_set_net_active_window(wm, window->id);
793 } else {
794 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
795 }
796
Giulio Camuffob18f7882015-03-29 14:20:23 +0300797 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400798
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500799 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800800 if (wm->focus_window->frame)
801 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400802 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500803 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400804 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500805 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800806 if (wm->focus_window->frame)
807 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400808 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500809 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200810
811 xcb_flush(wm->conn);
812
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400813}
814
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400815#define ICCCM_WITHDRAWN_STATE 0
816#define ICCCM_NORMAL_STATE 1
817#define ICCCM_ICONIC_STATE 3
818
819static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500820weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400821{
822 struct weston_wm *wm = window->wm;
823 uint32_t property[2];
824
825 property[0] = state;
826 property[1] = XCB_WINDOW_NONE;
827
828 xcb_change_property(wm->conn,
829 XCB_PROP_MODE_REPLACE,
830 window->id,
831 wm->atom.wm_state,
832 wm->atom.wm_state,
833 32, /* format */
834 2, property);
835}
836
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400837static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500838weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
839{
840 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200841 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500842 int i;
843
844 i = 0;
845 if (window->fullscreen)
846 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200847 if (window->maximized_vert)
848 property[i++] = wm->atom.net_wm_state_maximized_vert;
849 if (window->maximized_horz)
850 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500851
852 xcb_change_property(wm->conn,
853 XCB_PROP_MODE_REPLACE,
854 window->id,
855 wm->atom.net_wm_state,
856 XCB_ATOM_ATOM,
857 32, /* format */
858 i, property);
859}
860
861static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700862weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400863{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700864 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400865 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400866 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200867 int buttons = FRAME_BUTTON_CLOSE;
868
869 if (window->decorate & MWM_DECOR_MAXIMIZE)
870 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400871
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500872 window->frame = frame_create(window->wm->theme,
873 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200874 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500875 frame_resize_inside(window->frame, window->width, window->height);
876
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400877 weston_wm_window_get_frame_size(window, &width, &height);
878 weston_wm_window_get_child_position(window, &x, &y);
879
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400880 values[0] = wm->screen->black_pixel;
881 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400882 XCB_EVENT_MASK_KEY_PRESS |
883 XCB_EVENT_MASK_KEY_RELEASE |
884 XCB_EVENT_MASK_BUTTON_PRESS |
885 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400886 XCB_EVENT_MASK_POINTER_MOTION |
887 XCB_EVENT_MASK_ENTER_WINDOW |
888 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400889 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400890 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400891 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400892
893 window->frame_id = xcb_generate_id(wm->conn);
894 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400895 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400896 window->frame_id,
897 wm->screen->root,
898 0, 0,
899 width, height,
900 0,
901 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400902 wm->visual_id,
903 XCB_CW_BORDER_PIXEL |
904 XCB_CW_EVENT_MASK |
905 XCB_CW_COLORMAP, values);
906
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400907 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
908
909 values[0] = 0;
910 xcb_configure_window(wm->conn, window->id,
911 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
912
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400913 window->cairo_surface =
914 cairo_xcb_surface_create_with_xrender_format(wm->conn,
915 wm->screen,
916 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400917 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400918 width, height);
919
920 hash_table_insert(wm->window_hash, window->frame_id, window);
921}
922
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200923/*
924 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
925 * Passing a <0 desktop value deletes the property.
926 */
927static void
928weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700929 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200930{
931 if (desktop >= 0) {
932 xcb_change_property(window->wm->conn,
933 XCB_PROP_MODE_REPLACE,
934 window->id,
935 window->wm->atom.net_wm_desktop,
936 XCB_ATOM_CARDINAL,
937 32, /* format */
938 1, &desktop);
939 } else {
940 xcb_delete_property(window->wm->conn,
941 window->id,
942 window->wm->atom.net_wm_desktop);
943 }
944}
945
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400946static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700947weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
948{
949 xcb_map_request_event_t *map_request =
950 (xcb_map_request_event_t *) event;
951 struct weston_wm_window *window;
952
953 if (our_resource(wm, map_request->window)) {
954 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
955 map_request->window);
956 return;
957 }
958
Derek Foreman49372142015-04-09 10:51:22 -0500959 if (!wm_lookup_window(wm, map_request->window, &window))
960 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700961
962 weston_wm_window_read_properties(window);
963
964 if (window->frame_id == XCB_WINDOW_NONE)
965 weston_wm_window_create_frame(window);
966
967 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
968 window->id, window, window->frame_id);
969
970 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
971 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200972 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700973
974 xcb_map_window(wm->conn, map_request->window);
975 xcb_map_window(wm->conn, window->frame_id);
976}
977
978static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400979weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
980{
981 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
982
983 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400984 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
985 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400986 return;
987 }
988
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400989 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400990}
991
992static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400993weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
994{
995 xcb_unmap_notify_event_t *unmap_notify =
996 (xcb_unmap_notify_event_t *) event;
997 struct weston_wm_window *window;
998
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400999 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1000 unmap_notify->window,
1001 unmap_notify->event,
1002 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001003
1004 if (our_resource(wm, unmap_notify->window))
1005 return;
1006
MoD31700122013-06-11 19:58:55 -05001007 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001008 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1009 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001010 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001011
Derek Foreman49372142015-04-09 10:51:22 -05001012 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1013 return;
1014
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001015 if (window->surface_id) {
1016 /* Make sure we're not on the unpaired surface list or we
1017 * could be assigned a surface during surface creation that
1018 * was mapped before this unmap request.
1019 */
1020 wl_list_remove(&window->link);
1021 window->surface_id = 0;
1022 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001023 if (wm->focus_window == window)
1024 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001025 if (window->surface)
1026 wl_list_remove(&window->surface_destroy_listener.link);
1027 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001028 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001029
1030 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1031 weston_wm_window_set_virtual_desktop(window, -1);
1032
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001033 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001034}
1035
1036static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001037weston_wm_window_draw_decoration(void *data)
1038{
1039 struct weston_wm_window *window = data;
1040 struct weston_wm *wm = window->wm;
1041 struct theme *t = wm->theme;
1042 cairo_t *cr;
1043 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001044 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001045 struct weston_shell_interface *shell_interface =
1046 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001047 uint32_t flags = 0;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001048 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001049
1050 weston_wm_window_read_properties(window);
1051
1052 window->repaint_source = NULL;
1053
1054 weston_wm_window_get_frame_size(window, &width, &height);
1055 weston_wm_window_get_child_position(window, &x, &y);
1056
1057 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1058 cr = cairo_create(window->cairo_surface);
1059
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001060 if (window->fullscreen) {
1061 /* nothing */
1062 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001063 if (wm->focus_window == window)
1064 flags |= THEME_FRAME_ACTIVE;
1065
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001066 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001067 } else {
1068 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1069 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1070 cairo_paint(cr);
1071
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001072 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001073 }
1074
1075 cairo_destroy(cr);
1076
1077 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001078 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001079 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001080 pixman_region32_init(&window->surface->pending.opaque);
1081 } else {
1082 /* We leave an extra pixel around the X window area to
1083 * make sure we don't sample from the undefined alpha
1084 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001085 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001086 x - 1, y - 1,
1087 window->width + 2,
1088 window->height + 2);
1089 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001090 wl_list_for_each(view, &window->surface->views, surface_link)
1091 weston_view_geometry_dirty(view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001092
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001093 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001094
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001095 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001096 frame_input_rect(window->frame, &input_x, &input_y,
1097 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001098 } else {
1099 input_x = x;
1100 input_y = y;
1101 input_w = width;
1102 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001103 }
1104
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001105 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001106 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001107
1108 shell_interface->set_window_geometry(window->shsurf,
1109 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001110 }
1111}
1112
1113static void
1114weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1115{
1116 struct weston_wm *wm = window->wm;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001117 struct weston_view *view;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001118 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001119
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001120 if (window->frame_id == XCB_WINDOW_NONE) {
1121 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001122 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001123 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001124 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001125 pixman_region32_init(&window->surface->pending.opaque);
1126 } else {
1127 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1128 width, height);
1129 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001130 wl_list_for_each(view, &window->surface->views, surface_link)
1131 weston_view_geometry_dirty(view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001132 }
1133 return;
1134 }
1135
1136 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001137 return;
1138
1139 window->repaint_source =
1140 wl_event_loop_add_idle(wm->server->loop,
1141 weston_wm_window_draw_decoration,
1142 window);
1143}
1144
1145static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001146weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1147{
1148 xcb_property_notify_event_t *property_notify =
1149 (xcb_property_notify_event_t *) event;
1150 struct weston_wm_window *window;
1151
Derek Foreman49372142015-04-09 10:51:22 -05001152 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001153 return;
1154
1155 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001156
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001157 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001158 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001159 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001160 else
1161 read_and_dump_property(wm, property_notify->window,
1162 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001163
1164 if (property_notify->atom == wm->atom.net_wm_name ||
1165 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001166 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001167}
1168
1169static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001170weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001171 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001172{
1173 struct weston_wm_window *window;
1174 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001175 xcb_get_geometry_cookie_t geometry_cookie;
1176 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001177
Peter Huttererf3d62272013-08-08 11:57:05 +10001178 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001179 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001180 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001181 return;
1182 }
1183
MoD384a11a2013-06-22 11:04:21 -05001184 geometry_cookie = xcb_get_geometry(wm->conn, id);
1185
Giulio Camuffob18f7882015-03-29 14:20:23 +03001186 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1187 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001188 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1189
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001190 window->wm = wm;
1191 window->id = id;
1192 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001193 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001194 window->width = width;
1195 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001196 window->x = x;
1197 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001198 window->pos_dirty = false;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001199
MoD384a11a2013-06-22 11:04:21 -05001200 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1201 /* technically we should use XRender and check the visual format's
1202 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001203 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001204 window->has_alpha = geometry_reply->depth == 32;
1205 free(geometry_reply);
1206
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001207 hash_table_insert(wm->window_hash, id, window);
1208}
1209
1210static void
1211weston_wm_window_destroy(struct weston_wm_window *window)
1212{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001213 struct weston_wm *wm = window->wm;
1214
1215 if (window->repaint_source)
1216 wl_event_source_remove(window->repaint_source);
1217 if (window->cairo_surface)
1218 cairo_surface_destroy(window->cairo_surface);
1219
1220 if (window->frame_id) {
1221 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1222 xcb_destroy_window(wm->conn, window->frame_id);
1223 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001224 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001225 hash_table_remove(wm->window_hash, window->frame_id);
1226 window->frame_id = XCB_WINDOW_NONE;
1227 }
1228
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001229 if (window->surface_id)
1230 wl_list_remove(&window->link);
1231
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001232 if (window->surface)
1233 wl_list_remove(&window->surface_destroy_listener.link);
1234
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001235 hash_table_remove(window->wm->window_hash, window->id);
1236 free(window);
1237}
1238
1239static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001240weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1241{
1242 xcb_create_notify_event_t *create_notify =
1243 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001244
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001245 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1246 create_notify->window,
1247 create_notify->width, create_notify->height,
1248 create_notify->override_redirect ? ", override" : "",
1249 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001250
1251 if (our_resource(wm, create_notify->window))
1252 return;
1253
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001254 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001255 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001256 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001257 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001258}
1259
1260static void
1261weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1262{
1263 xcb_destroy_notify_event_t *destroy_notify =
1264 (xcb_destroy_notify_event_t *) event;
1265 struct weston_wm_window *window;
1266
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001267 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1268 destroy_notify->window,
1269 destroy_notify->event,
1270 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001271
1272 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001273 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001274
Derek Foreman49372142015-04-09 10:51:22 -05001275 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1276 return;
1277
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001278 weston_wm_window_destroy(window);
1279}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001280
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001281static void
1282weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1283{
1284 xcb_reparent_notify_event_t *reparent_notify =
1285 (xcb_reparent_notify_event_t *) event;
1286 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001287
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001288 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1289 reparent_notify->window,
1290 reparent_notify->parent,
1291 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001292
1293 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001294 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001295 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001296 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001297 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001298 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1299 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001300 weston_wm_window_destroy(window);
1301 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001302}
1303
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001304struct weston_seat *
1305weston_wm_pick_seat(struct weston_wm *wm)
1306{
1307 return container_of(wm->server->compositor->seat_list.next,
1308 struct weston_seat, link);
1309}
1310
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001311static struct weston_seat *
1312weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1313{
1314 struct weston_wm *wm = window->wm;
1315 struct weston_seat *seat, *s;
1316
1317 seat = NULL;
1318 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001319 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1320 struct weston_pointer *old_pointer =
1321 weston_seat_get_pointer(seat);
1322
1323 if (pointer && pointer->focus &&
1324 pointer->focus->surface == window->surface &&
1325 pointer->button_count > 0 &&
1326 (!seat ||
1327 pointer->grab_serial -
1328 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001329 seat = s;
1330 }
1331
1332 return seat;
1333}
1334
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001335static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001336weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1337 xcb_client_message_event_t *client_message)
1338{
1339 static const int map[] = {
1340 THEME_LOCATION_RESIZING_TOP_LEFT,
1341 THEME_LOCATION_RESIZING_TOP,
1342 THEME_LOCATION_RESIZING_TOP_RIGHT,
1343 THEME_LOCATION_RESIZING_RIGHT,
1344 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1345 THEME_LOCATION_RESIZING_BOTTOM,
1346 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1347 THEME_LOCATION_RESIZING_LEFT
1348 };
1349
1350 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001351 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001352 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001353 int detail;
1354 struct weston_shell_interface *shell_interface =
1355 &wm->server->compositor->shell_interface;
1356
Derek Foreman1281a362015-07-31 16:55:32 -05001357 if (!pointer || pointer->button_count != 1
1358 || !pointer->focus
1359 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001360 return;
1361
1362 detail = client_message->data.data32[2];
1363 switch (detail) {
1364 case _NET_WM_MOVERESIZE_MOVE:
Derek Foremane4d6c832015-08-05 14:48:11 -05001365 shell_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001366 break;
1367 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1368 case _NET_WM_MOVERESIZE_SIZE_TOP:
1369 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1370 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1371 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1372 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1373 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1374 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Derek Foremane4d6c832015-08-05 14:48:11 -05001375 shell_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001376 break;
1377 case _NET_WM_MOVERESIZE_CANCEL:
1378 break;
1379 }
1380}
1381
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001382#define _NET_WM_STATE_REMOVE 0
1383#define _NET_WM_STATE_ADD 1
1384#define _NET_WM_STATE_TOGGLE 2
1385
1386static int
1387update_state(int action, int *state)
1388{
1389 int new_state, changed;
1390
1391 switch (action) {
1392 case _NET_WM_STATE_REMOVE:
1393 new_state = 0;
1394 break;
1395 case _NET_WM_STATE_ADD:
1396 new_state = 1;
1397 break;
1398 case _NET_WM_STATE_TOGGLE:
1399 new_state = !*state;
1400 break;
1401 default:
1402 return 0;
1403 }
1404
1405 changed = (*state != new_state);
1406 *state = new_state;
1407
1408 return changed;
1409}
1410
1411static void
1412weston_wm_window_configure(void *data);
1413
1414static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001415weston_wm_window_set_toplevel(struct weston_wm_window *window)
1416{
1417 struct weston_shell_interface *shell_interface =
1418 &window->wm->server->compositor->shell_interface;
1419
1420 shell_interface->set_toplevel(window->shsurf);
1421 window->width = window->saved_width;
1422 window->height = window->saved_height;
1423 if (window->frame)
1424 frame_resize_inside(window->frame,
1425 window->width,
1426 window->height);
1427 weston_wm_window_configure(window);
1428}
1429
1430static inline bool
1431weston_wm_window_is_maximized(struct weston_wm_window *window)
1432{
1433 return window->maximized_horz && window->maximized_vert;
1434}
1435
1436static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001437weston_wm_window_handle_state(struct weston_wm_window *window,
1438 xcb_client_message_event_t *client_message)
1439{
1440 struct weston_wm *wm = window->wm;
1441 struct weston_shell_interface *shell_interface =
1442 &wm->server->compositor->shell_interface;
1443 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001444 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001445
1446 action = client_message->data.data32[0];
1447 property = client_message->data.data32[1];
1448
1449 if (property == wm->atom.net_wm_state_fullscreen &&
1450 update_state(action, &window->fullscreen)) {
1451 weston_wm_window_set_net_wm_state(window);
1452 if (window->fullscreen) {
1453 window->saved_width = window->width;
1454 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001455
1456 if (window->shsurf)
1457 shell_interface->set_fullscreen(window->shsurf,
1458 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1459 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001460 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001461 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001462 weston_wm_window_set_toplevel(window);
1463 }
1464 } else {
1465 if (property == wm->atom.net_wm_state_maximized_vert &&
1466 update_state(action, &window->maximized_vert))
1467 weston_wm_window_set_net_wm_state(window);
1468 if (property == wm->atom.net_wm_state_maximized_horz &&
1469 update_state(action, &window->maximized_horz))
1470 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001471
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001472 if (maximized != weston_wm_window_is_maximized(window)) {
1473 if (weston_wm_window_is_maximized(window)) {
1474 window->saved_width = window->width;
1475 window->saved_height = window->height;
1476
1477 if (window->shsurf)
1478 shell_interface->set_maximized(window->shsurf);
1479 } else if (window->shsurf) {
1480 weston_wm_window_set_toplevel(window);
1481 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001482 }
1483 }
1484}
1485
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001486static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001487surface_destroy(struct wl_listener *listener, void *data)
1488{
1489 struct weston_wm_window *window =
1490 container_of(listener,
1491 struct weston_wm_window, surface_destroy_listener);
1492
1493 wm_log("surface for xid %d destroyed\n", window->id);
1494
1495 /* This should have been freed by the shell.
1496 * Don't try to use it later. */
1497 window->shsurf = NULL;
1498 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001499}
1500
1501static void
1502weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1503 xcb_client_message_event_t *client_message)
1504{
1505 struct weston_wm *wm = window->wm;
1506 struct wl_resource *resource;
1507
1508 if (window->surface_id != 0) {
1509 wm_log("already have surface id for window %d\n", window->id);
1510 return;
1511 }
1512
1513 /* Xwayland will send the wayland requests to create the
1514 * wl_surface before sending this client message. Even so, we
1515 * can end up handling the X event before the wayland requests
1516 * and thus when we try to look up the surface ID, the surface
1517 * hasn't been created yet. In that case put the window on
1518 * the unpaired window list and continue when the surface gets
1519 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001520 uint32_t id = client_message->data.data32[0];
1521 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001522 if (resource) {
1523 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001524 xserver_map_shell_surface(window,
1525 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001526 }
1527 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001528 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001529 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001530 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001531}
1532
1533static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001534weston_wm_handle_client_message(struct weston_wm *wm,
1535 xcb_generic_event_t *event)
1536{
1537 xcb_client_message_event_t *client_message =
1538 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001539 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001540
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001541 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1542 get_atom_name(wm->conn, client_message->type),
1543 client_message->data.data32[0],
1544 client_message->data.data32[1],
1545 client_message->data.data32[2],
1546 client_message->data.data32[3],
1547 client_message->data.data32[4],
1548 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001549
Jason Ekstrand0250a742014-07-24 13:17:47 -07001550 /* The window may get created and destroyed before we actually
1551 * handle the message. If it doesn't exist, bail.
1552 */
Derek Foreman49372142015-04-09 10:51:22 -05001553 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001554 return;
1555
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001556 if (client_message->type == wm->atom.net_wm_moveresize)
1557 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001558 else if (client_message->type == wm->atom.net_wm_state)
1559 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001560 else if (client_message->type == wm->atom.wl_surface_id)
1561 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001562}
1563
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001564enum cursor_type {
1565 XWM_CURSOR_TOP,
1566 XWM_CURSOR_BOTTOM,
1567 XWM_CURSOR_LEFT,
1568 XWM_CURSOR_RIGHT,
1569 XWM_CURSOR_TOP_LEFT,
1570 XWM_CURSOR_TOP_RIGHT,
1571 XWM_CURSOR_BOTTOM_LEFT,
1572 XWM_CURSOR_BOTTOM_RIGHT,
1573 XWM_CURSOR_LEFT_PTR,
1574};
1575
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001576/*
1577 * The following correspondences between file names and cursors was copied
1578 * from: https://bugs.kde.org/attachment.cgi?id=67313
1579 */
1580
1581static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001582 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001583 "sw-resize",
1584 "size_bdiag"
1585};
1586
1587static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001588 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001589 "se-resize",
1590 "size_fdiag"
1591};
1592
1593static const char *bottom_sides[] = {
1594 "bottom_side",
1595 "s-resize",
1596 "size_ver"
1597};
1598
1599static const char *left_ptrs[] = {
1600 "left_ptr",
1601 "default",
1602 "top_left_arrow",
1603 "left-arrow"
1604};
1605
1606static const char *left_sides[] = {
1607 "left_side",
1608 "w-resize",
1609 "size_hor"
1610};
1611
1612static const char *right_sides[] = {
1613 "right_side",
1614 "e-resize",
1615 "size_hor"
1616};
1617
1618static const char *top_left_corners[] = {
1619 "top_left_corner",
1620 "nw-resize",
1621 "size_fdiag"
1622};
1623
1624static const char *top_right_corners[] = {
1625 "top_right_corner",
1626 "ne-resize",
1627 "size_bdiag"
1628};
1629
1630static const char *top_sides[] = {
1631 "top_side",
1632 "n-resize",
1633 "size_ver"
1634};
1635
1636struct cursor_alternatives {
1637 const char **names;
1638 size_t count;
1639};
1640
1641static const struct cursor_alternatives cursors[] = {
1642 {top_sides, ARRAY_LENGTH(top_sides)},
1643 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1644 {left_sides, ARRAY_LENGTH(left_sides)},
1645 {right_sides, ARRAY_LENGTH(right_sides)},
1646 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1647 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1648 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1649 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1650 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001651};
1652
1653static void
1654weston_wm_create_cursors(struct weston_wm *wm)
1655{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001656 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001657 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001658 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001659
1660 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1661 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001662 for (j = 0; j < cursors[i].count; j++) {
1663 name = cursors[i].names[j];
1664 wm->cursors[i] =
1665 xcb_cursor_library_load_cursor(wm, name);
1666 if (wm->cursors[i] != (xcb_cursor_t)-1)
1667 break;
1668 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001669 }
1670
1671 wm->last_cursor = -1;
1672}
1673
1674static void
1675weston_wm_destroy_cursors(struct weston_wm *wm)
1676{
1677 uint8_t i;
1678
1679 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1680 xcb_free_cursor(wm->conn, wm->cursors[i]);
1681
1682 free(wm->cursors);
1683}
1684
1685static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001686get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001687{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001688 switch (location) {
1689 case THEME_LOCATION_RESIZING_TOP:
1690 return XWM_CURSOR_TOP;
1691 case THEME_LOCATION_RESIZING_BOTTOM:
1692 return XWM_CURSOR_BOTTOM;
1693 case THEME_LOCATION_RESIZING_LEFT:
1694 return XWM_CURSOR_LEFT;
1695 case THEME_LOCATION_RESIZING_RIGHT:
1696 return XWM_CURSOR_RIGHT;
1697 case THEME_LOCATION_RESIZING_TOP_LEFT:
1698 return XWM_CURSOR_TOP_LEFT;
1699 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1700 return XWM_CURSOR_TOP_RIGHT;
1701 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1702 return XWM_CURSOR_BOTTOM_LEFT;
1703 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1704 return XWM_CURSOR_BOTTOM_RIGHT;
1705 case THEME_LOCATION_EXTERIOR:
1706 case THEME_LOCATION_TITLEBAR:
1707 default:
1708 return XWM_CURSOR_LEFT_PTR;
1709 }
1710}
1711
1712static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001713weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1714 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001715{
1716 uint32_t cursor_value_list;
1717
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001718 if (wm->last_cursor == cursor)
1719 return;
1720
1721 wm->last_cursor = cursor;
1722
1723 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001724 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001725 XCB_CW_CURSOR, &cursor_value_list);
1726 xcb_flush(wm->conn);
1727}
1728
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001729static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001730weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001731{
1732 xcb_client_message_event_t client_message;
1733
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001734 if (window->delete_window) {
1735 client_message.response_type = XCB_CLIENT_MESSAGE;
1736 client_message.format = 32;
1737 client_message.window = window->id;
1738 client_message.type = window->wm->atom.wm_protocols;
1739 client_message.data.data32[0] =
1740 window->wm->atom.wm_delete_window;
1741 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001742
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001743 xcb_send_event(window->wm->conn, 0, window->id,
1744 XCB_EVENT_MASK_NO_EVENT,
1745 (char *) &client_message);
1746 } else {
1747 xcb_kill_client(window->wm->conn, window->id);
1748 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001749}
1750
1751static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001752weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1753{
1754 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1755 struct weston_shell_interface *shell_interface =
1756 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001757 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001758 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001759 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001760 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001761 enum frame_button_state button_state;
1762 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001763
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001764 wm_log("XCB_BUTTON_%s (detail %d)\n",
1765 button->response_type == XCB_BUTTON_PRESS ?
1766 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001767
Derek Foreman49372142015-04-09 10:51:22 -05001768 if (!wm_lookup_window(wm, button->event, &window) ||
1769 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001770 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001771
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001772 if (button->detail != 1 && button->detail != 2)
1773 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001774
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001775 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001776 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001777
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001778 button_state = button->response_type == XCB_BUTTON_PRESS ?
1779 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1780 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1781
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001782 /* Make sure we're looking at the right location. The frame
1783 * could have received a motion event from a pointer from a
1784 * different wl_seat, but under X it looks like our core
1785 * pointer moved. Move the frame pointer to the button press
1786 * location before deciding what to do. */
1787 location = frame_pointer_motion(window->frame, NULL,
1788 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001789 location = frame_pointer_button(window->frame, NULL,
1790 button_id, button_state);
1791 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1792 weston_wm_window_schedule_repaint(window);
1793
1794 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001795 if (pointer)
1796 shell_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001797 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1798 }
1799
1800 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001801 if (pointer)
1802 shell_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001803 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1804 }
1805
1806 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001807 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001808 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001809 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001810
1811 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1812 window->maximized_horz = !window->maximized_horz;
1813 window->maximized_vert = !window->maximized_vert;
1814 if (weston_wm_window_is_maximized(window)) {
1815 window->saved_width = window->width;
1816 window->saved_height = window->height;
1817 shell_interface->set_maximized(window->shsurf);
1818 } else {
1819 weston_wm_window_set_toplevel(window);
1820 }
1821 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1822 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001823}
1824
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001825static void
1826weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1827{
1828 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1829 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001830 enum theme_location location;
1831 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001832
Derek Foreman49372142015-04-09 10:51:22 -05001833 if (!wm_lookup_window(wm, motion->event, &window) ||
1834 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001835 return;
1836
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001837 location = frame_pointer_motion(window->frame, NULL,
1838 motion->event_x, motion->event_y);
1839 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1840 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001841
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001842 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001843 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001844}
1845
1846static void
1847weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1848{
1849 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1850 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001851 enum theme_location location;
1852 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001853
Derek Foreman49372142015-04-09 10:51:22 -05001854 if (!wm_lookup_window(wm, enter->event, &window) ||
1855 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001856 return;
1857
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001858 location = frame_pointer_enter(window->frame, NULL,
1859 enter->event_x, enter->event_y);
1860 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1861 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001862
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001863 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001864 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001865}
1866
1867static void
1868weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1869{
1870 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1871 struct weston_wm_window *window;
1872
Derek Foreman49372142015-04-09 10:51:22 -05001873 if (!wm_lookup_window(wm, leave->event, &window) ||
1874 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001875 return;
1876
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001877 frame_pointer_leave(window->frame, NULL);
1878 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1879 weston_wm_window_schedule_repaint(window);
1880
Tiago Vignattic1903232012-07-16 12:15:37 -04001881 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001882}
1883
Giulio Camuffob18f7882015-03-29 14:20:23 +03001884static void
1885weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1886{
1887 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1888 /* Do not let X clients change the focus behind the compositor's
1889 * back. Reset the focus to the old one if it changed. */
1890 if (!wm->focus_window || focus->event != wm->focus_window->id)
1891 weston_wm_send_focus_window(wm, wm->focus_window);
1892}
1893
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001894static int
1895weston_wm_handle_event(int fd, uint32_t mask, void *data)
1896{
1897 struct weston_wm *wm = data;
1898 xcb_generic_event_t *event;
1899 int count = 0;
1900
1901 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1902 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001903 free(event);
1904 count++;
1905 continue;
1906 }
1907
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001908 if (weston_wm_handle_dnd_event(wm, event)) {
1909 free(event);
1910 count++;
1911 continue;
1912 }
1913
MoD31700122013-06-11 19:58:55 -05001914 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001915 case XCB_BUTTON_PRESS:
1916 case XCB_BUTTON_RELEASE:
1917 weston_wm_handle_button(wm, event);
1918 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001919 case XCB_ENTER_NOTIFY:
1920 weston_wm_handle_enter(wm, event);
1921 break;
1922 case XCB_LEAVE_NOTIFY:
1923 weston_wm_handle_leave(wm, event);
1924 break;
1925 case XCB_MOTION_NOTIFY:
1926 weston_wm_handle_motion(wm, event);
1927 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001928 case XCB_CREATE_NOTIFY:
1929 weston_wm_handle_create_notify(wm, event);
1930 break;
1931 case XCB_MAP_REQUEST:
1932 weston_wm_handle_map_request(wm, event);
1933 break;
1934 case XCB_MAP_NOTIFY:
1935 weston_wm_handle_map_notify(wm, event);
1936 break;
1937 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001938 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001939 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001940 case XCB_REPARENT_NOTIFY:
1941 weston_wm_handle_reparent_notify(wm, event);
1942 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001943 case XCB_CONFIGURE_REQUEST:
1944 weston_wm_handle_configure_request(wm, event);
1945 break;
1946 case XCB_CONFIGURE_NOTIFY:
1947 weston_wm_handle_configure_notify(wm, event);
1948 break;
1949 case XCB_DESTROY_NOTIFY:
1950 weston_wm_handle_destroy_notify(wm, event);
1951 break;
1952 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001953 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001954 break;
1955 case XCB_PROPERTY_NOTIFY:
1956 weston_wm_handle_property_notify(wm, event);
1957 break;
1958 case XCB_CLIENT_MESSAGE:
1959 weston_wm_handle_client_message(wm, event);
1960 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03001961 case XCB_FOCUS_IN:
1962 weston_wm_handle_focus_in(wm, event);
1963 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001964 }
1965
1966 free(event);
1967 count++;
1968 }
1969
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02001970 if (count != 0)
1971 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001972
1973 return count;
1974}
1975
1976static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02001977weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
1978 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
1979 wm->screen->root, wm->atom.net_active_window,
1980 wm->atom.window, 32, 1, &window);
1981}
1982
1983static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001984weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1985{
1986 xcb_depth_iterator_t d_iter;
1987 xcb_visualtype_iterator_t vt_iter;
1988 xcb_visualtype_t *visualtype;
1989
1990 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1991 visualtype = NULL;
1992 while (d_iter.rem > 0) {
1993 if (d_iter.data->depth == 32) {
1994 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1995 visualtype = vt_iter.data;
1996 break;
1997 }
1998
1999 xcb_depth_next(&d_iter);
2000 }
2001
2002 if (visualtype == NULL) {
2003 weston_log("no 32 bit visualtype\n");
2004 return;
2005 }
2006
2007 wm->visual_id = visualtype->visual_id;
2008 wm->colormap = xcb_generate_id(wm->conn);
2009 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2010 wm->colormap, wm->screen->root, wm->visual_id);
2011}
2012
2013static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002014weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002015{
2016
2017#define F(field) offsetof(struct weston_wm, field)
2018
2019 static const struct { const char *name; int offset; } atoms[] = {
2020 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002021 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002022 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2023 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002024 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002025 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002026 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002027 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002028 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002029 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002030 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2031 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002032 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2033 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002034 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2035 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2036 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002037 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002038 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2039
2040 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2041 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2042 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2043 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2044 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2045 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2046 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002047 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2048 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002049 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2050 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2051 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2052 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2053 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2054
2055 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2056 { "_NET_SUPPORTING_WM_CHECK",
2057 F(atom.net_supporting_wm_check) },
2058 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002059 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002060 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2061 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002062 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002063 { "TARGETS", F(atom.targets) },
2064 { "UTF8_STRING", F(atom.utf8_string) },
2065 { "_WL_SELECTION", F(atom.wl_selection) },
2066 { "INCR", F(atom.incr) },
2067 { "TIMESTAMP", F(atom.timestamp) },
2068 { "MULTIPLE", F(atom.multiple) },
2069 { "UTF8_STRING" , F(atom.utf8_string) },
2070 { "COMPOUND_TEXT", F(atom.compound_text) },
2071 { "TEXT", F(atom.text) },
2072 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002073 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002074 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2075 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002076 { "XdndSelection", F(atom.xdnd_selection) },
2077 { "XdndAware", F(atom.xdnd_aware) },
2078 { "XdndEnter", F(atom.xdnd_enter) },
2079 { "XdndLeave", F(atom.xdnd_leave) },
2080 { "XdndDrop", F(atom.xdnd_drop) },
2081 { "XdndStatus", F(atom.xdnd_status) },
2082 { "XdndFinished", F(atom.xdnd_finished) },
2083 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002084 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2085 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002086 };
2087#undef F
2088
2089 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2090 xcb_xfixes_query_version_reply_t *xfixes_reply;
2091 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2092 xcb_intern_atom_reply_t *reply;
2093 xcb_render_query_pict_formats_reply_t *formats_reply;
2094 xcb_render_query_pict_formats_cookie_t formats_cookie;
2095 xcb_render_pictforminfo_t *formats;
2096 uint32_t i;
2097
2098 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002099 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002100
2101 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2102
2103 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2104 cookies[i] = xcb_intern_atom (wm->conn, 0,
2105 strlen(atoms[i].name),
2106 atoms[i].name);
2107
2108 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2109 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2110 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2111 free(reply);
2112 }
2113
2114 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2115 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002116 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002117
2118 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2119 XCB_XFIXES_MAJOR_VERSION,
2120 XCB_XFIXES_MINOR_VERSION);
2121 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2122 xfixes_cookie, NULL);
2123
Martin Minarik6d118362012-06-07 18:01:59 +02002124 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002125 xfixes_reply->major_version, xfixes_reply->minor_version);
2126
2127 free(xfixes_reply);
2128
2129 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2130 formats_cookie, 0);
2131 if (formats_reply == NULL)
2132 return;
2133
2134 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002135 for (i = 0; i < formats_reply->num_formats; i++) {
2136 if (formats[i].direct.red_mask != 0xff &&
2137 formats[i].direct.red_shift != 16)
2138 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002139 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2140 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002141 wm->format_rgb = formats[i];
2142 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2143 formats[i].depth == 32 &&
2144 formats[i].direct.alpha_mask == 0xff &&
2145 formats[i].direct.alpha_shift == 24)
2146 wm->format_rgba = formats[i];
2147 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002148
2149 free(formats_reply);
2150}
2151
2152static void
2153weston_wm_create_wm_window(struct weston_wm *wm)
2154{
2155 static const char name[] = "Weston WM";
2156
2157 wm->wm_window = xcb_generate_id(wm->conn);
2158 xcb_create_window(wm->conn,
2159 XCB_COPY_FROM_PARENT,
2160 wm->wm_window,
2161 wm->screen->root,
2162 0, 0,
2163 10, 10,
2164 0,
2165 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2166 wm->screen->root_visual,
2167 0, NULL);
2168
2169 xcb_change_property(wm->conn,
2170 XCB_PROP_MODE_REPLACE,
2171 wm->wm_window,
2172 wm->atom.net_supporting_wm_check,
2173 XCB_ATOM_WINDOW,
2174 32, /* format */
2175 1, &wm->wm_window);
2176
2177 xcb_change_property(wm->conn,
2178 XCB_PROP_MODE_REPLACE,
2179 wm->wm_window,
2180 wm->atom.net_wm_name,
2181 wm->atom.utf8_string,
2182 8, /* format */
2183 strlen(name), name);
2184
2185 xcb_change_property(wm->conn,
2186 XCB_PROP_MODE_REPLACE,
2187 wm->screen->root,
2188 wm->atom.net_supporting_wm_check,
2189 XCB_ATOM_WINDOW,
2190 32, /* format */
2191 1, &wm->wm_window);
2192
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002193 /* Claim the WM_S0 selection even though we don't suport
2194 * the --replace functionality. */
2195 xcb_set_selection_owner(wm->conn,
2196 wm->wm_window,
2197 wm->atom.wm_s0,
2198 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002199
2200 xcb_set_selection_owner(wm->conn,
2201 wm->wm_window,
2202 wm->atom.net_wm_cm_s0,
2203 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002204}
2205
2206struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002207weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002208{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002209 struct weston_wm *wm;
2210 struct wl_event_loop *loop;
2211 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002212 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002213 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002214
Peter Huttererf3d62272013-08-08 11:57:05 +10002215 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002216 if (wm == NULL)
2217 return NULL;
2218
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219 wm->server = wxs;
2220 wm->window_hash = hash_table_create();
2221 if (wm->window_hash == NULL) {
2222 free(wm);
2223 return NULL;
2224 }
2225
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002226 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002227 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002228 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002229 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002230 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002231 hash_table_destroy(wm->window_hash);
2232 free(wm);
2233 return NULL;
2234 }
2235
2236 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2237 wm->screen = s.data;
2238
2239 loop = wl_display_get_event_loop(wxs->wl_display);
2240 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002241 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002242 WL_EVENT_READABLE,
2243 weston_wm_handle_event, wm);
2244 wl_event_source_check(wm->source);
2245
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002246 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002247 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002248
2249 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002250 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2251 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2252 XCB_EVENT_MASK_PROPERTY_CHANGE;
2253 xcb_change_window_attributes(wm->conn, wm->screen->root,
2254 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002255
2256 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2257 XCB_COMPOSITE_REDIRECT_MANUAL);
2258
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002259 wm->theme = theme_create();
2260
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002261 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002262 supported[1] = wm->atom.net_wm_state;
2263 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002264 supported[3] = wm->atom.net_wm_state_maximized_vert;
2265 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002266 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002267 xcb_change_property(wm->conn,
2268 XCB_PROP_MODE_REPLACE,
2269 wm->screen->root,
2270 wm->atom.net_supported,
2271 XCB_ATOM_ATOM,
2272 32, /* format */
2273 ARRAY_LENGTH(supported), supported);
2274
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002275 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2276
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002277 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002278
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002279 weston_wm_dnd_init(wm);
2280
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002281 xcb_flush(wm->conn);
2282
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002283 wm->create_surface_listener.notify = weston_wm_create_surface;
2284 wl_signal_add(&wxs->compositor->create_surface_signal,
2285 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002286 wm->activate_listener.notify = weston_wm_window_activate;
2287 wl_signal_add(&wxs->compositor->activate_signal,
2288 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002289 wm->kill_listener.notify = weston_wm_kill_client;
2290 wl_signal_add(&wxs->compositor->kill_signal,
2291 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002292 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002293
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002294 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002295 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002296
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002297 /* Create wm window and take WM_S0 selection last, which
2298 * signals to Xwayland that we're done with setup. */
2299 weston_wm_create_wm_window(wm);
2300
2301 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002302
2303 return wm;
2304}
2305
2306void
2307weston_wm_destroy(struct weston_wm *wm)
2308{
2309 /* FIXME: Free windows in hash. */
2310 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002311 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002312 xcb_disconnect(wm->conn);
2313 wl_event_source_remove(wm->source);
2314 wl_list_remove(&wm->selection_listener.link);
2315 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002316 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002317 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002318
2319 free(wm);
2320}
2321
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002322static struct weston_wm_window *
2323get_wm_window(struct weston_surface *surface)
2324{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002325 struct wl_listener *listener;
2326
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002327 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002328 if (listener)
2329 return container_of(listener, struct weston_wm_window,
2330 surface_destroy_listener);
2331
2332 return NULL;
2333}
2334
2335static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002336weston_wm_window_configure(void *data)
2337{
2338 struct weston_wm_window *window = data;
2339 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002340 uint32_t values[4];
2341 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002342
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002343 weston_wm_window_get_child_position(window, &x, &y);
2344 values[0] = x;
2345 values[1] = y;
2346 values[2] = window->width;
2347 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002348 xcb_configure_window(wm->conn,
2349 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002350 XCB_CONFIG_WINDOW_X |
2351 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002352 XCB_CONFIG_WINDOW_WIDTH |
2353 XCB_CONFIG_WINDOW_HEIGHT,
2354 values);
2355
2356 weston_wm_window_get_frame_size(window, &width, &height);
2357 values[0] = width;
2358 values[1] = height;
2359 xcb_configure_window(wm->conn,
2360 window->frame_id,
2361 XCB_CONFIG_WINDOW_WIDTH |
2362 XCB_CONFIG_WINDOW_HEIGHT,
2363 values);
2364
2365 window->configure_source = NULL;
2366
2367 weston_wm_window_schedule_repaint(window);
2368}
2369
2370static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002371send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002372{
2373 struct weston_wm_window *window = get_wm_window(surface);
2374 struct weston_wm *wm = window->wm;
2375 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002376 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002377
Marek Chalupae9fe4672014-10-29 13:44:44 +01002378 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002379 hborder = 2 * t->width;
2380 vborder = t->titlebar_height + t->width;
2381 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002382 hborder = 0;
2383 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002384 }
2385
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002386 if (width > hborder)
2387 window->width = width - hborder;
2388 else
2389 window->width = 1;
2390
2391 if (height > vborder)
2392 window->height = height - vborder;
2393 else
2394 window->height = 1;
2395
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002396 if (window->frame)
2397 frame_resize_inside(window->frame, window->width, window->height);
2398
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002399 if (window->configure_source)
2400 return;
2401
2402 window->configure_source =
2403 wl_event_loop_add_idle(wm->server->loop,
2404 weston_wm_window_configure, window);
2405}
2406
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002407static void
2408send_position(struct weston_surface *surface, int32_t x, int32_t y)
2409{
2410 struct weston_wm_window *window = get_wm_window(surface);
2411 struct weston_wm *wm;
2412 uint32_t mask, values[2];
2413
2414 if (!window || !window->wm)
2415 return;
2416
2417 wm = window->wm;
2418 /* We use pos_dirty to tell whether a configure message is in flight.
2419 * This is needed in case we send two configure events in a very
2420 * short time, since window->x/y is set in after a roundtrip, hence
2421 * we cannot just check if the current x and y are different. */
2422 if (window->x != x || window->y != y || window->pos_dirty) {
2423 window->pos_dirty = true;
2424 values[0] = x;
2425 values[1] = y;
2426 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2427
2428 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2429 xcb_flush(wm->conn);
2430 }
2431}
2432
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002433static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002434 send_configure,
2435 send_position
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002436};
2437
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002438static int
2439legacy_fullscreen(struct weston_wm *wm,
2440 struct weston_wm_window *window,
2441 struct weston_output **output_ret)
2442{
2443 struct weston_compositor *compositor = wm->server->compositor;
2444 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002445 uint32_t minmax = PMinSize | PMaxSize;
2446 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002447
2448 /* Heuristics for detecting legacy fullscreen windows... */
2449
2450 wl_list_for_each(output, &compositor->output_list, link) {
2451 if (output->x == window->x &&
2452 output->y == window->y &&
2453 output->width == window->width &&
2454 output->height == window->height &&
2455 window->override_redirect) {
2456 *output_ret = output;
2457 return 1;
2458 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002459
2460 matching_size = 0;
2461 if ((window->size_hints.flags & (USSize |PSize)) &&
2462 window->size_hints.width == output->width &&
2463 window->size_hints.height == output->height)
2464 matching_size = 1;
2465 if ((window->size_hints.flags & minmax) == minmax &&
2466 window->size_hints.min_width == output->width &&
2467 window->size_hints.min_height == output->height &&
2468 window->size_hints.max_width == output->width &&
2469 window->size_hints.max_height == output->height)
2470 matching_size = 1;
2471
2472 if (matching_size && !window->decorate &&
2473 (window->size_hints.flags & (USPosition | PPosition)) &&
2474 window->size_hints.x == output->x &&
2475 window->size_hints.y == output->y) {
2476 *output_ret = output;
2477 return 1;
2478 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002479 }
2480
2481 return 0;
2482}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002483
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002484static bool
2485weston_wm_window_type_inactive(struct weston_wm_window *window)
2486{
2487 struct weston_wm *wm = window->wm;
2488
2489 return window->type == wm->atom.net_wm_window_type_tooltip ||
2490 window->type == wm->atom.net_wm_window_type_dropdown ||
2491 window->type == wm->atom.net_wm_window_type_dnd ||
2492 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002493 window->type == wm->atom.net_wm_window_type_popup ||
2494 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002495}
2496
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002497static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002498xserver_map_shell_surface(struct weston_wm_window *window,
2499 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002500{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002501 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002502 struct weston_shell_interface *shell_interface =
2503 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002504 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002505 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002506 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002507
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002508 weston_wm_window_read_properties(window);
2509
2510 /* A weston_wm_window may have many different surfaces assigned
2511 * throughout its life, so we must make sure to remove the listener
2512 * from the old surface signal list. */
2513 if (window->surface)
2514 wl_list_remove(&window->surface_destroy_listener.link);
2515
2516 window->surface = surface;
2517 window->surface_destroy_listener.notify = surface_destroy;
2518 wl_signal_add(&window->surface->destroy_signal,
2519 &window->surface_destroy_listener);
2520
2521 weston_wm_window_schedule_repaint(window);
2522
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002523 if (!shell_interface->create_shell_surface)
2524 return;
2525
Pekka Paalanen50b67472014-10-01 15:02:41 +03002526 if (window->surface->configure) {
2527 weston_log("warning, unexpected in %s: "
2528 "surface's configure hook is already set.\n",
2529 __func__);
2530 return;
2531 }
2532
Murray Calavera883ac022015-06-06 13:02:22 +00002533 window->shsurf =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002534 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002535 window->surface,
2536 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002537
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002538 if (window->name)
2539 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002540 if (window->pid > 0)
2541 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002542
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002543 if (window->fullscreen) {
2544 window->saved_width = window->width;
2545 window->saved_height = window->height;
2546 shell_interface->set_fullscreen(window->shsurf,
2547 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2548 0, NULL);
2549 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002550 } else if (legacy_fullscreen(wm, window, &output)) {
2551 window->fullscreen = 1;
2552 shell_interface->set_fullscreen(window->shsurf,
2553 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2554 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002555 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002556 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002557 window->x,
2558 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002559 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002560 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002561 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002562 if (weston_wm_window_type_inactive(window))
2563 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002564 shell_interface->set_transient(window->shsurf,
2565 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002566 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002567 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002568 } else if (weston_wm_window_is_maximized(window)) {
2569 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002570 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002571 if (weston_wm_window_type_inactive(window)) {
2572 shell_interface->set_xwayland(window->shsurf,
2573 window->x,
2574 window->y,
2575 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2576 } else {
2577 shell_interface->set_toplevel(window->shsurf);
2578 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002579 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002580}