blob: 07c2ef30bb60a8f89a29823dbd3ed35d3ed23dff [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;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400406
407 if (!window->properties_dirty)
408 return;
409 window->properties_dirty = 0;
410
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 for (i = 0; i < ARRAY_LENGTH(props); i++)
412 cookie[i] = xcb_get_property(wm->conn,
413 0, /* delete */
414 window->id,
415 props[i].atom,
416 XCB_ATOM_ANY, 0, 2048);
417
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300418 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700419 window->size_hints.flags = 0;
420 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700421 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700422
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 for (i = 0; i < ARRAY_LENGTH(props); i++) {
424 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
425 if (!reply)
426 /* Bad window, typically */
427 continue;
428 if (reply->type == XCB_ATOM_NONE) {
429 /* No such property */
430 free(reply);
431 continue;
432 }
433
434 p = ((char *) window + props[i].offset);
435
436 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300437 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400438 case XCB_ATOM_STRING:
439 /* FIXME: We're using this for both string and
440 utf8_string */
441 if (*(char **) p)
442 free(*(char **) p);
443
444 *(char **) p =
445 strndup(xcb_get_property_value(reply),
446 xcb_get_property_value_length(reply));
447 break;
448 case XCB_ATOM_WINDOW:
449 xid = xcb_get_property_value(reply);
450 *(struct weston_wm_window **) p =
451 hash_table_lookup(wm->window_hash, *xid);
452 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300453 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400454 case XCB_ATOM_ATOM:
455 atom = xcb_get_property_value(reply);
456 *(xcb_atom_t *) p = *atom;
457 break;
458 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700459 atom = xcb_get_property_value(reply);
460 for (i = 0; i < reply->value_len; i++)
461 if (atom[i] == wm->atom.wm_delete_window)
462 window->delete_window = 1;
463 break;
464
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400465 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700466 case TYPE_WM_NORMAL_HINTS:
467 memcpy(&window->size_hints,
468 xcb_get_property_value(reply),
469 sizeof window->size_hints);
470 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500471 case TYPE_NET_WM_STATE:
472 window->fullscreen = 0;
473 atom = xcb_get_property_value(reply);
474 for (i = 0; i < reply->value_len; i++)
475 if (atom[i] == wm->atom.net_wm_state_fullscreen)
476 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200477 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
478 window->maximized_vert = 1;
479 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
480 window->maximized_horz = 1;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500481 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400482 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700483 memcpy(&window->motif_hints,
484 xcb_get_property_value(reply),
485 sizeof window->motif_hints);
486 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
487 window->decorate =
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200488 window->motif_hints.decorations;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400489 break;
490 default:
491 break;
492 }
493 free(reply);
494 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200495
496 if (window->shsurf && window->name)
497 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500498 if (window->frame && window->name)
499 frame_set_title(window->frame, window->name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400500}
501
502static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400503weston_wm_window_get_frame_size(struct weston_wm_window *window,
504 int *width, int *height)
505{
506 struct theme *t = window->wm->theme;
507
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500508 if (window->fullscreen) {
509 *width = window->width;
510 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800511 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500512 *width = frame_width(window->frame);
513 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400514 } else {
515 *width = window->width + t->margin * 2;
516 *height = window->height + t->margin * 2;
517 }
518}
519
520static void
521weston_wm_window_get_child_position(struct weston_wm_window *window,
522 int *x, int *y)
523{
524 struct theme *t = window->wm->theme;
525
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500526 if (window->fullscreen) {
527 *x = 0;
528 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800529 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500530 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400531 } else {
532 *x = t->margin;
533 *y = t->margin;
534 }
535}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400536
537static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500538weston_wm_window_send_configure_notify(struct weston_wm_window *window)
539{
540 xcb_configure_notify_event_t configure_notify;
541 struct weston_wm *wm = window->wm;
542 int x, y;
543
544 weston_wm_window_get_child_position(window, &x, &y);
545 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
546 configure_notify.pad0 = 0;
547 configure_notify.event = window->id;
548 configure_notify.window = window->id;
549 configure_notify.above_sibling = XCB_WINDOW_NONE;
550 configure_notify.x = x;
551 configure_notify.y = y;
552 configure_notify.width = window->width;
553 configure_notify.height = window->height;
554 configure_notify.border_width = 0;
555 configure_notify.override_redirect = 0;
556 configure_notify.pad1 = 0;
557
558 xcb_send_event(wm->conn, 0, window->id,
559 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
560 (char *) &configure_notify);
561}
562
563static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400564weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
565{
566 xcb_configure_request_event_t *configure_request =
567 (xcb_configure_request_event_t *) event;
568 struct weston_wm_window *window;
569 uint32_t mask, values[16];
570 int x, y, width, height, i = 0;
571
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400572 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
573 configure_request->window,
574 configure_request->x, configure_request->y,
575 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400576
577 window = hash_table_lookup(wm->window_hash, configure_request->window);
578
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500579 if (window->fullscreen) {
580 weston_wm_window_send_configure_notify(window);
581 return;
582 }
583
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400584 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
585 window->width = configure_request->width;
586 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
587 window->height = configure_request->height;
588
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500589 if (window->frame)
590 frame_resize_inside(window->frame, window->width, window->height);
591
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400592 weston_wm_window_get_child_position(window, &x, &y);
593 values[i++] = x;
594 values[i++] = y;
595 values[i++] = window->width;
596 values[i++] = window->height;
597 values[i++] = 0;
598 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
599 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
600 XCB_CONFIG_WINDOW_BORDER_WIDTH;
601 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
602 values[i++] = configure_request->sibling;
603 mask |= XCB_CONFIG_WINDOW_SIBLING;
604 }
605 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
606 values[i++] = configure_request->stack_mode;
607 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
608 }
609
610 xcb_configure_window(wm->conn, window->id, mask, values);
611
612 weston_wm_window_get_frame_size(window, &width, &height);
613 values[0] = width;
614 values[1] = height;
615 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
616 xcb_configure_window(wm->conn, window->frame_id, mask, values);
617
618 weston_wm_window_schedule_repaint(window);
619}
620
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400621static int
622our_resource(struct weston_wm *wm, uint32_t id)
623{
624 const xcb_setup_t *setup;
625
626 setup = xcb_get_setup(wm->conn);
627
628 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
629}
630
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400631static void
632weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
633{
634 xcb_configure_notify_event_t *configure_notify =
635 (xcb_configure_notify_event_t *) event;
636 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400637
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400638 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400639 configure_notify->window,
640 configure_notify->x, configure_notify->y,
641 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300642
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400643 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700644 window->x = configure_notify->x;
645 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700646 if (window->override_redirect) {
647 window->width = configure_notify->width;
648 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500649 if (window->frame)
650 frame_resize_inside(window->frame,
651 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700652 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400653}
654
655static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300656weston_wm_kill_client(struct wl_listener *listener, void *data)
657{
658 struct weston_surface *surface = data;
659 struct weston_wm_window *window = get_wm_window(surface);
660 char name[1024];
661
662 if (!window)
663 return;
664
665 gethostname(name, 1024);
666
667 /* this is only one heuristic to guess the PID of a client is valid,
668 * assuming it's compliant with icccm and ewmh. Non-compliants and
669 * remote applications of course fail. */
670 if (!strcmp(window->machine, name) && window->pid != 0)
671 kill(window->pid, SIGKILL);
672}
673
674static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700675weston_wm_create_surface(struct wl_listener *listener, void *data)
676{
677 struct weston_surface *surface = data;
678 struct weston_wm *wm =
679 container_of(listener,
680 struct weston_wm, create_surface_listener);
681 struct weston_wm_window *window;
682
683 if (wl_resource_get_client(surface->resource) != wm->server->client)
684 return;
685
686 wl_list_for_each(window, &wm->unpaired_window_list, link)
687 if (window->surface_id ==
688 wl_resource_get_id(surface->resource)) {
689 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700690 window->surface_id = 0;
691 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700692 break;
693 }
694}
695
696static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400697weston_wm_window_activate(struct wl_listener *listener, void *data)
698{
699 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200700 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600701 struct weston_wm *wm =
702 container_of(listener, struct weston_wm, activate_listener);
703 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400704
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200705 if (surface) {
706 window = get_wm_window(surface);
707 }
708
Scott Moreau85ecac02012-05-21 15:49:14 -0600709 if (window) {
Boyan Dingb9f863c2014-08-30 10:33:23 +0800710 if (window->override_redirect)
711 return;
712
Scott Moreau85ecac02012-05-21 15:49:14 -0600713 client_message.response_type = XCB_CLIENT_MESSAGE;
714 client_message.format = 32;
715 client_message.window = window->id;
716 client_message.type = wm->atom.wm_protocols;
717 client_message.data.data32[0] = wm->atom.wm_take_focus;
718 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
719
720 xcb_send_event(wm->conn, 0, window->id,
721 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
722 (char *) &client_message);
723
724 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
725 window->id, XCB_TIME_CURRENT_TIME);
726 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400727 xcb_set_input_focus (wm->conn,
728 XCB_INPUT_FOCUS_POINTER_ROOT,
729 XCB_NONE,
730 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600731 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500733 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800734 if (wm->focus_window->frame)
735 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400736 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500737 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400738 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500739 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800740 if (wm->focus_window->frame)
741 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400742 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500743 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400744}
745
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300746static void
747weston_wm_window_transform(struct wl_listener *listener, void *data)
748{
749 struct weston_surface *surface = data;
750 struct weston_wm_window *window = get_wm_window(surface);
751 struct weston_wm *wm =
752 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300753 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300754
755 if (!window || !wm)
756 return;
757
Jason Ekstranda7af7042013-10-12 22:38:11 -0500758 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300759 return;
760
Jason Ekstranda7af7042013-10-12 22:38:11 -0500761 if (window->x != window->view->geometry.x ||
762 window->y != window->view->geometry.y) {
763 values[0] = window->view->geometry.x;
764 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700765 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300766
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700767 xcb_configure_window(wm->conn, window->frame_id, mask, values);
768 xcb_flush(wm->conn);
769 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300770}
771
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400772#define ICCCM_WITHDRAWN_STATE 0
773#define ICCCM_NORMAL_STATE 1
774#define ICCCM_ICONIC_STATE 3
775
776static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500777weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400778{
779 struct weston_wm *wm = window->wm;
780 uint32_t property[2];
781
782 property[0] = state;
783 property[1] = XCB_WINDOW_NONE;
784
785 xcb_change_property(wm->conn,
786 XCB_PROP_MODE_REPLACE,
787 window->id,
788 wm->atom.wm_state,
789 wm->atom.wm_state,
790 32, /* format */
791 2, property);
792}
793
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400794static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500795weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
796{
797 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200798 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500799 int i;
800
801 i = 0;
802 if (window->fullscreen)
803 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200804 if (window->maximized_vert)
805 property[i++] = wm->atom.net_wm_state_maximized_vert;
806 if (window->maximized_horz)
807 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500808
809 xcb_change_property(wm->conn,
810 XCB_PROP_MODE_REPLACE,
811 window->id,
812 wm->atom.net_wm_state,
813 XCB_ATOM_ATOM,
814 32, /* format */
815 i, property);
816}
817
818static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700819weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400820{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700821 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400822 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400823 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200824 int buttons = FRAME_BUTTON_CLOSE;
825
826 if (window->decorate & MWM_DECOR_MAXIMIZE)
827 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400828
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500829 window->frame = frame_create(window->wm->theme,
830 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200831 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500832 frame_resize_inside(window->frame, window->width, window->height);
833
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400834 weston_wm_window_get_frame_size(window, &width, &height);
835 weston_wm_window_get_child_position(window, &x, &y);
836
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400837 values[0] = wm->screen->black_pixel;
838 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839 XCB_EVENT_MASK_KEY_PRESS |
840 XCB_EVENT_MASK_KEY_RELEASE |
841 XCB_EVENT_MASK_BUTTON_PRESS |
842 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400843 XCB_EVENT_MASK_POINTER_MOTION |
844 XCB_EVENT_MASK_ENTER_WINDOW |
845 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400846 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400847 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400848 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400849
850 window->frame_id = xcb_generate_id(wm->conn);
851 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400852 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400853 window->frame_id,
854 wm->screen->root,
855 0, 0,
856 width, height,
857 0,
858 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400859 wm->visual_id,
860 XCB_CW_BORDER_PIXEL |
861 XCB_CW_EVENT_MASK |
862 XCB_CW_COLORMAP, values);
863
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400864 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
865
866 values[0] = 0;
867 xcb_configure_window(wm->conn, window->id,
868 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
869
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400870 window->cairo_surface =
871 cairo_xcb_surface_create_with_xrender_format(wm->conn,
872 wm->screen,
873 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400874 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400875 width, height);
876
877 hash_table_insert(wm->window_hash, window->frame_id, window);
878}
879
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200880/*
881 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
882 * Passing a <0 desktop value deletes the property.
883 */
884static void
885weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
886 int desktop)
887{
888 if (desktop >= 0) {
889 xcb_change_property(window->wm->conn,
890 XCB_PROP_MODE_REPLACE,
891 window->id,
892 window->wm->atom.net_wm_desktop,
893 XCB_ATOM_CARDINAL,
894 32, /* format */
895 1, &desktop);
896 } else {
897 xcb_delete_property(window->wm->conn,
898 window->id,
899 window->wm->atom.net_wm_desktop);
900 }
901}
902
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400903static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700904weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
905{
906 xcb_map_request_event_t *map_request =
907 (xcb_map_request_event_t *) event;
908 struct weston_wm_window *window;
909
910 if (our_resource(wm, map_request->window)) {
911 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
912 map_request->window);
913 return;
914 }
915
916 window = hash_table_lookup(wm->window_hash, map_request->window);
917
918 weston_wm_window_read_properties(window);
919
920 if (window->frame_id == XCB_WINDOW_NONE)
921 weston_wm_window_create_frame(window);
922
923 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
924 window->id, window, window->frame_id);
925
926 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
927 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200928 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700929
930 xcb_map_window(wm->conn, map_request->window);
931 xcb_map_window(wm->conn, window->frame_id);
932}
933
934static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400935weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
936{
937 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
938
939 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400940 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
941 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400942 return;
943 }
944
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400945 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400946}
947
948static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400949weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
950{
951 xcb_unmap_notify_event_t *unmap_notify =
952 (xcb_unmap_notify_event_t *) event;
953 struct weston_wm_window *window;
954
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400955 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
956 unmap_notify->window,
957 unmap_notify->event,
958 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400959
960 if (our_resource(wm, unmap_notify->window))
961 return;
962
MoD31700122013-06-11 19:58:55 -0500963 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400964 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
965 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400966 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400967
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400968 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400969 if (wm->focus_window == window)
970 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400971 if (window->surface)
972 wl_list_remove(&window->surface_destroy_listener.link);
973 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200974 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -0800975 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200976
977 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
978 weston_wm_window_set_virtual_desktop(window, -1);
979
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700980 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400981}
982
983static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400984weston_wm_window_draw_decoration(void *data)
985{
986 struct weston_wm_window *window = data;
987 struct weston_wm *wm = window->wm;
988 struct theme *t = wm->theme;
989 cairo_t *cr;
990 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500991 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -0700992 struct weston_shell_interface *shell_interface =
993 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400994 uint32_t flags = 0;
995
996 weston_wm_window_read_properties(window);
997
998 window->repaint_source = NULL;
999
1000 weston_wm_window_get_frame_size(window, &width, &height);
1001 weston_wm_window_get_child_position(window, &x, &y);
1002
1003 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1004 cr = cairo_create(window->cairo_surface);
1005
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001006 if (window->fullscreen) {
1007 /* nothing */
1008 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001009 if (wm->focus_window == window)
1010 flags |= THEME_FRAME_ACTIVE;
1011
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001012 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001013 } else {
1014 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1015 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1016 cairo_paint(cr);
1017
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001018 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001019 }
1020
1021 cairo_destroy(cr);
1022
1023 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001024 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001025 if(window->has_alpha) {
1026 pixman_region32_init(&window->surface->pending.opaque);
1027 } else {
1028 /* We leave an extra pixel around the X window area to
1029 * make sure we don't sample from the undefined alpha
1030 * channel when filtering. */
1031 pixman_region32_init_rect(&window->surface->pending.opaque,
1032 x - 1, y - 1,
1033 window->width + 2,
1034 window->height + 2);
1035 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 if (window->view)
1037 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001038
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001039 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001040
1041 if (window->fullscreen) {
1042 input_x = 0;
1043 input_y = 0;
1044 input_w = window->width;
1045 input_h = window->height;
1046 } else if (window->decorate) {
1047 frame_input_rect(window->frame, &input_x, &input_y,
1048 &input_w, &input_h);
1049 }
1050
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001051 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001052 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001053
1054 shell_interface->set_window_geometry(window->shsurf,
1055 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001056 }
1057}
1058
1059static void
1060weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1061{
1062 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001063 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001064
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001065 if (window->frame_id == XCB_WINDOW_NONE) {
1066 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001067 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001068 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001069 if(window->has_alpha) {
1070 pixman_region32_init(&window->surface->pending.opaque);
1071 } else {
1072 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1073 width, height);
1074 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 if (window->view)
1076 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001077 }
1078 return;
1079 }
1080
1081 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001082 return;
1083
1084 window->repaint_source =
1085 wl_event_loop_add_idle(wm->server->loop,
1086 weston_wm_window_draw_decoration,
1087 window);
1088}
1089
1090static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001091weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1092{
1093 xcb_property_notify_event_t *property_notify =
1094 (xcb_property_notify_event_t *) event;
1095 struct weston_wm_window *window;
1096
1097 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001098 if (!window)
1099 return;
1100
1101 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001102
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001103 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001104 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001105 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001106 else
1107 read_and_dump_property(wm, property_notify->window,
1108 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001109
1110 if (property_notify->atom == wm->atom.net_wm_name ||
1111 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001112 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001113}
1114
1115static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001116weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001117 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001118{
1119 struct weston_wm_window *window;
1120 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001121 xcb_get_geometry_cookie_t geometry_cookie;
1122 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001123
Peter Huttererf3d62272013-08-08 11:57:05 +10001124 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001125 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001126 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001127 return;
1128 }
1129
MoD384a11a2013-06-22 11:04:21 -05001130 geometry_cookie = xcb_get_geometry(wm->conn, id);
1131
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001132 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1133 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1134
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001135 window->wm = wm;
1136 window->id = id;
1137 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001138 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001139 window->width = width;
1140 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001141 window->x = x;
1142 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001143
MoD384a11a2013-06-22 11:04:21 -05001144 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1145 /* technically we should use XRender and check the visual format's
1146 alpha_mask, but checking depth is simpler and works in all known cases */
1147 if(geometry_reply != NULL)
1148 window->has_alpha = geometry_reply->depth == 32;
1149 free(geometry_reply);
1150
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001151 hash_table_insert(wm->window_hash, id, window);
1152}
1153
1154static void
1155weston_wm_window_destroy(struct weston_wm_window *window)
1156{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001157 struct weston_wm *wm = window->wm;
1158
1159 if (window->repaint_source)
1160 wl_event_source_remove(window->repaint_source);
1161 if (window->cairo_surface)
1162 cairo_surface_destroy(window->cairo_surface);
1163
1164 if (window->frame_id) {
1165 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1166 xcb_destroy_window(wm->conn, window->frame_id);
1167 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001168 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001169 hash_table_remove(wm->window_hash, window->frame_id);
1170 window->frame_id = XCB_WINDOW_NONE;
1171 }
1172
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001173 if (window->surface_id)
1174 wl_list_remove(&window->link);
1175
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001176 hash_table_remove(window->wm->window_hash, window->id);
1177 free(window);
1178}
1179
1180static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001181weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1182{
1183 xcb_create_notify_event_t *create_notify =
1184 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001185
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001186 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1187 create_notify->window,
1188 create_notify->width, create_notify->height,
1189 create_notify->override_redirect ? ", override" : "",
1190 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001191
1192 if (our_resource(wm, create_notify->window))
1193 return;
1194
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001195 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001196 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001197 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001198 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001199}
1200
1201static void
1202weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1203{
1204 xcb_destroy_notify_event_t *destroy_notify =
1205 (xcb_destroy_notify_event_t *) event;
1206 struct weston_wm_window *window;
1207
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001208 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1209 destroy_notify->window,
1210 destroy_notify->event,
1211 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001212
1213 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001214 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001215
1216 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001217 weston_wm_window_destroy(window);
1218}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001219
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001220static void
1221weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1222{
1223 xcb_reparent_notify_event_t *reparent_notify =
1224 (xcb_reparent_notify_event_t *) event;
1225 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001226
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001227 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1228 reparent_notify->window,
1229 reparent_notify->parent,
1230 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001231
1232 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001233 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001234 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001235 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001236 } else if (!our_resource(wm, reparent_notify->parent)) {
1237 window = hash_table_lookup(wm->window_hash,
1238 reparent_notify->window);
1239 weston_wm_window_destroy(window);
1240 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001241}
1242
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001243struct weston_seat *
1244weston_wm_pick_seat(struct weston_wm *wm)
1245{
1246 return container_of(wm->server->compositor->seat_list.next,
1247 struct weston_seat, link);
1248}
1249
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001250static struct weston_seat *
1251weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1252{
1253 struct weston_wm *wm = window->wm;
1254 struct weston_seat *seat, *s;
1255
1256 seat = NULL;
1257 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001258 if (s->pointer != NULL && s->pointer->focus &&
1259 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001260 s->pointer->button_count > 0 &&
1261 (seat == NULL ||
1262 s->pointer->grab_serial -
1263 seat->pointer->grab_serial < (1 << 30)))
1264 seat = s;
1265 }
1266
1267 return seat;
1268}
1269
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001270static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001271weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1272 xcb_client_message_event_t *client_message)
1273{
1274 static const int map[] = {
1275 THEME_LOCATION_RESIZING_TOP_LEFT,
1276 THEME_LOCATION_RESIZING_TOP,
1277 THEME_LOCATION_RESIZING_TOP_RIGHT,
1278 THEME_LOCATION_RESIZING_RIGHT,
1279 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1280 THEME_LOCATION_RESIZING_BOTTOM,
1281 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1282 THEME_LOCATION_RESIZING_LEFT
1283 };
1284
1285 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001286 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001287 int detail;
1288 struct weston_shell_interface *shell_interface =
1289 &wm->server->compositor->shell_interface;
1290
Boyan Dingc06a1802014-07-06 11:44:58 +08001291 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001292 || !seat->pointer->focus
1293 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001294 return;
1295
1296 detail = client_message->data.data32[2];
1297 switch (detail) {
1298 case _NET_WM_MOVERESIZE_MOVE:
1299 shell_interface->move(window->shsurf, seat);
1300 break;
1301 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1302 case _NET_WM_MOVERESIZE_SIZE_TOP:
1303 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1304 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1305 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1306 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1307 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1308 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1309 shell_interface->resize(window->shsurf, seat, map[detail]);
1310 break;
1311 case _NET_WM_MOVERESIZE_CANCEL:
1312 break;
1313 }
1314}
1315
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001316#define _NET_WM_STATE_REMOVE 0
1317#define _NET_WM_STATE_ADD 1
1318#define _NET_WM_STATE_TOGGLE 2
1319
1320static int
1321update_state(int action, int *state)
1322{
1323 int new_state, changed;
1324
1325 switch (action) {
1326 case _NET_WM_STATE_REMOVE:
1327 new_state = 0;
1328 break;
1329 case _NET_WM_STATE_ADD:
1330 new_state = 1;
1331 break;
1332 case _NET_WM_STATE_TOGGLE:
1333 new_state = !*state;
1334 break;
1335 default:
1336 return 0;
1337 }
1338
1339 changed = (*state != new_state);
1340 *state = new_state;
1341
1342 return changed;
1343}
1344
1345static void
1346weston_wm_window_configure(void *data);
1347
1348static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001349weston_wm_window_set_toplevel(struct weston_wm_window *window)
1350{
1351 struct weston_shell_interface *shell_interface =
1352 &window->wm->server->compositor->shell_interface;
1353
1354 shell_interface->set_toplevel(window->shsurf);
1355 window->width = window->saved_width;
1356 window->height = window->saved_height;
1357 if (window->frame)
1358 frame_resize_inside(window->frame,
1359 window->width,
1360 window->height);
1361 weston_wm_window_configure(window);
1362}
1363
1364static inline bool
1365weston_wm_window_is_maximized(struct weston_wm_window *window)
1366{
1367 return window->maximized_horz && window->maximized_vert;
1368}
1369
1370static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001371weston_wm_window_handle_state(struct weston_wm_window *window,
1372 xcb_client_message_event_t *client_message)
1373{
1374 struct weston_wm *wm = window->wm;
1375 struct weston_shell_interface *shell_interface =
1376 &wm->server->compositor->shell_interface;
1377 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001378 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001379
1380 action = client_message->data.data32[0];
1381 property = client_message->data.data32[1];
1382
1383 if (property == wm->atom.net_wm_state_fullscreen &&
1384 update_state(action, &window->fullscreen)) {
1385 weston_wm_window_set_net_wm_state(window);
1386 if (window->fullscreen) {
1387 window->saved_width = window->width;
1388 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001389
1390 if (window->shsurf)
1391 shell_interface->set_fullscreen(window->shsurf,
1392 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1393 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001394 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001395 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001396 weston_wm_window_set_toplevel(window);
1397 }
1398 } else {
1399 if (property == wm->atom.net_wm_state_maximized_vert &&
1400 update_state(action, &window->maximized_vert))
1401 weston_wm_window_set_net_wm_state(window);
1402 if (property == wm->atom.net_wm_state_maximized_horz &&
1403 update_state(action, &window->maximized_horz))
1404 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001405
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001406 if (maximized != weston_wm_window_is_maximized(window)) {
1407 if (weston_wm_window_is_maximized(window)) {
1408 window->saved_width = window->width;
1409 window->saved_height = window->height;
1410
1411 if (window->shsurf)
1412 shell_interface->set_maximized(window->shsurf);
1413 } else if (window->shsurf) {
1414 weston_wm_window_set_toplevel(window);
1415 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001416 }
1417 }
1418}
1419
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001420static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001421surface_destroy(struct wl_listener *listener, void *data)
1422{
1423 struct weston_wm_window *window =
1424 container_of(listener,
1425 struct weston_wm_window, surface_destroy_listener);
1426
1427 wm_log("surface for xid %d destroyed\n", window->id);
1428
1429 /* This should have been freed by the shell.
1430 * Don't try to use it later. */
1431 window->shsurf = NULL;
1432 window->surface = NULL;
1433 window->view = NULL;
1434}
1435
1436static void
1437weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1438 xcb_client_message_event_t *client_message)
1439{
1440 struct weston_wm *wm = window->wm;
1441 struct wl_resource *resource;
1442
1443 if (window->surface_id != 0) {
1444 wm_log("already have surface id for window %d\n", window->id);
1445 return;
1446 }
1447
1448 /* Xwayland will send the wayland requests to create the
1449 * wl_surface before sending this client message. Even so, we
1450 * can end up handling the X event before the wayland requests
1451 * and thus when we try to look up the surface ID, the surface
1452 * hasn't been created yet. In that case put the window on
1453 * the unpaired window list and continue when the surface gets
1454 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001455 uint32_t id = client_message->data.data32[0];
1456 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001457 if (resource) {
1458 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001459 xserver_map_shell_surface(window,
1460 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001461 }
1462 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001463 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001464 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001465 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001466}
1467
1468static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001469weston_wm_handle_client_message(struct weston_wm *wm,
1470 xcb_generic_event_t *event)
1471{
1472 xcb_client_message_event_t *client_message =
1473 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001474 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001475
1476 window = hash_table_lookup(wm->window_hash, client_message->window);
1477
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001478 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1479 get_atom_name(wm->conn, client_message->type),
1480 client_message->data.data32[0],
1481 client_message->data.data32[1],
1482 client_message->data.data32[2],
1483 client_message->data.data32[3],
1484 client_message->data.data32[4],
1485 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001486
Jason Ekstrand0250a742014-07-24 13:17:47 -07001487 /* The window may get created and destroyed before we actually
1488 * handle the message. If it doesn't exist, bail.
1489 */
1490 if (!window)
1491 return;
1492
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001493 if (client_message->type == wm->atom.net_wm_moveresize)
1494 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001495 else if (client_message->type == wm->atom.net_wm_state)
1496 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001497 else if (client_message->type == wm->atom.wl_surface_id)
1498 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001499}
1500
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001501enum cursor_type {
1502 XWM_CURSOR_TOP,
1503 XWM_CURSOR_BOTTOM,
1504 XWM_CURSOR_LEFT,
1505 XWM_CURSOR_RIGHT,
1506 XWM_CURSOR_TOP_LEFT,
1507 XWM_CURSOR_TOP_RIGHT,
1508 XWM_CURSOR_BOTTOM_LEFT,
1509 XWM_CURSOR_BOTTOM_RIGHT,
1510 XWM_CURSOR_LEFT_PTR,
1511};
1512
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001513/*
1514 * The following correspondences between file names and cursors was copied
1515 * from: https://bugs.kde.org/attachment.cgi?id=67313
1516 */
1517
1518static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001519 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001520 "sw-resize",
1521 "size_bdiag"
1522};
1523
1524static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001525 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001526 "se-resize",
1527 "size_fdiag"
1528};
1529
1530static const char *bottom_sides[] = {
1531 "bottom_side",
1532 "s-resize",
1533 "size_ver"
1534};
1535
1536static const char *left_ptrs[] = {
1537 "left_ptr",
1538 "default",
1539 "top_left_arrow",
1540 "left-arrow"
1541};
1542
1543static const char *left_sides[] = {
1544 "left_side",
1545 "w-resize",
1546 "size_hor"
1547};
1548
1549static const char *right_sides[] = {
1550 "right_side",
1551 "e-resize",
1552 "size_hor"
1553};
1554
1555static const char *top_left_corners[] = {
1556 "top_left_corner",
1557 "nw-resize",
1558 "size_fdiag"
1559};
1560
1561static const char *top_right_corners[] = {
1562 "top_right_corner",
1563 "ne-resize",
1564 "size_bdiag"
1565};
1566
1567static const char *top_sides[] = {
1568 "top_side",
1569 "n-resize",
1570 "size_ver"
1571};
1572
1573struct cursor_alternatives {
1574 const char **names;
1575 size_t count;
1576};
1577
1578static const struct cursor_alternatives cursors[] = {
1579 {top_sides, ARRAY_LENGTH(top_sides)},
1580 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1581 {left_sides, ARRAY_LENGTH(left_sides)},
1582 {right_sides, ARRAY_LENGTH(right_sides)},
1583 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1584 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1585 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1586 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1587 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001588};
1589
1590static void
1591weston_wm_create_cursors(struct weston_wm *wm)
1592{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001593 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001594 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001595 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001596
1597 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1598 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001599 for (j = 0; j < cursors[i].count; j++) {
1600 name = cursors[i].names[j];
1601 wm->cursors[i] =
1602 xcb_cursor_library_load_cursor(wm, name);
1603 if (wm->cursors[i] != (xcb_cursor_t)-1)
1604 break;
1605 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001606 }
1607
1608 wm->last_cursor = -1;
1609}
1610
1611static void
1612weston_wm_destroy_cursors(struct weston_wm *wm)
1613{
1614 uint8_t i;
1615
1616 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1617 xcb_free_cursor(wm->conn, wm->cursors[i]);
1618
1619 free(wm->cursors);
1620}
1621
1622static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001623get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001624{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001625 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001626
1627 switch (location) {
1628 case THEME_LOCATION_RESIZING_TOP:
1629 return XWM_CURSOR_TOP;
1630 case THEME_LOCATION_RESIZING_BOTTOM:
1631 return XWM_CURSOR_BOTTOM;
1632 case THEME_LOCATION_RESIZING_LEFT:
1633 return XWM_CURSOR_LEFT;
1634 case THEME_LOCATION_RESIZING_RIGHT:
1635 return XWM_CURSOR_RIGHT;
1636 case THEME_LOCATION_RESIZING_TOP_LEFT:
1637 return XWM_CURSOR_TOP_LEFT;
1638 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1639 return XWM_CURSOR_TOP_RIGHT;
1640 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1641 return XWM_CURSOR_BOTTOM_LEFT;
1642 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1643 return XWM_CURSOR_BOTTOM_RIGHT;
1644 case THEME_LOCATION_EXTERIOR:
1645 case THEME_LOCATION_TITLEBAR:
1646 default:
1647 return XWM_CURSOR_LEFT_PTR;
1648 }
1649}
1650
1651static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001652weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1653 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001654{
1655 uint32_t cursor_value_list;
1656
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001657 if (wm->last_cursor == cursor)
1658 return;
1659
1660 wm->last_cursor = cursor;
1661
1662 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001663 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001664 XCB_CW_CURSOR, &cursor_value_list);
1665 xcb_flush(wm->conn);
1666}
1667
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001668static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001669weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001670{
1671 xcb_client_message_event_t client_message;
1672
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001673 if (window->delete_window) {
1674 client_message.response_type = XCB_CLIENT_MESSAGE;
1675 client_message.format = 32;
1676 client_message.window = window->id;
1677 client_message.type = window->wm->atom.wm_protocols;
1678 client_message.data.data32[0] =
1679 window->wm->atom.wm_delete_window;
1680 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001681
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001682 xcb_send_event(window->wm->conn, 0, window->id,
1683 XCB_EVENT_MASK_NO_EVENT,
1684 (char *) &client_message);
1685 } else {
1686 xcb_kill_client(window->wm->conn, window->id);
1687 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001688}
1689
1690static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001691weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1692{
1693 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1694 struct weston_shell_interface *shell_interface =
1695 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001696 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001697 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001698 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001699 enum frame_button_state button_state;
1700 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001701
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001702 wm_log("XCB_BUTTON_%s (detail %d)\n",
1703 button->response_type == XCB_BUTTON_PRESS ?
1704 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001705
1706 window = hash_table_lookup(wm->window_hash, button->event);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001707 if (!window || !window->decorate)
1708 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001709
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001710 if (button->detail != 1 && button->detail != 2)
1711 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001712
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001713 seat = weston_wm_pick_seat_for_window(window);
1714
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001715 button_state = button->response_type == XCB_BUTTON_PRESS ?
1716 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1717 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1718
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001719 /* Make sure we're looking at the right location. The frame
1720 * could have received a motion event from a pointer from a
1721 * different wl_seat, but under X it looks like our core
1722 * pointer moved. Move the frame pointer to the button press
1723 * location before deciding what to do. */
1724 location = frame_pointer_motion(window->frame, NULL,
1725 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001726 location = frame_pointer_button(window->frame, NULL,
1727 button_id, button_state);
1728 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1729 weston_wm_window_schedule_repaint(window);
1730
1731 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001732 if (seat != NULL)
1733 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001734 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1735 }
1736
1737 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001738 if (seat != NULL)
1739 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001740 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1741 }
1742
1743 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001744 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001745 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001746 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001747
1748 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1749 window->maximized_horz = !window->maximized_horz;
1750 window->maximized_vert = !window->maximized_vert;
1751 if (weston_wm_window_is_maximized(window)) {
1752 window->saved_width = window->width;
1753 window->saved_height = window->height;
1754 shell_interface->set_maximized(window->shsurf);
1755 } else {
1756 weston_wm_window_set_toplevel(window);
1757 }
1758 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1759 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760}
1761
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001762static void
1763weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1764{
1765 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1766 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001767 enum theme_location location;
1768 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001769
1770 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001771 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001772 return;
1773
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001774 location = frame_pointer_motion(window->frame, NULL,
1775 motion->event_x, motion->event_y);
1776 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1777 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001778
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001779 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001780 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001781}
1782
1783static void
1784weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1785{
1786 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1787 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001788 enum theme_location location;
1789 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001790
1791 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001792 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001793 return;
1794
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001795 location = frame_pointer_enter(window->frame, NULL,
1796 enter->event_x, enter->event_y);
1797 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1798 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001799
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001800 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001801 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001802}
1803
1804static void
1805weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1806{
1807 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1808 struct weston_wm_window *window;
1809
1810 window = hash_table_lookup(wm->window_hash, leave->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 frame_pointer_leave(window->frame, NULL);
1815 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1816 weston_wm_window_schedule_repaint(window);
1817
Tiago Vignattic1903232012-07-16 12:15:37 -04001818 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001819}
1820
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001821static int
1822weston_wm_handle_event(int fd, uint32_t mask, void *data)
1823{
1824 struct weston_wm *wm = data;
1825 xcb_generic_event_t *event;
1826 int count = 0;
1827
1828 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1829 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001830 free(event);
1831 count++;
1832 continue;
1833 }
1834
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001835 if (weston_wm_handle_dnd_event(wm, event)) {
1836 free(event);
1837 count++;
1838 continue;
1839 }
1840
MoD31700122013-06-11 19:58:55 -05001841 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001842 case XCB_BUTTON_PRESS:
1843 case XCB_BUTTON_RELEASE:
1844 weston_wm_handle_button(wm, event);
1845 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001846 case XCB_ENTER_NOTIFY:
1847 weston_wm_handle_enter(wm, event);
1848 break;
1849 case XCB_LEAVE_NOTIFY:
1850 weston_wm_handle_leave(wm, event);
1851 break;
1852 case XCB_MOTION_NOTIFY:
1853 weston_wm_handle_motion(wm, event);
1854 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001855 case XCB_CREATE_NOTIFY:
1856 weston_wm_handle_create_notify(wm, event);
1857 break;
1858 case XCB_MAP_REQUEST:
1859 weston_wm_handle_map_request(wm, event);
1860 break;
1861 case XCB_MAP_NOTIFY:
1862 weston_wm_handle_map_notify(wm, event);
1863 break;
1864 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001865 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001866 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001867 case XCB_REPARENT_NOTIFY:
1868 weston_wm_handle_reparent_notify(wm, event);
1869 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001870 case XCB_CONFIGURE_REQUEST:
1871 weston_wm_handle_configure_request(wm, event);
1872 break;
1873 case XCB_CONFIGURE_NOTIFY:
1874 weston_wm_handle_configure_notify(wm, event);
1875 break;
1876 case XCB_DESTROY_NOTIFY:
1877 weston_wm_handle_destroy_notify(wm, event);
1878 break;
1879 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001880 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001881 break;
1882 case XCB_PROPERTY_NOTIFY:
1883 weston_wm_handle_property_notify(wm, event);
1884 break;
1885 case XCB_CLIENT_MESSAGE:
1886 weston_wm_handle_client_message(wm, event);
1887 break;
1888 }
1889
1890 free(event);
1891 count++;
1892 }
1893
1894 xcb_flush(wm->conn);
1895
1896 return count;
1897}
1898
1899static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001900weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1901{
1902 xcb_depth_iterator_t d_iter;
1903 xcb_visualtype_iterator_t vt_iter;
1904 xcb_visualtype_t *visualtype;
1905
1906 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1907 visualtype = NULL;
1908 while (d_iter.rem > 0) {
1909 if (d_iter.data->depth == 32) {
1910 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1911 visualtype = vt_iter.data;
1912 break;
1913 }
1914
1915 xcb_depth_next(&d_iter);
1916 }
1917
1918 if (visualtype == NULL) {
1919 weston_log("no 32 bit visualtype\n");
1920 return;
1921 }
1922
1923 wm->visual_id = visualtype->visual_id;
1924 wm->colormap = xcb_generate_id(wm->conn);
1925 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1926 wm->colormap, wm->screen->root, wm->visual_id);
1927}
1928
1929static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001930weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001931{
1932
1933#define F(field) offsetof(struct weston_wm, field)
1934
1935 static const struct { const char *name; int offset; } atoms[] = {
1936 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001937 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001938 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1939 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001940 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001941 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001942 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001943 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001944 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001945 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001946 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1947 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001948 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
1949 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001950 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1951 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1952 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001953 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001954 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1955
1956 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1957 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1958 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1959 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1960 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1961 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1962 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001963 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1964 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001965 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1966 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1967 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1968 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1969 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1970
1971 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1972 { "_NET_SUPPORTING_WM_CHECK",
1973 F(atom.net_supporting_wm_check) },
1974 { "_NET_SUPPORTED", F(atom.net_supported) },
1975 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1976 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001977 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001978 { "TARGETS", F(atom.targets) },
1979 { "UTF8_STRING", F(atom.utf8_string) },
1980 { "_WL_SELECTION", F(atom.wl_selection) },
1981 { "INCR", F(atom.incr) },
1982 { "TIMESTAMP", F(atom.timestamp) },
1983 { "MULTIPLE", F(atom.multiple) },
1984 { "UTF8_STRING" , F(atom.utf8_string) },
1985 { "COMPOUND_TEXT", F(atom.compound_text) },
1986 { "TEXT", F(atom.text) },
1987 { "STRING", F(atom.string) },
1988 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1989 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001990 { "XdndSelection", F(atom.xdnd_selection) },
1991 { "XdndAware", F(atom.xdnd_aware) },
1992 { "XdndEnter", F(atom.xdnd_enter) },
1993 { "XdndLeave", F(atom.xdnd_leave) },
1994 { "XdndDrop", F(atom.xdnd_drop) },
1995 { "XdndStatus", F(atom.xdnd_status) },
1996 { "XdndFinished", F(atom.xdnd_finished) },
1997 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001998 { "XdndActionCopy", F(atom.xdnd_action_copy) },
1999 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002000 };
2001#undef F
2002
2003 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2004 xcb_xfixes_query_version_reply_t *xfixes_reply;
2005 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2006 xcb_intern_atom_reply_t *reply;
2007 xcb_render_query_pict_formats_reply_t *formats_reply;
2008 xcb_render_query_pict_formats_cookie_t formats_cookie;
2009 xcb_render_pictforminfo_t *formats;
2010 uint32_t i;
2011
2012 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002013 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002014
2015 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2016
2017 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2018 cookies[i] = xcb_intern_atom (wm->conn, 0,
2019 strlen(atoms[i].name),
2020 atoms[i].name);
2021
2022 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2023 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2024 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2025 free(reply);
2026 }
2027
2028 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2029 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002030 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002031
2032 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2033 XCB_XFIXES_MAJOR_VERSION,
2034 XCB_XFIXES_MINOR_VERSION);
2035 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2036 xfixes_cookie, NULL);
2037
Martin Minarik6d118362012-06-07 18:01:59 +02002038 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002039 xfixes_reply->major_version, xfixes_reply->minor_version);
2040
2041 free(xfixes_reply);
2042
2043 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2044 formats_cookie, 0);
2045 if (formats_reply == NULL)
2046 return;
2047
2048 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002049 for (i = 0; i < formats_reply->num_formats; i++) {
2050 if (formats[i].direct.red_mask != 0xff &&
2051 formats[i].direct.red_shift != 16)
2052 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002053 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2054 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002055 wm->format_rgb = formats[i];
2056 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2057 formats[i].depth == 32 &&
2058 formats[i].direct.alpha_mask == 0xff &&
2059 formats[i].direct.alpha_shift == 24)
2060 wm->format_rgba = formats[i];
2061 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002062
2063 free(formats_reply);
2064}
2065
2066static void
2067weston_wm_create_wm_window(struct weston_wm *wm)
2068{
2069 static const char name[] = "Weston WM";
2070
2071 wm->wm_window = xcb_generate_id(wm->conn);
2072 xcb_create_window(wm->conn,
2073 XCB_COPY_FROM_PARENT,
2074 wm->wm_window,
2075 wm->screen->root,
2076 0, 0,
2077 10, 10,
2078 0,
2079 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2080 wm->screen->root_visual,
2081 0, NULL);
2082
2083 xcb_change_property(wm->conn,
2084 XCB_PROP_MODE_REPLACE,
2085 wm->wm_window,
2086 wm->atom.net_supporting_wm_check,
2087 XCB_ATOM_WINDOW,
2088 32, /* format */
2089 1, &wm->wm_window);
2090
2091 xcb_change_property(wm->conn,
2092 XCB_PROP_MODE_REPLACE,
2093 wm->wm_window,
2094 wm->atom.net_wm_name,
2095 wm->atom.utf8_string,
2096 8, /* format */
2097 strlen(name), name);
2098
2099 xcb_change_property(wm->conn,
2100 XCB_PROP_MODE_REPLACE,
2101 wm->screen->root,
2102 wm->atom.net_supporting_wm_check,
2103 XCB_ATOM_WINDOW,
2104 32, /* format */
2105 1, &wm->wm_window);
2106
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002107 /* Claim the WM_S0 selection even though we don't suport
2108 * the --replace functionality. */
2109 xcb_set_selection_owner(wm->conn,
2110 wm->wm_window,
2111 wm->atom.wm_s0,
2112 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002113
2114 xcb_set_selection_owner(wm->conn,
2115 wm->wm_window,
2116 wm->atom.net_wm_cm_s0,
2117 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002118}
2119
2120struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002121weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002122{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002123 struct weston_wm *wm;
2124 struct wl_event_loop *loop;
2125 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002126 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002127 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002128
Peter Huttererf3d62272013-08-08 11:57:05 +10002129 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002130 if (wm == NULL)
2131 return NULL;
2132
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002133 wm->server = wxs;
2134 wm->window_hash = hash_table_create();
2135 if (wm->window_hash == NULL) {
2136 free(wm);
2137 return NULL;
2138 }
2139
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002140 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002141 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002142 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002143 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002144 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002145 hash_table_destroy(wm->window_hash);
2146 free(wm);
2147 return NULL;
2148 }
2149
2150 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2151 wm->screen = s.data;
2152
2153 loop = wl_display_get_event_loop(wxs->wl_display);
2154 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002155 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002156 WL_EVENT_READABLE,
2157 weston_wm_handle_event, wm);
2158 wl_event_source_check(wm->source);
2159
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002160 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002161 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002162
2163 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002164 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2165 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2166 XCB_EVENT_MASK_PROPERTY_CHANGE;
2167 xcb_change_window_attributes(wm->conn, wm->screen->root,
2168 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002169
2170 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2171 XCB_COMPOSITE_REDIRECT_MANUAL);
2172
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002173 wm->theme = theme_create();
2174
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002175 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002176 supported[1] = wm->atom.net_wm_state;
2177 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002178 supported[3] = wm->atom.net_wm_state_maximized_vert;
2179 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002180 xcb_change_property(wm->conn,
2181 XCB_PROP_MODE_REPLACE,
2182 wm->screen->root,
2183 wm->atom.net_supported,
2184 XCB_ATOM_ATOM,
2185 32, /* format */
2186 ARRAY_LENGTH(supported), supported);
2187
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002188 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002189
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002190 weston_wm_dnd_init(wm);
2191
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002192 xcb_flush(wm->conn);
2193
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002194 wm->create_surface_listener.notify = weston_wm_create_surface;
2195 wl_signal_add(&wxs->compositor->create_surface_signal,
2196 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002197 wm->activate_listener.notify = weston_wm_window_activate;
2198 wl_signal_add(&wxs->compositor->activate_signal,
2199 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002200 wm->transform_listener.notify = weston_wm_window_transform;
2201 wl_signal_add(&wxs->compositor->transform_signal,
2202 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002203 wm->kill_listener.notify = weston_wm_kill_client;
2204 wl_signal_add(&wxs->compositor->kill_signal,
2205 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002206 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002207
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002208 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002209 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002210
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002211 /* Create wm window and take WM_S0 selection last, which
2212 * signals to Xwayland that we're done with setup. */
2213 weston_wm_create_wm_window(wm);
2214
2215 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002216
2217 return wm;
2218}
2219
2220void
2221weston_wm_destroy(struct weston_wm *wm)
2222{
2223 /* FIXME: Free windows in hash. */
2224 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002225 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002226 xcb_disconnect(wm->conn);
2227 wl_event_source_remove(wm->source);
2228 wl_list_remove(&wm->selection_listener.link);
2229 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002230 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002231 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002232
2233 free(wm);
2234}
2235
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002236static struct weston_wm_window *
2237get_wm_window(struct weston_surface *surface)
2238{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002239 struct wl_listener *listener;
2240
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002241 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002242 if (listener)
2243 return container_of(listener, struct weston_wm_window,
2244 surface_destroy_listener);
2245
2246 return NULL;
2247}
2248
2249static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002250weston_wm_window_configure(void *data)
2251{
2252 struct weston_wm_window *window = data;
2253 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002254 uint32_t values[4];
2255 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002256
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002257 weston_wm_window_get_child_position(window, &x, &y);
2258 values[0] = x;
2259 values[1] = y;
2260 values[2] = window->width;
2261 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002262 xcb_configure_window(wm->conn,
2263 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002264 XCB_CONFIG_WINDOW_X |
2265 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002266 XCB_CONFIG_WINDOW_WIDTH |
2267 XCB_CONFIG_WINDOW_HEIGHT,
2268 values);
2269
2270 weston_wm_window_get_frame_size(window, &width, &height);
2271 values[0] = width;
2272 values[1] = height;
2273 xcb_configure_window(wm->conn,
2274 window->frame_id,
2275 XCB_CONFIG_WINDOW_WIDTH |
2276 XCB_CONFIG_WINDOW_HEIGHT,
2277 values);
2278
2279 window->configure_source = NULL;
2280
2281 weston_wm_window_schedule_repaint(window);
2282}
2283
2284static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002285send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002286{
2287 struct weston_wm_window *window = get_wm_window(surface);
2288 struct weston_wm *wm = window->wm;
2289 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002290 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002291
Marek Chalupae9fe4672014-10-29 13:44:44 +01002292 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002293 hborder = 2 * t->width;
2294 vborder = t->titlebar_height + t->width;
2295 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002296 hborder = 0;
2297 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002298 }
2299
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002300 if (width > hborder)
2301 window->width = width - hborder;
2302 else
2303 window->width = 1;
2304
2305 if (height > vborder)
2306 window->height = height - vborder;
2307 else
2308 window->height = 1;
2309
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002310 if (window->frame)
2311 frame_resize_inside(window->frame, window->width, window->height);
2312
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002313 if (window->configure_source)
2314 return;
2315
2316 window->configure_source =
2317 wl_event_loop_add_idle(wm->server->loop,
2318 weston_wm_window_configure, window);
2319}
2320
2321static const struct weston_shell_client shell_client = {
2322 send_configure
2323};
2324
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002325static int
2326legacy_fullscreen(struct weston_wm *wm,
2327 struct weston_wm_window *window,
2328 struct weston_output **output_ret)
2329{
2330 struct weston_compositor *compositor = wm->server->compositor;
2331 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002332 uint32_t minmax = PMinSize | PMaxSize;
2333 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002334
2335 /* Heuristics for detecting legacy fullscreen windows... */
2336
2337 wl_list_for_each(output, &compositor->output_list, link) {
2338 if (output->x == window->x &&
2339 output->y == window->y &&
2340 output->width == window->width &&
2341 output->height == window->height &&
2342 window->override_redirect) {
2343 *output_ret = output;
2344 return 1;
2345 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002346
2347 matching_size = 0;
2348 if ((window->size_hints.flags & (USSize |PSize)) &&
2349 window->size_hints.width == output->width &&
2350 window->size_hints.height == output->height)
2351 matching_size = 1;
2352 if ((window->size_hints.flags & minmax) == minmax &&
2353 window->size_hints.min_width == output->width &&
2354 window->size_hints.min_height == output->height &&
2355 window->size_hints.max_width == output->width &&
2356 window->size_hints.max_height == output->height)
2357 matching_size = 1;
2358
2359 if (matching_size && !window->decorate &&
2360 (window->size_hints.flags & (USPosition | PPosition)) &&
2361 window->size_hints.x == output->x &&
2362 window->size_hints.y == output->y) {
2363 *output_ret = output;
2364 return 1;
2365 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002366 }
2367
2368 return 0;
2369}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002370
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002371static bool
2372weston_wm_window_type_inactive(struct weston_wm_window *window)
2373{
2374 struct weston_wm *wm = window->wm;
2375
2376 return window->type == wm->atom.net_wm_window_type_tooltip ||
2377 window->type == wm->atom.net_wm_window_type_dropdown ||
2378 window->type == wm->atom.net_wm_window_type_dnd ||
2379 window->type == wm->atom.net_wm_window_type_combo ||
2380 window->type == wm->atom.net_wm_window_type_popup;
2381}
2382
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002383static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002384xserver_map_shell_surface(struct weston_wm_window *window,
2385 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002386{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002387 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002388 struct weston_shell_interface *shell_interface =
2389 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002390 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002391 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002392 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002393
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002394 weston_wm_window_read_properties(window);
2395
2396 /* A weston_wm_window may have many different surfaces assigned
2397 * throughout its life, so we must make sure to remove the listener
2398 * from the old surface signal list. */
2399 if (window->surface)
2400 wl_list_remove(&window->surface_destroy_listener.link);
2401
2402 window->surface = surface;
2403 window->surface_destroy_listener.notify = surface_destroy;
2404 wl_signal_add(&window->surface->destroy_signal,
2405 &window->surface_destroy_listener);
2406
2407 weston_wm_window_schedule_repaint(window);
2408
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002409 if (!shell_interface->create_shell_surface)
2410 return;
2411
Jason Ekstranda7af7042013-10-12 22:38:11 -05002412 if (!shell_interface->get_primary_view)
2413 return;
2414
Pekka Paalanen50b67472014-10-01 15:02:41 +03002415 if (window->surface->configure) {
2416 weston_log("warning, unexpected in %s: "
2417 "surface's configure hook is already set.\n",
2418 __func__);
2419 return;
2420 }
2421
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002422 window->shsurf =
2423 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002424 window->surface,
2425 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002426 window->view = shell_interface->get_primary_view(shell_interface->shell,
2427 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002428
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002429 if (window->name)
2430 shell_interface->set_title(window->shsurf, window->name);
2431
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002432 if (window->fullscreen) {
2433 window->saved_width = window->width;
2434 window->saved_height = window->height;
2435 shell_interface->set_fullscreen(window->shsurf,
2436 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2437 0, NULL);
2438 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002439 } else if (legacy_fullscreen(wm, window, &output)) {
2440 window->fullscreen = 1;
2441 shell_interface->set_fullscreen(window->shsurf,
2442 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2443 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002444 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002445 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002446 window->x,
2447 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002448 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002449 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002450 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002451 if (weston_wm_window_type_inactive(window))
2452 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002453 shell_interface->set_transient(window->shsurf,
2454 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002455 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002456 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002457 } else if (weston_wm_window_is_maximized(window)) {
2458 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002459 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002460 if (weston_wm_window_type_inactive(window)) {
2461 shell_interface->set_xwayland(window->shsurf,
2462 window->x,
2463 window->y,
2464 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2465 } else {
2466 shell_interface->set_toplevel(window->shsurf);
2467 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002468 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002469}