blob: 7018c92f883218af3a0d62d84c8e4b115d673646 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040034#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050035#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040036
37#include "xwayland.h"
38
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080039#include "cairo-util.h"
40#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040041#include "hash.h"
42
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070043struct wm_size_hints {
44 uint32_t flags;
45 int32_t x, y;
46 int32_t width, height; /* should set so old wm's don't mess up */
47 int32_t min_width, min_height;
48 int32_t max_width, max_height;
49 int32_t width_inc, height_inc;
50 struct {
51 int32_t x;
52 int32_t y;
53 } min_aspect, max_aspect;
54 int32_t base_width, base_height;
55 int32_t win_gravity;
56};
57
58#define USPosition (1L << 0)
59#define USSize (1L << 1)
60#define PPosition (1L << 2)
61#define PSize (1L << 3)
62#define PMinSize (1L << 4)
63#define PMaxSize (1L << 5)
64#define PResizeInc (1L << 6)
65#define PAspect (1L << 7)
66#define PBaseSize (1L << 8)
67#define PWinGravity (1L << 9)
68
Kristian Høgsberg380deee2012-05-21 17:12:41 -040069struct motif_wm_hints {
70 uint32_t flags;
71 uint32_t functions;
72 uint32_t decorations;
73 int32_t input_mode;
74 uint32_t status;
75};
76
77#define MWM_HINTS_FUNCTIONS (1L << 0)
78#define MWM_HINTS_DECORATIONS (1L << 1)
79#define MWM_HINTS_INPUT_MODE (1L << 2)
80#define MWM_HINTS_STATUS (1L << 3)
81
82#define MWM_FUNC_ALL (1L << 0)
83#define MWM_FUNC_RESIZE (1L << 1)
84#define MWM_FUNC_MOVE (1L << 2)
85#define MWM_FUNC_MINIMIZE (1L << 3)
86#define MWM_FUNC_MAXIMIZE (1L << 4)
87#define MWM_FUNC_CLOSE (1L << 5)
88
89#define MWM_DECOR_ALL (1L << 0)
90#define MWM_DECOR_BORDER (1L << 1)
91#define MWM_DECOR_RESIZEH (1L << 2)
92#define MWM_DECOR_TITLE (1L << 3)
93#define MWM_DECOR_MENU (1L << 4)
94#define MWM_DECOR_MINIMIZE (1L << 5)
95#define MWM_DECOR_MAXIMIZE (1L << 6)
96
97#define MWM_INPUT_MODELESS 0
98#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
99#define MWM_INPUT_SYSTEM_MODAL 2
100#define MWM_INPUT_FULL_APPLICATION_MODAL 3
101#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
102
103#define MWM_TEAROFF_WINDOW (1L<<0)
104
105#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
106#define _NET_WM_MOVERESIZE_SIZE_TOP 1
107#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
108#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
109#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
110#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
111#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
112#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
113#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
114#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
115#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
116#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
117
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400118struct weston_wm_window {
119 struct weston_wm *wm;
120 xcb_window_t id;
121 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500122 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400123 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700124 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400125 struct weston_surface *surface;
126 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500127 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400128 struct wl_listener surface_destroy_listener;
129 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400130 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300132 int pid;
133 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400134 char *class;
135 char *name;
136 struct weston_wm_window *transient_for;
137 uint32_t protocols;
138 xcb_atom_t type;
139 int width, height;
140 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500141 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300143 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500144 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500145 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700146 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200147 int maximized_vert;
148 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700149 struct wm_size_hints size_hints;
150 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700151 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400152};
153
154static struct weston_wm_window *
155get_wm_window(struct weston_surface *surface);
156
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400157static void
158weston_wm_window_schedule_repaint(struct weston_wm_window *window);
159
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700160static void
161xserver_map_shell_surface(struct weston_wm_window *window,
162 struct weston_surface *surface);
163
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400164static int __attribute__ ((format (printf, 1, 2)))
165wm_log(const char *fmt, ...)
166{
167#ifdef WM_DEBUG
168 int l;
169 va_list argp;
170
171 va_start(argp, fmt);
172 l = weston_vlog(fmt, argp);
173 va_end(argp);
174
175 return l;
176#else
177 return 0;
178#endif
179}
180
181static int __attribute__ ((format (printf, 1, 2)))
182wm_log_continue(const char *fmt, ...)
183{
184#ifdef WM_DEBUG
185 int l;
186 va_list argp;
187
188 va_start(argp, fmt);
189 l = weston_vlog_continue(fmt, argp);
190 va_end(argp);
191
192 return l;
193#else
194 return 0;
195#endif
196}
197
198
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400199const char *
200get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
201{
202 xcb_get_atom_name_cookie_t cookie;
203 xcb_get_atom_name_reply_t *reply;
204 xcb_generic_error_t *e;
205 static char buffer[64];
206
207 if (atom == XCB_ATOM_NONE)
208 return "None";
209
210 cookie = xcb_get_atom_name (c, atom);
211 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500212
213 if(reply) {
214 snprintf(buffer, sizeof buffer, "%.*s",
215 xcb_get_atom_name_name_length (reply),
216 xcb_get_atom_name_name (reply));
217 } else {
218 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
219 }
220
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400221 free(reply);
222
223 return buffer;
224}
225
Tiago Vignatti90fada42012-07-16 12:02:08 -0400226static xcb_cursor_t
227xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
228{
229 xcb_connection_t *c = wm->conn;
230 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
231 xcb_screen_t *screen = s.data;
232 xcb_gcontext_t gc;
233 xcb_pixmap_t pix;
234 xcb_render_picture_t pic;
235 xcb_cursor_t cursor;
236 int stride = img->width * 4;
237
238 pix = xcb_generate_id(c);
239 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
240
241 pic = xcb_generate_id(c);
242 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
243
244 gc = xcb_generate_id(c);
245 xcb_create_gc(c, gc, pix, 0, 0);
246
247 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
248 img->width, img->height, 0, 0, 0, 32,
249 stride * img->height, (uint8_t *) img->pixels);
250 xcb_free_gc(c, gc);
251
252 cursor = xcb_generate_id(c);
253 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
254
255 xcb_render_free_picture(c, pic);
256 xcb_free_pixmap(c, pix);
257
258 return cursor;
259}
260
261static xcb_cursor_t
262xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
263{
264 /* TODO: treat animated cursors as well */
265 if (images->nimage != 1)
266 return -1;
267
268 return xcb_cursor_image_load_cursor(wm, images->images[0]);
269}
270
271static xcb_cursor_t
272xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
273{
274 xcb_cursor_t cursor;
275 XcursorImages *images;
276 char *v = NULL;
277 int size = 0;
278
279 if (!file)
280 return 0;
281
282 v = getenv ("XCURSOR_SIZE");
283 if (v)
284 size = atoi(v);
285
286 if (!size)
287 size = 32;
288
289 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300290 if (!images)
291 return -1;
292
Tiago Vignatti90fada42012-07-16 12:02:08 -0400293 cursor = xcb_cursor_images_load_cursor (wm, images);
294 XcursorImagesDestroy (images);
295
296 return cursor;
297}
298
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400299void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400300dump_property(struct weston_wm *wm,
301 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400302{
303 int32_t *incr_value;
304 const char *text_value, *name;
305 xcb_atom_t *atom_value;
306 int width, len;
307 uint32_t i;
308
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400309 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400310 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400311 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400312 return;
313 }
314
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400315 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
316 get_atom_name(wm->conn, reply->type),
317 reply->format,
318 xcb_get_property_value_length(reply),
319 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400320
321 if (reply->type == wm->atom.incr) {
322 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400323 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400324 } else if (reply->type == wm->atom.utf8_string ||
325 reply->type == wm->atom.string) {
326 text_value = xcb_get_property_value(reply);
327 if (reply->value_len > 40)
328 len = 40;
329 else
330 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400331 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400332 } else if (reply->type == XCB_ATOM_ATOM) {
333 atom_value = xcb_get_property_value(reply);
334 for (i = 0; i < reply->value_len; i++) {
335 name = get_atom_name(wm->conn, atom_value[i]);
336 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400337 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400338 width = 4;
339 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400340 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400341 }
342
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400343 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400344 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400345 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400347 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400348 }
349}
350
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200351static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400352read_and_dump_property(struct weston_wm *wm,
353 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400355 xcb_get_property_reply_t *reply;
356 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400358 cookie = xcb_get_property(wm->conn, 0, window,
359 property, XCB_ATOM_ANY, 0, 2048);
360 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400362 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400364 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400365}
366
367/* We reuse some predefined, but otherwise useles atoms */
368#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
369#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500370#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700371#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400372
373static void
374weston_wm_window_read_properties(struct weston_wm_window *window)
375{
376 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200377 struct weston_shell_interface *shell_interface =
378 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400379
380#define F(field) offsetof(struct weston_wm_window, field)
381 const struct {
382 xcb_atom_t atom;
383 xcb_atom_t type;
384 int offset;
385 } props[] = {
386 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
387 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
388 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
389 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700390 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500391 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400392 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
393 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300394 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300396 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400397 };
398#undef F
399
400 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
401 xcb_get_property_reply_t *reply;
402 void *p;
403 uint32_t *xid;
404 xcb_atom_t *atom;
405 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200406 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400407
408 if (!window->properties_dirty)
409 return;
410 window->properties_dirty = 0;
411
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400412 for (i = 0; i < ARRAY_LENGTH(props); i++)
413 cookie[i] = xcb_get_property(wm->conn,
414 0, /* delete */
415 window->id,
416 props[i].atom,
417 XCB_ATOM_ANY, 0, 2048);
418
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300419 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700420 window->size_hints.flags = 0;
421 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700422 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700423
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400424 for (i = 0; i < ARRAY_LENGTH(props); i++) {
425 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
426 if (!reply)
427 /* Bad window, typically */
428 continue;
429 if (reply->type == XCB_ATOM_NONE) {
430 /* No such property */
431 free(reply);
432 continue;
433 }
434
435 p = ((char *) window + props[i].offset);
436
437 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300438 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400439 case XCB_ATOM_STRING:
440 /* FIXME: We're using this for both string and
441 utf8_string */
442 if (*(char **) p)
443 free(*(char **) p);
444
445 *(char **) p =
446 strndup(xcb_get_property_value(reply),
447 xcb_get_property_value_length(reply));
448 break;
449 case XCB_ATOM_WINDOW:
450 xid = xcb_get_property_value(reply);
451 *(struct weston_wm_window **) p =
452 hash_table_lookup(wm->window_hash, *xid);
453 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300454 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400455 case XCB_ATOM_ATOM:
456 atom = xcb_get_property_value(reply);
457 *(xcb_atom_t *) p = *atom;
458 break;
459 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700460 atom = xcb_get_property_value(reply);
461 for (i = 0; i < reply->value_len; i++)
462 if (atom[i] == wm->atom.wm_delete_window)
463 window->delete_window = 1;
464 break;
465
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400466 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700467 case TYPE_WM_NORMAL_HINTS:
468 memcpy(&window->size_hints,
469 xcb_get_property_value(reply),
470 sizeof window->size_hints);
471 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500472 case TYPE_NET_WM_STATE:
473 window->fullscreen = 0;
474 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900475 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500476 if (atom[i] == wm->atom.net_wm_state_fullscreen)
477 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200478 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
479 window->maximized_vert = 1;
480 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
481 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900482 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500483 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400484 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700485 memcpy(&window->motif_hints,
486 xcb_get_property_value(reply),
487 sizeof window->motif_hints);
488 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
489 window->decorate =
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200490 window->motif_hints.decorations;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400491 break;
492 default:
493 break;
494 }
495 free(reply);
496 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200497
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200498 if (window->pid > 0) {
499 gethostname(name, sizeof(name));
500 for (i = 0; i < sizeof(name); i++) {
501 if (name[i] == '\0')
502 break;
503 }
504 if (i == sizeof(name))
505 name[0] = '\0'; /* ignore stupid hostnames */
506
507 /* this is only one heuristic to guess the PID of a client is
508 * valid, assuming it's compliant with icccm and ewmh.
509 * Non-compliants and remote applications of course fail. */
510 if (!window->machine || strcmp(window->machine, name))
511 window->pid = 0;
512 }
513
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200514 if (window->shsurf && window->name)
515 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500516 if (window->frame && window->name)
517 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200518 if (window->shsurf && window->pid > 0)
519 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400520}
521
522static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400523weston_wm_window_get_frame_size(struct weston_wm_window *window,
524 int *width, int *height)
525{
526 struct theme *t = window->wm->theme;
527
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500528 if (window->fullscreen) {
529 *width = window->width;
530 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800531 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500532 *width = frame_width(window->frame);
533 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400534 } else {
535 *width = window->width + t->margin * 2;
536 *height = window->height + t->margin * 2;
537 }
538}
539
540static void
541weston_wm_window_get_child_position(struct weston_wm_window *window,
542 int *x, int *y)
543{
544 struct theme *t = window->wm->theme;
545
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500546 if (window->fullscreen) {
547 *x = 0;
548 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800549 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500550 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400551 } else {
552 *x = t->margin;
553 *y = t->margin;
554 }
555}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400556
557static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500558weston_wm_window_send_configure_notify(struct weston_wm_window *window)
559{
560 xcb_configure_notify_event_t configure_notify;
561 struct weston_wm *wm = window->wm;
562 int x, y;
563
564 weston_wm_window_get_child_position(window, &x, &y);
565 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
566 configure_notify.pad0 = 0;
567 configure_notify.event = window->id;
568 configure_notify.window = window->id;
569 configure_notify.above_sibling = XCB_WINDOW_NONE;
570 configure_notify.x = x;
571 configure_notify.y = y;
572 configure_notify.width = window->width;
573 configure_notify.height = window->height;
574 configure_notify.border_width = 0;
575 configure_notify.override_redirect = 0;
576 configure_notify.pad1 = 0;
577
578 xcb_send_event(wm->conn, 0, window->id,
579 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
580 (char *) &configure_notify);
581}
582
583static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400584weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
585{
586 xcb_configure_request_event_t *configure_request =
587 (xcb_configure_request_event_t *) event;
588 struct weston_wm_window *window;
589 uint32_t mask, values[16];
590 int x, y, width, height, i = 0;
591
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400592 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
593 configure_request->window,
594 configure_request->x, configure_request->y,
595 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400596
597 window = hash_table_lookup(wm->window_hash, configure_request->window);
598
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500599 if (window->fullscreen) {
600 weston_wm_window_send_configure_notify(window);
601 return;
602 }
603
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400604 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
605 window->width = configure_request->width;
606 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
607 window->height = configure_request->height;
608
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500609 if (window->frame)
610 frame_resize_inside(window->frame, window->width, window->height);
611
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400612 weston_wm_window_get_child_position(window, &x, &y);
613 values[i++] = x;
614 values[i++] = y;
615 values[i++] = window->width;
616 values[i++] = window->height;
617 values[i++] = 0;
618 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
619 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
620 XCB_CONFIG_WINDOW_BORDER_WIDTH;
621 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
622 values[i++] = configure_request->sibling;
623 mask |= XCB_CONFIG_WINDOW_SIBLING;
624 }
625 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
626 values[i++] = configure_request->stack_mode;
627 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
628 }
629
630 xcb_configure_window(wm->conn, window->id, mask, values);
631
632 weston_wm_window_get_frame_size(window, &width, &height);
633 values[0] = width;
634 values[1] = height;
635 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
636 xcb_configure_window(wm->conn, window->frame_id, mask, values);
637
638 weston_wm_window_schedule_repaint(window);
639}
640
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400641static int
642our_resource(struct weston_wm *wm, uint32_t id)
643{
644 const xcb_setup_t *setup;
645
646 setup = xcb_get_setup(wm->conn);
647
648 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
649}
650
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400651static void
652weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
653{
654 xcb_configure_notify_event_t *configure_notify =
655 (xcb_configure_notify_event_t *) event;
656 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400657
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400658 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400659 configure_notify->window,
660 configure_notify->x, configure_notify->y,
661 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300662
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400663 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700664 window->x = configure_notify->x;
665 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700666 if (window->override_redirect) {
667 window->width = configure_notify->width;
668 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500669 if (window->frame)
670 frame_resize_inside(window->frame,
671 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700672 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400673}
674
675static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300676weston_wm_kill_client(struct wl_listener *listener, void *data)
677{
678 struct weston_surface *surface = data;
679 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300680 if (!window)
681 return;
682
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200683 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300684 kill(window->pid, SIGKILL);
685}
686
687static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700688weston_wm_create_surface(struct wl_listener *listener, void *data)
689{
690 struct weston_surface *surface = data;
691 struct weston_wm *wm =
692 container_of(listener,
693 struct weston_wm, create_surface_listener);
694 struct weston_wm_window *window;
695
696 if (wl_resource_get_client(surface->resource) != wm->server->client)
697 return;
698
699 wl_list_for_each(window, &wm->unpaired_window_list, link)
700 if (window->surface_id ==
701 wl_resource_get_id(surface->resource)) {
702 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700703 window->surface_id = 0;
704 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700705 break;
706 }
707}
708
709static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400710weston_wm_window_activate(struct wl_listener *listener, void *data)
711{
712 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200713 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600714 struct weston_wm *wm =
715 container_of(listener, struct weston_wm, activate_listener);
716 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400717
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200718 if (surface) {
719 window = get_wm_window(surface);
720 }
721
Scott Moreau85ecac02012-05-21 15:49:14 -0600722 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700723 uint32_t values[1];
724
Boyan Dingb9f863c2014-08-30 10:33:23 +0800725 if (window->override_redirect)
726 return;
727
Scott Moreau85ecac02012-05-21 15:49:14 -0600728 client_message.response_type = XCB_CLIENT_MESSAGE;
729 client_message.format = 32;
730 client_message.window = window->id;
731 client_message.type = wm->atom.wm_protocols;
732 client_message.data.data32[0] = wm->atom.wm_take_focus;
733 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
734
735 xcb_send_event(wm->conn, 0, window->id,
736 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
737 (char *) &client_message);
738
739 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
740 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700741
742 values[0] = XCB_STACK_MODE_ABOVE;
743 xcb_configure_window (wm->conn, window->frame_id,
744 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600745 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400746 xcb_set_input_focus (wm->conn,
747 XCB_INPUT_FOCUS_POINTER_ROOT,
748 XCB_NONE,
749 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600750 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400751
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500752 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800753 if (wm->focus_window->frame)
754 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400755 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500756 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400757 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500758 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800759 if (wm->focus_window->frame)
760 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400761 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500762 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400763}
764
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300765static void
766weston_wm_window_transform(struct wl_listener *listener, void *data)
767{
768 struct weston_surface *surface = data;
769 struct weston_wm_window *window = get_wm_window(surface);
770 struct weston_wm *wm =
771 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300772 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300773
774 if (!window || !wm)
775 return;
776
Jason Ekstranda7af7042013-10-12 22:38:11 -0500777 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300778 return;
779
Jason Ekstranda7af7042013-10-12 22:38:11 -0500780 if (window->x != window->view->geometry.x ||
781 window->y != window->view->geometry.y) {
782 values[0] = window->view->geometry.x;
783 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700784 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300785
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700786 xcb_configure_window(wm->conn, window->frame_id, mask, values);
787 xcb_flush(wm->conn);
788 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300789}
790
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400791#define ICCCM_WITHDRAWN_STATE 0
792#define ICCCM_NORMAL_STATE 1
793#define ICCCM_ICONIC_STATE 3
794
795static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500796weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400797{
798 struct weston_wm *wm = window->wm;
799 uint32_t property[2];
800
801 property[0] = state;
802 property[1] = XCB_WINDOW_NONE;
803
804 xcb_change_property(wm->conn,
805 XCB_PROP_MODE_REPLACE,
806 window->id,
807 wm->atom.wm_state,
808 wm->atom.wm_state,
809 32, /* format */
810 2, property);
811}
812
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400813static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500814weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
815{
816 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200817 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500818 int i;
819
820 i = 0;
821 if (window->fullscreen)
822 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200823 if (window->maximized_vert)
824 property[i++] = wm->atom.net_wm_state_maximized_vert;
825 if (window->maximized_horz)
826 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500827
828 xcb_change_property(wm->conn,
829 XCB_PROP_MODE_REPLACE,
830 window->id,
831 wm->atom.net_wm_state,
832 XCB_ATOM_ATOM,
833 32, /* format */
834 i, property);
835}
836
837static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700838weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700840 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400841 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400842 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200843 int buttons = FRAME_BUTTON_CLOSE;
844
845 if (window->decorate & MWM_DECOR_MAXIMIZE)
846 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400847
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500848 window->frame = frame_create(window->wm->theme,
849 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200850 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500851 frame_resize_inside(window->frame, window->width, window->height);
852
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400853 weston_wm_window_get_frame_size(window, &width, &height);
854 weston_wm_window_get_child_position(window, &x, &y);
855
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400856 values[0] = wm->screen->black_pixel;
857 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400858 XCB_EVENT_MASK_KEY_PRESS |
859 XCB_EVENT_MASK_KEY_RELEASE |
860 XCB_EVENT_MASK_BUTTON_PRESS |
861 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400862 XCB_EVENT_MASK_POINTER_MOTION |
863 XCB_EVENT_MASK_ENTER_WINDOW |
864 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400866 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400867 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400868
869 window->frame_id = xcb_generate_id(wm->conn);
870 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400871 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400872 window->frame_id,
873 wm->screen->root,
874 0, 0,
875 width, height,
876 0,
877 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400878 wm->visual_id,
879 XCB_CW_BORDER_PIXEL |
880 XCB_CW_EVENT_MASK |
881 XCB_CW_COLORMAP, values);
882
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400883 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
884
885 values[0] = 0;
886 xcb_configure_window(wm->conn, window->id,
887 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
888
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400889 window->cairo_surface =
890 cairo_xcb_surface_create_with_xrender_format(wm->conn,
891 wm->screen,
892 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400893 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400894 width, height);
895
896 hash_table_insert(wm->window_hash, window->frame_id, window);
897}
898
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200899/*
900 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
901 * Passing a <0 desktop value deletes the property.
902 */
903static void
904weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
905 int desktop)
906{
907 if (desktop >= 0) {
908 xcb_change_property(window->wm->conn,
909 XCB_PROP_MODE_REPLACE,
910 window->id,
911 window->wm->atom.net_wm_desktop,
912 XCB_ATOM_CARDINAL,
913 32, /* format */
914 1, &desktop);
915 } else {
916 xcb_delete_property(window->wm->conn,
917 window->id,
918 window->wm->atom.net_wm_desktop);
919 }
920}
921
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400922static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700923weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
924{
925 xcb_map_request_event_t *map_request =
926 (xcb_map_request_event_t *) event;
927 struct weston_wm_window *window;
928
929 if (our_resource(wm, map_request->window)) {
930 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
931 map_request->window);
932 return;
933 }
934
935 window = hash_table_lookup(wm->window_hash, map_request->window);
936
937 weston_wm_window_read_properties(window);
938
939 if (window->frame_id == XCB_WINDOW_NONE)
940 weston_wm_window_create_frame(window);
941
942 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
943 window->id, window, window->frame_id);
944
945 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
946 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200947 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700948
949 xcb_map_window(wm->conn, map_request->window);
950 xcb_map_window(wm->conn, window->frame_id);
951}
952
953static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400954weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
955{
956 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
957
958 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400959 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
960 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400961 return;
962 }
963
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400964 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400965}
966
967static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400968weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
969{
970 xcb_unmap_notify_event_t *unmap_notify =
971 (xcb_unmap_notify_event_t *) event;
972 struct weston_wm_window *window;
973
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400974 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
975 unmap_notify->window,
976 unmap_notify->event,
977 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400978
979 if (our_resource(wm, unmap_notify->window))
980 return;
981
MoD31700122013-06-11 19:58:55 -0500982 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400983 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
984 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400985 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400986
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400987 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400988 if (wm->focus_window == window)
989 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400990 if (window->surface)
991 wl_list_remove(&window->surface_destroy_listener.link);
992 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200993 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -0800994 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200995
996 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
997 weston_wm_window_set_virtual_desktop(window, -1);
998
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700999 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001000}
1001
1002static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001003weston_wm_window_draw_decoration(void *data)
1004{
1005 struct weston_wm_window *window = data;
1006 struct weston_wm *wm = window->wm;
1007 struct theme *t = wm->theme;
1008 cairo_t *cr;
1009 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001010 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001011 struct weston_shell_interface *shell_interface =
1012 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001013 uint32_t flags = 0;
1014
1015 weston_wm_window_read_properties(window);
1016
1017 window->repaint_source = NULL;
1018
1019 weston_wm_window_get_frame_size(window, &width, &height);
1020 weston_wm_window_get_child_position(window, &x, &y);
1021
1022 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1023 cr = cairo_create(window->cairo_surface);
1024
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001025 if (window->fullscreen) {
1026 /* nothing */
1027 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001028 if (wm->focus_window == window)
1029 flags |= THEME_FRAME_ACTIVE;
1030
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001031 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001032 } else {
1033 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1034 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1035 cairo_paint(cr);
1036
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001037 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001038 }
1039
1040 cairo_destroy(cr);
1041
1042 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001043 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001044 if(window->has_alpha) {
1045 pixman_region32_init(&window->surface->pending.opaque);
1046 } else {
1047 /* We leave an extra pixel around the X window area to
1048 * make sure we don't sample from the undefined alpha
1049 * channel when filtering. */
1050 pixman_region32_init_rect(&window->surface->pending.opaque,
1051 x - 1, y - 1,
1052 window->width + 2,
1053 window->height + 2);
1054 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055 if (window->view)
1056 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001057
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001058 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001059
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001060 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001061 frame_input_rect(window->frame, &input_x, &input_y,
1062 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001063 } else {
1064 input_x = x;
1065 input_y = y;
1066 input_w = width;
1067 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001068 }
1069
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001070 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001071 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001072
1073 shell_interface->set_window_geometry(window->shsurf,
1074 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001075 }
1076}
1077
1078static void
1079weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1080{
1081 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001082 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001083
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001084 if (window->frame_id == XCB_WINDOW_NONE) {
1085 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001086 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001087 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001088 if(window->has_alpha) {
1089 pixman_region32_init(&window->surface->pending.opaque);
1090 } else {
1091 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1092 width, height);
1093 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001094 if (window->view)
1095 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001096 }
1097 return;
1098 }
1099
1100 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001101 return;
1102
1103 window->repaint_source =
1104 wl_event_loop_add_idle(wm->server->loop,
1105 weston_wm_window_draw_decoration,
1106 window);
1107}
1108
1109static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001110weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1111{
1112 xcb_property_notify_event_t *property_notify =
1113 (xcb_property_notify_event_t *) event;
1114 struct weston_wm_window *window;
1115
1116 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001117 if (!window)
1118 return;
1119
1120 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001121
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001122 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001123 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001124 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001125 else
1126 read_and_dump_property(wm, property_notify->window,
1127 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001128
1129 if (property_notify->atom == wm->atom.net_wm_name ||
1130 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001131 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001132}
1133
1134static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001135weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001136 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001137{
1138 struct weston_wm_window *window;
1139 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001140 xcb_get_geometry_cookie_t geometry_cookie;
1141 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001142
Peter Huttererf3d62272013-08-08 11:57:05 +10001143 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001144 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001145 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001146 return;
1147 }
1148
MoD384a11a2013-06-22 11:04:21 -05001149 geometry_cookie = xcb_get_geometry(wm->conn, id);
1150
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001151 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1152 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1153
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001154 window->wm = wm;
1155 window->id = id;
1156 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001157 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001158 window->width = width;
1159 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001160 window->x = x;
1161 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001162
MoD384a11a2013-06-22 11:04:21 -05001163 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1164 /* technically we should use XRender and check the visual format's
1165 alpha_mask, but checking depth is simpler and works in all known cases */
1166 if(geometry_reply != NULL)
1167 window->has_alpha = geometry_reply->depth == 32;
1168 free(geometry_reply);
1169
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001170 hash_table_insert(wm->window_hash, id, window);
1171}
1172
1173static void
1174weston_wm_window_destroy(struct weston_wm_window *window)
1175{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001176 struct weston_wm *wm = window->wm;
1177
1178 if (window->repaint_source)
1179 wl_event_source_remove(window->repaint_source);
1180 if (window->cairo_surface)
1181 cairo_surface_destroy(window->cairo_surface);
1182
1183 if (window->frame_id) {
1184 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1185 xcb_destroy_window(wm->conn, window->frame_id);
1186 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001187 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001188 hash_table_remove(wm->window_hash, window->frame_id);
1189 window->frame_id = XCB_WINDOW_NONE;
1190 }
1191
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001192 if (window->surface_id)
1193 wl_list_remove(&window->link);
1194
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001195 hash_table_remove(window->wm->window_hash, window->id);
1196 free(window);
1197}
1198
1199static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001200weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1201{
1202 xcb_create_notify_event_t *create_notify =
1203 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001204
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001205 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1206 create_notify->window,
1207 create_notify->width, create_notify->height,
1208 create_notify->override_redirect ? ", override" : "",
1209 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001210
1211 if (our_resource(wm, create_notify->window))
1212 return;
1213
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001214 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001215 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001216 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001217 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001218}
1219
1220static void
1221weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1222{
1223 xcb_destroy_notify_event_t *destroy_notify =
1224 (xcb_destroy_notify_event_t *) event;
1225 struct weston_wm_window *window;
1226
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001227 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1228 destroy_notify->window,
1229 destroy_notify->event,
1230 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001231
1232 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001233 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001234
1235 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001236 weston_wm_window_destroy(window);
1237}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001238
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001239static void
1240weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1241{
1242 xcb_reparent_notify_event_t *reparent_notify =
1243 (xcb_reparent_notify_event_t *) event;
1244 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001245
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001246 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1247 reparent_notify->window,
1248 reparent_notify->parent,
1249 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001250
1251 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001252 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001253 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001254 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001255 } else if (!our_resource(wm, reparent_notify->parent)) {
1256 window = hash_table_lookup(wm->window_hash,
1257 reparent_notify->window);
1258 weston_wm_window_destroy(window);
1259 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001260}
1261
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001262struct weston_seat *
1263weston_wm_pick_seat(struct weston_wm *wm)
1264{
1265 return container_of(wm->server->compositor->seat_list.next,
1266 struct weston_seat, link);
1267}
1268
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001269static struct weston_seat *
1270weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1271{
1272 struct weston_wm *wm = window->wm;
1273 struct weston_seat *seat, *s;
1274
1275 seat = NULL;
1276 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001277 if (s->pointer != NULL && s->pointer->focus &&
1278 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001279 s->pointer->button_count > 0 &&
1280 (seat == NULL ||
1281 s->pointer->grab_serial -
1282 seat->pointer->grab_serial < (1 << 30)))
1283 seat = s;
1284 }
1285
1286 return seat;
1287}
1288
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001289static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001290weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1291 xcb_client_message_event_t *client_message)
1292{
1293 static const int map[] = {
1294 THEME_LOCATION_RESIZING_TOP_LEFT,
1295 THEME_LOCATION_RESIZING_TOP,
1296 THEME_LOCATION_RESIZING_TOP_RIGHT,
1297 THEME_LOCATION_RESIZING_RIGHT,
1298 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1299 THEME_LOCATION_RESIZING_BOTTOM,
1300 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1301 THEME_LOCATION_RESIZING_LEFT
1302 };
1303
1304 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001305 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001306 int detail;
1307 struct weston_shell_interface *shell_interface =
1308 &wm->server->compositor->shell_interface;
1309
Boyan Dingc06a1802014-07-06 11:44:58 +08001310 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001311 || !seat->pointer->focus
1312 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001313 return;
1314
1315 detail = client_message->data.data32[2];
1316 switch (detail) {
1317 case _NET_WM_MOVERESIZE_MOVE:
1318 shell_interface->move(window->shsurf, seat);
1319 break;
1320 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1321 case _NET_WM_MOVERESIZE_SIZE_TOP:
1322 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1323 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1324 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1325 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1326 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1327 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1328 shell_interface->resize(window->shsurf, seat, map[detail]);
1329 break;
1330 case _NET_WM_MOVERESIZE_CANCEL:
1331 break;
1332 }
1333}
1334
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001335#define _NET_WM_STATE_REMOVE 0
1336#define _NET_WM_STATE_ADD 1
1337#define _NET_WM_STATE_TOGGLE 2
1338
1339static int
1340update_state(int action, int *state)
1341{
1342 int new_state, changed;
1343
1344 switch (action) {
1345 case _NET_WM_STATE_REMOVE:
1346 new_state = 0;
1347 break;
1348 case _NET_WM_STATE_ADD:
1349 new_state = 1;
1350 break;
1351 case _NET_WM_STATE_TOGGLE:
1352 new_state = !*state;
1353 break;
1354 default:
1355 return 0;
1356 }
1357
1358 changed = (*state != new_state);
1359 *state = new_state;
1360
1361 return changed;
1362}
1363
1364static void
1365weston_wm_window_configure(void *data);
1366
1367static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001368weston_wm_window_set_toplevel(struct weston_wm_window *window)
1369{
1370 struct weston_shell_interface *shell_interface =
1371 &window->wm->server->compositor->shell_interface;
1372
1373 shell_interface->set_toplevel(window->shsurf);
1374 window->width = window->saved_width;
1375 window->height = window->saved_height;
1376 if (window->frame)
1377 frame_resize_inside(window->frame,
1378 window->width,
1379 window->height);
1380 weston_wm_window_configure(window);
1381}
1382
1383static inline bool
1384weston_wm_window_is_maximized(struct weston_wm_window *window)
1385{
1386 return window->maximized_horz && window->maximized_vert;
1387}
1388
1389static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001390weston_wm_window_handle_state(struct weston_wm_window *window,
1391 xcb_client_message_event_t *client_message)
1392{
1393 struct weston_wm *wm = window->wm;
1394 struct weston_shell_interface *shell_interface =
1395 &wm->server->compositor->shell_interface;
1396 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001397 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001398
1399 action = client_message->data.data32[0];
1400 property = client_message->data.data32[1];
1401
1402 if (property == wm->atom.net_wm_state_fullscreen &&
1403 update_state(action, &window->fullscreen)) {
1404 weston_wm_window_set_net_wm_state(window);
1405 if (window->fullscreen) {
1406 window->saved_width = window->width;
1407 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001408
1409 if (window->shsurf)
1410 shell_interface->set_fullscreen(window->shsurf,
1411 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1412 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001413 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001414 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001415 weston_wm_window_set_toplevel(window);
1416 }
1417 } else {
1418 if (property == wm->atom.net_wm_state_maximized_vert &&
1419 update_state(action, &window->maximized_vert))
1420 weston_wm_window_set_net_wm_state(window);
1421 if (property == wm->atom.net_wm_state_maximized_horz &&
1422 update_state(action, &window->maximized_horz))
1423 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001424
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001425 if (maximized != weston_wm_window_is_maximized(window)) {
1426 if (weston_wm_window_is_maximized(window)) {
1427 window->saved_width = window->width;
1428 window->saved_height = window->height;
1429
1430 if (window->shsurf)
1431 shell_interface->set_maximized(window->shsurf);
1432 } else if (window->shsurf) {
1433 weston_wm_window_set_toplevel(window);
1434 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001435 }
1436 }
1437}
1438
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001439static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001440surface_destroy(struct wl_listener *listener, void *data)
1441{
1442 struct weston_wm_window *window =
1443 container_of(listener,
1444 struct weston_wm_window, surface_destroy_listener);
1445
1446 wm_log("surface for xid %d destroyed\n", window->id);
1447
1448 /* This should have been freed by the shell.
1449 * Don't try to use it later. */
1450 window->shsurf = NULL;
1451 window->surface = NULL;
1452 window->view = NULL;
1453}
1454
1455static void
1456weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1457 xcb_client_message_event_t *client_message)
1458{
1459 struct weston_wm *wm = window->wm;
1460 struct wl_resource *resource;
1461
1462 if (window->surface_id != 0) {
1463 wm_log("already have surface id for window %d\n", window->id);
1464 return;
1465 }
1466
1467 /* Xwayland will send the wayland requests to create the
1468 * wl_surface before sending this client message. Even so, we
1469 * can end up handling the X event before the wayland requests
1470 * and thus when we try to look up the surface ID, the surface
1471 * hasn't been created yet. In that case put the window on
1472 * the unpaired window list and continue when the surface gets
1473 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001474 uint32_t id = client_message->data.data32[0];
1475 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001476 if (resource) {
1477 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001478 xserver_map_shell_surface(window,
1479 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001480 }
1481 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001482 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001483 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001484 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001485}
1486
1487static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001488weston_wm_handle_client_message(struct weston_wm *wm,
1489 xcb_generic_event_t *event)
1490{
1491 xcb_client_message_event_t *client_message =
1492 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001493 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001494
1495 window = hash_table_lookup(wm->window_hash, client_message->window);
1496
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001497 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1498 get_atom_name(wm->conn, client_message->type),
1499 client_message->data.data32[0],
1500 client_message->data.data32[1],
1501 client_message->data.data32[2],
1502 client_message->data.data32[3],
1503 client_message->data.data32[4],
1504 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001505
Jason Ekstrand0250a742014-07-24 13:17:47 -07001506 /* The window may get created and destroyed before we actually
1507 * handle the message. If it doesn't exist, bail.
1508 */
1509 if (!window)
1510 return;
1511
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001512 if (client_message->type == wm->atom.net_wm_moveresize)
1513 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001514 else if (client_message->type == wm->atom.net_wm_state)
1515 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001516 else if (client_message->type == wm->atom.wl_surface_id)
1517 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001518}
1519
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001520enum cursor_type {
1521 XWM_CURSOR_TOP,
1522 XWM_CURSOR_BOTTOM,
1523 XWM_CURSOR_LEFT,
1524 XWM_CURSOR_RIGHT,
1525 XWM_CURSOR_TOP_LEFT,
1526 XWM_CURSOR_TOP_RIGHT,
1527 XWM_CURSOR_BOTTOM_LEFT,
1528 XWM_CURSOR_BOTTOM_RIGHT,
1529 XWM_CURSOR_LEFT_PTR,
1530};
1531
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001532/*
1533 * The following correspondences between file names and cursors was copied
1534 * from: https://bugs.kde.org/attachment.cgi?id=67313
1535 */
1536
1537static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001538 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001539 "sw-resize",
1540 "size_bdiag"
1541};
1542
1543static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001544 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001545 "se-resize",
1546 "size_fdiag"
1547};
1548
1549static const char *bottom_sides[] = {
1550 "bottom_side",
1551 "s-resize",
1552 "size_ver"
1553};
1554
1555static const char *left_ptrs[] = {
1556 "left_ptr",
1557 "default",
1558 "top_left_arrow",
1559 "left-arrow"
1560};
1561
1562static const char *left_sides[] = {
1563 "left_side",
1564 "w-resize",
1565 "size_hor"
1566};
1567
1568static const char *right_sides[] = {
1569 "right_side",
1570 "e-resize",
1571 "size_hor"
1572};
1573
1574static const char *top_left_corners[] = {
1575 "top_left_corner",
1576 "nw-resize",
1577 "size_fdiag"
1578};
1579
1580static const char *top_right_corners[] = {
1581 "top_right_corner",
1582 "ne-resize",
1583 "size_bdiag"
1584};
1585
1586static const char *top_sides[] = {
1587 "top_side",
1588 "n-resize",
1589 "size_ver"
1590};
1591
1592struct cursor_alternatives {
1593 const char **names;
1594 size_t count;
1595};
1596
1597static const struct cursor_alternatives cursors[] = {
1598 {top_sides, ARRAY_LENGTH(top_sides)},
1599 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1600 {left_sides, ARRAY_LENGTH(left_sides)},
1601 {right_sides, ARRAY_LENGTH(right_sides)},
1602 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1603 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1604 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1605 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1606 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001607};
1608
1609static void
1610weston_wm_create_cursors(struct weston_wm *wm)
1611{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001612 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001613 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001614 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001615
1616 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1617 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001618 for (j = 0; j < cursors[i].count; j++) {
1619 name = cursors[i].names[j];
1620 wm->cursors[i] =
1621 xcb_cursor_library_load_cursor(wm, name);
1622 if (wm->cursors[i] != (xcb_cursor_t)-1)
1623 break;
1624 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001625 }
1626
1627 wm->last_cursor = -1;
1628}
1629
1630static void
1631weston_wm_destroy_cursors(struct weston_wm *wm)
1632{
1633 uint8_t i;
1634
1635 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1636 xcb_free_cursor(wm->conn, wm->cursors[i]);
1637
1638 free(wm->cursors);
1639}
1640
1641static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001642get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001643{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001644 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001645
1646 switch (location) {
1647 case THEME_LOCATION_RESIZING_TOP:
1648 return XWM_CURSOR_TOP;
1649 case THEME_LOCATION_RESIZING_BOTTOM:
1650 return XWM_CURSOR_BOTTOM;
1651 case THEME_LOCATION_RESIZING_LEFT:
1652 return XWM_CURSOR_LEFT;
1653 case THEME_LOCATION_RESIZING_RIGHT:
1654 return XWM_CURSOR_RIGHT;
1655 case THEME_LOCATION_RESIZING_TOP_LEFT:
1656 return XWM_CURSOR_TOP_LEFT;
1657 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1658 return XWM_CURSOR_TOP_RIGHT;
1659 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1660 return XWM_CURSOR_BOTTOM_LEFT;
1661 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1662 return XWM_CURSOR_BOTTOM_RIGHT;
1663 case THEME_LOCATION_EXTERIOR:
1664 case THEME_LOCATION_TITLEBAR:
1665 default:
1666 return XWM_CURSOR_LEFT_PTR;
1667 }
1668}
1669
1670static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001671weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1672 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001673{
1674 uint32_t cursor_value_list;
1675
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001676 if (wm->last_cursor == cursor)
1677 return;
1678
1679 wm->last_cursor = cursor;
1680
1681 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001682 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001683 XCB_CW_CURSOR, &cursor_value_list);
1684 xcb_flush(wm->conn);
1685}
1686
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001687static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001688weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001689{
1690 xcb_client_message_event_t client_message;
1691
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001692 if (window->delete_window) {
1693 client_message.response_type = XCB_CLIENT_MESSAGE;
1694 client_message.format = 32;
1695 client_message.window = window->id;
1696 client_message.type = window->wm->atom.wm_protocols;
1697 client_message.data.data32[0] =
1698 window->wm->atom.wm_delete_window;
1699 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001700
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001701 xcb_send_event(window->wm->conn, 0, window->id,
1702 XCB_EVENT_MASK_NO_EVENT,
1703 (char *) &client_message);
1704 } else {
1705 xcb_kill_client(window->wm->conn, window->id);
1706 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001707}
1708
1709static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001710weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1711{
1712 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1713 struct weston_shell_interface *shell_interface =
1714 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001715 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001716 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001717 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001718 enum frame_button_state button_state;
1719 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001720
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001721 wm_log("XCB_BUTTON_%s (detail %d)\n",
1722 button->response_type == XCB_BUTTON_PRESS ?
1723 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001724
1725 window = hash_table_lookup(wm->window_hash, button->event);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001726 if (!window || !window->decorate)
1727 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001728
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001729 if (button->detail != 1 && button->detail != 2)
1730 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001731
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001732 seat = weston_wm_pick_seat_for_window(window);
1733
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001734 button_state = button->response_type == XCB_BUTTON_PRESS ?
1735 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1736 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1737
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001738 /* Make sure we're looking at the right location. The frame
1739 * could have received a motion event from a pointer from a
1740 * different wl_seat, but under X it looks like our core
1741 * pointer moved. Move the frame pointer to the button press
1742 * location before deciding what to do. */
1743 location = frame_pointer_motion(window->frame, NULL,
1744 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001745 location = frame_pointer_button(window->frame, NULL,
1746 button_id, button_state);
1747 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1748 weston_wm_window_schedule_repaint(window);
1749
1750 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001751 if (seat != NULL)
1752 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001753 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1754 }
1755
1756 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001757 if (seat != NULL)
1758 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001759 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1760 }
1761
1762 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001763 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001764 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001765 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001766
1767 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1768 window->maximized_horz = !window->maximized_horz;
1769 window->maximized_vert = !window->maximized_vert;
1770 if (weston_wm_window_is_maximized(window)) {
1771 window->saved_width = window->width;
1772 window->saved_height = window->height;
1773 shell_interface->set_maximized(window->shsurf);
1774 } else {
1775 weston_wm_window_set_toplevel(window);
1776 }
1777 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1778 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001779}
1780
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001781static void
1782weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1783{
1784 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1785 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001786 enum theme_location location;
1787 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001788
1789 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001790 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001791 return;
1792
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001793 location = frame_pointer_motion(window->frame, NULL,
1794 motion->event_x, motion->event_y);
1795 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1796 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001797
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001798 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001799 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001800}
1801
1802static void
1803weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1804{
1805 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1806 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001807 enum theme_location location;
1808 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001809
1810 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001811 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001812 return;
1813
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001814 location = frame_pointer_enter(window->frame, NULL,
1815 enter->event_x, enter->event_y);
1816 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1817 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001818
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001819 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001820 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001821}
1822
1823static void
1824weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1825{
1826 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1827 struct weston_wm_window *window;
1828
1829 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001830 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001831 return;
1832
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001833 frame_pointer_leave(window->frame, NULL);
1834 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1835 weston_wm_window_schedule_repaint(window);
1836
Tiago Vignattic1903232012-07-16 12:15:37 -04001837 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001838}
1839
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001840static int
1841weston_wm_handle_event(int fd, uint32_t mask, void *data)
1842{
1843 struct weston_wm *wm = data;
1844 xcb_generic_event_t *event;
1845 int count = 0;
1846
1847 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1848 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001849 free(event);
1850 count++;
1851 continue;
1852 }
1853
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001854 if (weston_wm_handle_dnd_event(wm, event)) {
1855 free(event);
1856 count++;
1857 continue;
1858 }
1859
MoD31700122013-06-11 19:58:55 -05001860 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001861 case XCB_BUTTON_PRESS:
1862 case XCB_BUTTON_RELEASE:
1863 weston_wm_handle_button(wm, event);
1864 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001865 case XCB_ENTER_NOTIFY:
1866 weston_wm_handle_enter(wm, event);
1867 break;
1868 case XCB_LEAVE_NOTIFY:
1869 weston_wm_handle_leave(wm, event);
1870 break;
1871 case XCB_MOTION_NOTIFY:
1872 weston_wm_handle_motion(wm, event);
1873 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001874 case XCB_CREATE_NOTIFY:
1875 weston_wm_handle_create_notify(wm, event);
1876 break;
1877 case XCB_MAP_REQUEST:
1878 weston_wm_handle_map_request(wm, event);
1879 break;
1880 case XCB_MAP_NOTIFY:
1881 weston_wm_handle_map_notify(wm, event);
1882 break;
1883 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001884 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001885 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001886 case XCB_REPARENT_NOTIFY:
1887 weston_wm_handle_reparent_notify(wm, event);
1888 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001889 case XCB_CONFIGURE_REQUEST:
1890 weston_wm_handle_configure_request(wm, event);
1891 break;
1892 case XCB_CONFIGURE_NOTIFY:
1893 weston_wm_handle_configure_notify(wm, event);
1894 break;
1895 case XCB_DESTROY_NOTIFY:
1896 weston_wm_handle_destroy_notify(wm, event);
1897 break;
1898 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001899 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001900 break;
1901 case XCB_PROPERTY_NOTIFY:
1902 weston_wm_handle_property_notify(wm, event);
1903 break;
1904 case XCB_CLIENT_MESSAGE:
1905 weston_wm_handle_client_message(wm, event);
1906 break;
1907 }
1908
1909 free(event);
1910 count++;
1911 }
1912
1913 xcb_flush(wm->conn);
1914
1915 return count;
1916}
1917
1918static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001919weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1920{
1921 xcb_depth_iterator_t d_iter;
1922 xcb_visualtype_iterator_t vt_iter;
1923 xcb_visualtype_t *visualtype;
1924
1925 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1926 visualtype = NULL;
1927 while (d_iter.rem > 0) {
1928 if (d_iter.data->depth == 32) {
1929 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1930 visualtype = vt_iter.data;
1931 break;
1932 }
1933
1934 xcb_depth_next(&d_iter);
1935 }
1936
1937 if (visualtype == NULL) {
1938 weston_log("no 32 bit visualtype\n");
1939 return;
1940 }
1941
1942 wm->visual_id = visualtype->visual_id;
1943 wm->colormap = xcb_generate_id(wm->conn);
1944 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1945 wm->colormap, wm->screen->root, wm->visual_id);
1946}
1947
1948static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001949weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001950{
1951
1952#define F(field) offsetof(struct weston_wm, field)
1953
1954 static const struct { const char *name; int offset; } atoms[] = {
1955 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001956 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001957 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1958 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001959 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001960 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001961 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001962 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001963 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001964 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001965 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1966 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001967 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
1968 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001969 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1970 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1971 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001972 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001973 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1974
1975 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1976 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1977 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1978 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1979 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1980 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1981 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001982 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1983 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001984 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1985 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1986 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1987 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1988 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1989
1990 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1991 { "_NET_SUPPORTING_WM_CHECK",
1992 F(atom.net_supporting_wm_check) },
1993 { "_NET_SUPPORTED", F(atom.net_supported) },
1994 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1995 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001996 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001997 { "TARGETS", F(atom.targets) },
1998 { "UTF8_STRING", F(atom.utf8_string) },
1999 { "_WL_SELECTION", F(atom.wl_selection) },
2000 { "INCR", F(atom.incr) },
2001 { "TIMESTAMP", F(atom.timestamp) },
2002 { "MULTIPLE", F(atom.multiple) },
2003 { "UTF8_STRING" , F(atom.utf8_string) },
2004 { "COMPOUND_TEXT", F(atom.compound_text) },
2005 { "TEXT", F(atom.text) },
2006 { "STRING", F(atom.string) },
2007 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2008 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002009 { "XdndSelection", F(atom.xdnd_selection) },
2010 { "XdndAware", F(atom.xdnd_aware) },
2011 { "XdndEnter", F(atom.xdnd_enter) },
2012 { "XdndLeave", F(atom.xdnd_leave) },
2013 { "XdndDrop", F(atom.xdnd_drop) },
2014 { "XdndStatus", F(atom.xdnd_status) },
2015 { "XdndFinished", F(atom.xdnd_finished) },
2016 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002017 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2018 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002019 };
2020#undef F
2021
2022 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2023 xcb_xfixes_query_version_reply_t *xfixes_reply;
2024 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2025 xcb_intern_atom_reply_t *reply;
2026 xcb_render_query_pict_formats_reply_t *formats_reply;
2027 xcb_render_query_pict_formats_cookie_t formats_cookie;
2028 xcb_render_pictforminfo_t *formats;
2029 uint32_t i;
2030
2031 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002032 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002033
2034 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2035
2036 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2037 cookies[i] = xcb_intern_atom (wm->conn, 0,
2038 strlen(atoms[i].name),
2039 atoms[i].name);
2040
2041 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2042 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2043 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2044 free(reply);
2045 }
2046
2047 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2048 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002049 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002050
2051 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2052 XCB_XFIXES_MAJOR_VERSION,
2053 XCB_XFIXES_MINOR_VERSION);
2054 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2055 xfixes_cookie, NULL);
2056
Martin Minarik6d118362012-06-07 18:01:59 +02002057 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002058 xfixes_reply->major_version, xfixes_reply->minor_version);
2059
2060 free(xfixes_reply);
2061
2062 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2063 formats_cookie, 0);
2064 if (formats_reply == NULL)
2065 return;
2066
2067 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002068 for (i = 0; i < formats_reply->num_formats; i++) {
2069 if (formats[i].direct.red_mask != 0xff &&
2070 formats[i].direct.red_shift != 16)
2071 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002072 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2073 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002074 wm->format_rgb = formats[i];
2075 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2076 formats[i].depth == 32 &&
2077 formats[i].direct.alpha_mask == 0xff &&
2078 formats[i].direct.alpha_shift == 24)
2079 wm->format_rgba = formats[i];
2080 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002081
2082 free(formats_reply);
2083}
2084
2085static void
2086weston_wm_create_wm_window(struct weston_wm *wm)
2087{
2088 static const char name[] = "Weston WM";
2089
2090 wm->wm_window = xcb_generate_id(wm->conn);
2091 xcb_create_window(wm->conn,
2092 XCB_COPY_FROM_PARENT,
2093 wm->wm_window,
2094 wm->screen->root,
2095 0, 0,
2096 10, 10,
2097 0,
2098 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2099 wm->screen->root_visual,
2100 0, NULL);
2101
2102 xcb_change_property(wm->conn,
2103 XCB_PROP_MODE_REPLACE,
2104 wm->wm_window,
2105 wm->atom.net_supporting_wm_check,
2106 XCB_ATOM_WINDOW,
2107 32, /* format */
2108 1, &wm->wm_window);
2109
2110 xcb_change_property(wm->conn,
2111 XCB_PROP_MODE_REPLACE,
2112 wm->wm_window,
2113 wm->atom.net_wm_name,
2114 wm->atom.utf8_string,
2115 8, /* format */
2116 strlen(name), name);
2117
2118 xcb_change_property(wm->conn,
2119 XCB_PROP_MODE_REPLACE,
2120 wm->screen->root,
2121 wm->atom.net_supporting_wm_check,
2122 XCB_ATOM_WINDOW,
2123 32, /* format */
2124 1, &wm->wm_window);
2125
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002126 /* Claim the WM_S0 selection even though we don't suport
2127 * the --replace functionality. */
2128 xcb_set_selection_owner(wm->conn,
2129 wm->wm_window,
2130 wm->atom.wm_s0,
2131 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002132
2133 xcb_set_selection_owner(wm->conn,
2134 wm->wm_window,
2135 wm->atom.net_wm_cm_s0,
2136 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002137}
2138
2139struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002140weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002141{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002142 struct weston_wm *wm;
2143 struct wl_event_loop *loop;
2144 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002145 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002146 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002147
Peter Huttererf3d62272013-08-08 11:57:05 +10002148 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002149 if (wm == NULL)
2150 return NULL;
2151
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002152 wm->server = wxs;
2153 wm->window_hash = hash_table_create();
2154 if (wm->window_hash == NULL) {
2155 free(wm);
2156 return NULL;
2157 }
2158
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002159 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002160 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002161 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002162 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002163 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002164 hash_table_destroy(wm->window_hash);
2165 free(wm);
2166 return NULL;
2167 }
2168
2169 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2170 wm->screen = s.data;
2171
2172 loop = wl_display_get_event_loop(wxs->wl_display);
2173 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002174 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002175 WL_EVENT_READABLE,
2176 weston_wm_handle_event, wm);
2177 wl_event_source_check(wm->source);
2178
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002179 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002180 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002181
2182 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002183 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2184 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2185 XCB_EVENT_MASK_PROPERTY_CHANGE;
2186 xcb_change_window_attributes(wm->conn, wm->screen->root,
2187 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002188
2189 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2190 XCB_COMPOSITE_REDIRECT_MANUAL);
2191
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002192 wm->theme = theme_create();
2193
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002194 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002195 supported[1] = wm->atom.net_wm_state;
2196 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002197 supported[3] = wm->atom.net_wm_state_maximized_vert;
2198 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002199 xcb_change_property(wm->conn,
2200 XCB_PROP_MODE_REPLACE,
2201 wm->screen->root,
2202 wm->atom.net_supported,
2203 XCB_ATOM_ATOM,
2204 32, /* format */
2205 ARRAY_LENGTH(supported), supported);
2206
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002207 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002208
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002209 weston_wm_dnd_init(wm);
2210
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002211 xcb_flush(wm->conn);
2212
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002213 wm->create_surface_listener.notify = weston_wm_create_surface;
2214 wl_signal_add(&wxs->compositor->create_surface_signal,
2215 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002216 wm->activate_listener.notify = weston_wm_window_activate;
2217 wl_signal_add(&wxs->compositor->activate_signal,
2218 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002219 wm->transform_listener.notify = weston_wm_window_transform;
2220 wl_signal_add(&wxs->compositor->transform_signal,
2221 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002222 wm->kill_listener.notify = weston_wm_kill_client;
2223 wl_signal_add(&wxs->compositor->kill_signal,
2224 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002225 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002226
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002227 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002228 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002229
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002230 /* Create wm window and take WM_S0 selection last, which
2231 * signals to Xwayland that we're done with setup. */
2232 weston_wm_create_wm_window(wm);
2233
2234 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002235
2236 return wm;
2237}
2238
2239void
2240weston_wm_destroy(struct weston_wm *wm)
2241{
2242 /* FIXME: Free windows in hash. */
2243 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002244 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002245 xcb_disconnect(wm->conn);
2246 wl_event_source_remove(wm->source);
2247 wl_list_remove(&wm->selection_listener.link);
2248 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002249 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002250 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002251 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002252
2253 free(wm);
2254}
2255
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002256static struct weston_wm_window *
2257get_wm_window(struct weston_surface *surface)
2258{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002259 struct wl_listener *listener;
2260
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002261 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002262 if (listener)
2263 return container_of(listener, struct weston_wm_window,
2264 surface_destroy_listener);
2265
2266 return NULL;
2267}
2268
2269static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002270weston_wm_window_configure(void *data)
2271{
2272 struct weston_wm_window *window = data;
2273 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002274 uint32_t values[4];
2275 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002276
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002277 weston_wm_window_get_child_position(window, &x, &y);
2278 values[0] = x;
2279 values[1] = y;
2280 values[2] = window->width;
2281 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002282 xcb_configure_window(wm->conn,
2283 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002284 XCB_CONFIG_WINDOW_X |
2285 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002286 XCB_CONFIG_WINDOW_WIDTH |
2287 XCB_CONFIG_WINDOW_HEIGHT,
2288 values);
2289
2290 weston_wm_window_get_frame_size(window, &width, &height);
2291 values[0] = width;
2292 values[1] = height;
2293 xcb_configure_window(wm->conn,
2294 window->frame_id,
2295 XCB_CONFIG_WINDOW_WIDTH |
2296 XCB_CONFIG_WINDOW_HEIGHT,
2297 values);
2298
2299 window->configure_source = NULL;
2300
2301 weston_wm_window_schedule_repaint(window);
2302}
2303
2304static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002305send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002306{
2307 struct weston_wm_window *window = get_wm_window(surface);
2308 struct weston_wm *wm = window->wm;
2309 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002310 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002311
Marek Chalupae9fe4672014-10-29 13:44:44 +01002312 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002313 hborder = 2 * t->width;
2314 vborder = t->titlebar_height + t->width;
2315 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002316 hborder = 0;
2317 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002318 }
2319
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002320 if (width > hborder)
2321 window->width = width - hborder;
2322 else
2323 window->width = 1;
2324
2325 if (height > vborder)
2326 window->height = height - vborder;
2327 else
2328 window->height = 1;
2329
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002330 if (window->frame)
2331 frame_resize_inside(window->frame, window->width, window->height);
2332
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002333 if (window->configure_source)
2334 return;
2335
2336 window->configure_source =
2337 wl_event_loop_add_idle(wm->server->loop,
2338 weston_wm_window_configure, window);
2339}
2340
2341static const struct weston_shell_client shell_client = {
2342 send_configure
2343};
2344
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002345static int
2346legacy_fullscreen(struct weston_wm *wm,
2347 struct weston_wm_window *window,
2348 struct weston_output **output_ret)
2349{
2350 struct weston_compositor *compositor = wm->server->compositor;
2351 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002352 uint32_t minmax = PMinSize | PMaxSize;
2353 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002354
2355 /* Heuristics for detecting legacy fullscreen windows... */
2356
2357 wl_list_for_each(output, &compositor->output_list, link) {
2358 if (output->x == window->x &&
2359 output->y == window->y &&
2360 output->width == window->width &&
2361 output->height == window->height &&
2362 window->override_redirect) {
2363 *output_ret = output;
2364 return 1;
2365 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002366
2367 matching_size = 0;
2368 if ((window->size_hints.flags & (USSize |PSize)) &&
2369 window->size_hints.width == output->width &&
2370 window->size_hints.height == output->height)
2371 matching_size = 1;
2372 if ((window->size_hints.flags & minmax) == minmax &&
2373 window->size_hints.min_width == output->width &&
2374 window->size_hints.min_height == output->height &&
2375 window->size_hints.max_width == output->width &&
2376 window->size_hints.max_height == output->height)
2377 matching_size = 1;
2378
2379 if (matching_size && !window->decorate &&
2380 (window->size_hints.flags & (USPosition | PPosition)) &&
2381 window->size_hints.x == output->x &&
2382 window->size_hints.y == output->y) {
2383 *output_ret = output;
2384 return 1;
2385 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002386 }
2387
2388 return 0;
2389}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002390
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002391static bool
2392weston_wm_window_type_inactive(struct weston_wm_window *window)
2393{
2394 struct weston_wm *wm = window->wm;
2395
2396 return window->type == wm->atom.net_wm_window_type_tooltip ||
2397 window->type == wm->atom.net_wm_window_type_dropdown ||
2398 window->type == wm->atom.net_wm_window_type_dnd ||
2399 window->type == wm->atom.net_wm_window_type_combo ||
2400 window->type == wm->atom.net_wm_window_type_popup;
2401}
2402
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002403static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002404xserver_map_shell_surface(struct weston_wm_window *window,
2405 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002406{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002407 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002408 struct weston_shell_interface *shell_interface =
2409 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002410 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002411 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002412 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002413
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002414 weston_wm_window_read_properties(window);
2415
2416 /* A weston_wm_window may have many different surfaces assigned
2417 * throughout its life, so we must make sure to remove the listener
2418 * from the old surface signal list. */
2419 if (window->surface)
2420 wl_list_remove(&window->surface_destroy_listener.link);
2421
2422 window->surface = surface;
2423 window->surface_destroy_listener.notify = surface_destroy;
2424 wl_signal_add(&window->surface->destroy_signal,
2425 &window->surface_destroy_listener);
2426
2427 weston_wm_window_schedule_repaint(window);
2428
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002429 if (!shell_interface->create_shell_surface)
2430 return;
2431
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432 if (!shell_interface->get_primary_view)
2433 return;
2434
Pekka Paalanen50b67472014-10-01 15:02:41 +03002435 if (window->surface->configure) {
2436 weston_log("warning, unexpected in %s: "
2437 "surface's configure hook is already set.\n",
2438 __func__);
2439 return;
2440 }
2441
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002442 window->shsurf =
2443 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002444 window->surface,
2445 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002446 window->view = shell_interface->get_primary_view(shell_interface->shell,
2447 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002448
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002449 if (window->name)
2450 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002451 if (window->pid > 0)
2452 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002453
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002454 if (window->fullscreen) {
2455 window->saved_width = window->width;
2456 window->saved_height = window->height;
2457 shell_interface->set_fullscreen(window->shsurf,
2458 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2459 0, NULL);
2460 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002461 } else if (legacy_fullscreen(wm, window, &output)) {
2462 window->fullscreen = 1;
2463 shell_interface->set_fullscreen(window->shsurf,
2464 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2465 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002466 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002467 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002468 window->x,
2469 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002470 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002471 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002472 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002473 if (weston_wm_window_type_inactive(window))
2474 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002475 shell_interface->set_transient(window->shsurf,
2476 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002477 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002478 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002479 } else if (weston_wm_window_is_maximized(window)) {
2480 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002481 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002482 if (weston_wm_window_type_inactive(window)) {
2483 shell_interface->set_xwayland(window->shsurf,
2484 window->x,
2485 window->y,
2486 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2487 } else {
2488 shell_interface->set_toplevel(window->shsurf);
2489 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002490 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002491}