blob: 145d940e3db886af090542d7fad67f41e3ac7057 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040034#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050035#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040036
37#include "xwayland.h"
38
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080039#include "cairo-util.h"
40#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040041#include "hash.h"
42
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070043struct wm_size_hints {
44 uint32_t flags;
45 int32_t x, y;
46 int32_t width, height; /* should set so old wm's don't mess up */
47 int32_t min_width, min_height;
48 int32_t max_width, max_height;
49 int32_t width_inc, height_inc;
50 struct {
51 int32_t x;
52 int32_t y;
53 } min_aspect, max_aspect;
54 int32_t base_width, base_height;
55 int32_t win_gravity;
56};
57
58#define USPosition (1L << 0)
59#define USSize (1L << 1)
60#define PPosition (1L << 2)
61#define PSize (1L << 3)
62#define PMinSize (1L << 4)
63#define PMaxSize (1L << 5)
64#define PResizeInc (1L << 6)
65#define PAspect (1L << 7)
66#define PBaseSize (1L << 8)
67#define PWinGravity (1L << 9)
68
Kristian Høgsberg380deee2012-05-21 17:12:41 -040069struct motif_wm_hints {
70 uint32_t flags;
71 uint32_t functions;
72 uint32_t decorations;
73 int32_t input_mode;
74 uint32_t status;
75};
76
77#define MWM_HINTS_FUNCTIONS (1L << 0)
78#define MWM_HINTS_DECORATIONS (1L << 1)
79#define MWM_HINTS_INPUT_MODE (1L << 2)
80#define MWM_HINTS_STATUS (1L << 3)
81
82#define MWM_FUNC_ALL (1L << 0)
83#define MWM_FUNC_RESIZE (1L << 1)
84#define MWM_FUNC_MOVE (1L << 2)
85#define MWM_FUNC_MINIMIZE (1L << 3)
86#define MWM_FUNC_MAXIMIZE (1L << 4)
87#define MWM_FUNC_CLOSE (1L << 5)
88
89#define MWM_DECOR_ALL (1L << 0)
90#define MWM_DECOR_BORDER (1L << 1)
91#define MWM_DECOR_RESIZEH (1L << 2)
92#define MWM_DECOR_TITLE (1L << 3)
93#define MWM_DECOR_MENU (1L << 4)
94#define MWM_DECOR_MINIMIZE (1L << 5)
95#define MWM_DECOR_MAXIMIZE (1L << 6)
96
97#define MWM_INPUT_MODELESS 0
98#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
99#define MWM_INPUT_SYSTEM_MODAL 2
100#define MWM_INPUT_FULL_APPLICATION_MODAL 3
101#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
102
103#define MWM_TEAROFF_WINDOW (1L<<0)
104
105#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
106#define _NET_WM_MOVERESIZE_SIZE_TOP 1
107#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
108#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
109#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
110#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
111#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
112#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
113#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
114#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
115#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
116#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
117
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400118struct weston_wm_window {
119 struct weston_wm *wm;
120 xcb_window_t id;
121 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500122 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400123 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700124 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400125 struct weston_surface *surface;
126 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500127 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400128 struct wl_listener surface_destroy_listener;
129 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400130 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300132 int pid;
133 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400134 char *class;
135 char *name;
136 struct weston_wm_window *transient_for;
137 uint32_t protocols;
138 xcb_atom_t type;
139 int width, height;
140 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500141 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300143 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500144 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500145 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700146 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200147 int maximized_vert;
148 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700149 struct wm_size_hints size_hints;
150 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700151 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400152};
153
154static struct weston_wm_window *
155get_wm_window(struct weston_surface *surface);
156
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400157static void
158weston_wm_window_schedule_repaint(struct weston_wm_window *window);
159
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700160static void
161xserver_map_shell_surface(struct weston_wm_window *window,
162 struct weston_surface *surface);
163
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400164static int __attribute__ ((format (printf, 1, 2)))
165wm_log(const char *fmt, ...)
166{
167#ifdef WM_DEBUG
168 int l;
169 va_list argp;
170
171 va_start(argp, fmt);
172 l = weston_vlog(fmt, argp);
173 va_end(argp);
174
175 return l;
176#else
177 return 0;
178#endif
179}
180
181static int __attribute__ ((format (printf, 1, 2)))
182wm_log_continue(const char *fmt, ...)
183{
184#ifdef WM_DEBUG
185 int l;
186 va_list argp;
187
188 va_start(argp, fmt);
189 l = weston_vlog_continue(fmt, argp);
190 va_end(argp);
191
192 return l;
193#else
194 return 0;
195#endif
196}
197
198
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400199const char *
200get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
201{
202 xcb_get_atom_name_cookie_t cookie;
203 xcb_get_atom_name_reply_t *reply;
204 xcb_generic_error_t *e;
205 static char buffer[64];
206
207 if (atom == XCB_ATOM_NONE)
208 return "None";
209
210 cookie = xcb_get_atom_name (c, atom);
211 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500212
213 if(reply) {
214 snprintf(buffer, sizeof buffer, "%.*s",
215 xcb_get_atom_name_name_length (reply),
216 xcb_get_atom_name_name (reply));
217 } else {
218 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
219 }
220
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400221 free(reply);
222
223 return buffer;
224}
225
Tiago Vignatti90fada42012-07-16 12:02:08 -0400226static xcb_cursor_t
227xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
228{
229 xcb_connection_t *c = wm->conn;
230 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
231 xcb_screen_t *screen = s.data;
232 xcb_gcontext_t gc;
233 xcb_pixmap_t pix;
234 xcb_render_picture_t pic;
235 xcb_cursor_t cursor;
236 int stride = img->width * 4;
237
238 pix = xcb_generate_id(c);
239 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
240
241 pic = xcb_generate_id(c);
242 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
243
244 gc = xcb_generate_id(c);
245 xcb_create_gc(c, gc, pix, 0, 0);
246
247 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
248 img->width, img->height, 0, 0, 0, 32,
249 stride * img->height, (uint8_t *) img->pixels);
250 xcb_free_gc(c, gc);
251
252 cursor = xcb_generate_id(c);
253 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
254
255 xcb_render_free_picture(c, pic);
256 xcb_free_pixmap(c, pix);
257
258 return cursor;
259}
260
261static xcb_cursor_t
262xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
263{
264 /* TODO: treat animated cursors as well */
265 if (images->nimage != 1)
266 return -1;
267
268 return xcb_cursor_image_load_cursor(wm, images->images[0]);
269}
270
271static xcb_cursor_t
272xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
273{
274 xcb_cursor_t cursor;
275 XcursorImages *images;
276 char *v = NULL;
277 int size = 0;
278
279 if (!file)
280 return 0;
281
282 v = getenv ("XCURSOR_SIZE");
283 if (v)
284 size = atoi(v);
285
286 if (!size)
287 size = 32;
288
289 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300290 if (!images)
291 return -1;
292
Tiago Vignatti90fada42012-07-16 12:02:08 -0400293 cursor = xcb_cursor_images_load_cursor (wm, images);
294 XcursorImagesDestroy (images);
295
296 return cursor;
297}
298
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400299void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400300dump_property(struct weston_wm *wm,
301 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400302{
303 int32_t *incr_value;
304 const char *text_value, *name;
305 xcb_atom_t *atom_value;
306 int width, len;
307 uint32_t i;
308
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400309 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400310 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400311 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400312 return;
313 }
314
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400315 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
316 get_atom_name(wm->conn, reply->type),
317 reply->format,
318 xcb_get_property_value_length(reply),
319 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400320
321 if (reply->type == wm->atom.incr) {
322 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400323 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400324 } else if (reply->type == wm->atom.utf8_string ||
325 reply->type == wm->atom.string) {
326 text_value = xcb_get_property_value(reply);
327 if (reply->value_len > 40)
328 len = 40;
329 else
330 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400331 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400332 } else if (reply->type == XCB_ATOM_ATOM) {
333 atom_value = xcb_get_property_value(reply);
334 for (i = 0; i < reply->value_len; i++) {
335 name = get_atom_name(wm->conn, atom_value[i]);
336 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400337 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400338 width = 4;
339 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400340 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400341 }
342
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400343 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400344 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400345 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400347 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400348 }
349}
350
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200351static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400352read_and_dump_property(struct weston_wm *wm,
353 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400355 xcb_get_property_reply_t *reply;
356 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400358 cookie = xcb_get_property(wm->conn, 0, window,
359 property, XCB_ATOM_ANY, 0, 2048);
360 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400362 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400364 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400365}
366
367/* We reuse some predefined, but otherwise useles atoms */
368#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
369#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500370#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700371#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400372
373static void
374weston_wm_window_read_properties(struct weston_wm_window *window)
375{
376 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200377 struct weston_shell_interface *shell_interface =
378 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400379
380#define F(field) offsetof(struct weston_wm_window, field)
381 const struct {
382 xcb_atom_t atom;
383 xcb_atom_t type;
384 int offset;
385 } props[] = {
386 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
387 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
388 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
389 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700390 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500391 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400392 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
393 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300394 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300396 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400397 };
398#undef F
399
400 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
401 xcb_get_property_reply_t *reply;
402 void *p;
403 uint32_t *xid;
404 xcb_atom_t *atom;
405 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200406 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400407
408 if (!window->properties_dirty)
409 return;
410 window->properties_dirty = 0;
411
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400412 for (i = 0; i < ARRAY_LENGTH(props); i++)
413 cookie[i] = xcb_get_property(wm->conn,
414 0, /* delete */
415 window->id,
416 props[i].atom,
417 XCB_ATOM_ANY, 0, 2048);
418
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300419 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700420 window->size_hints.flags = 0;
421 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700422 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700423
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400424 for (i = 0; i < ARRAY_LENGTH(props); i++) {
425 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
426 if (!reply)
427 /* Bad window, typically */
428 continue;
429 if (reply->type == XCB_ATOM_NONE) {
430 /* No such property */
431 free(reply);
432 continue;
433 }
434
435 p = ((char *) window + props[i].offset);
436
437 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300438 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400439 case XCB_ATOM_STRING:
440 /* FIXME: We're using this for both string and
441 utf8_string */
442 if (*(char **) p)
443 free(*(char **) p);
444
445 *(char **) p =
446 strndup(xcb_get_property_value(reply),
447 xcb_get_property_value_length(reply));
448 break;
449 case XCB_ATOM_WINDOW:
450 xid = xcb_get_property_value(reply);
451 *(struct weston_wm_window **) p =
452 hash_table_lookup(wm->window_hash, *xid);
453 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300454 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400455 case XCB_ATOM_ATOM:
456 atom = xcb_get_property_value(reply);
457 *(xcb_atom_t *) p = *atom;
458 break;
459 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700460 atom = xcb_get_property_value(reply);
461 for (i = 0; i < reply->value_len; i++)
462 if (atom[i] == wm->atom.wm_delete_window)
463 window->delete_window = 1;
464 break;
465
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400466 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700467 case TYPE_WM_NORMAL_HINTS:
468 memcpy(&window->size_hints,
469 xcb_get_property_value(reply),
470 sizeof window->size_hints);
471 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500472 case TYPE_NET_WM_STATE:
473 window->fullscreen = 0;
474 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900475 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500476 if (atom[i] == wm->atom.net_wm_state_fullscreen)
477 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200478 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
479 window->maximized_vert = 1;
480 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
481 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900482 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500483 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400484 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700485 memcpy(&window->motif_hints,
486 xcb_get_property_value(reply),
487 sizeof window->motif_hints);
488 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
489 window->decorate =
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200490 window->motif_hints.decorations;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400491 break;
492 default:
493 break;
494 }
495 free(reply);
496 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200497
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200498 if (window->pid > 0) {
499 gethostname(name, sizeof(name));
500 for (i = 0; i < sizeof(name); i++) {
501 if (name[i] == '\0')
502 break;
503 }
504 if (i == sizeof(name))
505 name[0] = '\0'; /* ignore stupid hostnames */
506
507 /* this is only one heuristic to guess the PID of a client is
508 * valid, assuming it's compliant with icccm and ewmh.
509 * Non-compliants and remote applications of course fail. */
510 if (!window->machine || strcmp(window->machine, name))
511 window->pid = 0;
512 }
513
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200514 if (window->shsurf && window->name)
515 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500516 if (window->frame && window->name)
517 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200518 if (window->shsurf && window->pid > 0)
519 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400520}
521
522static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400523weston_wm_window_get_frame_size(struct weston_wm_window *window,
524 int *width, int *height)
525{
526 struct theme *t = window->wm->theme;
527
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500528 if (window->fullscreen) {
529 *width = window->width;
530 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800531 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500532 *width = frame_width(window->frame);
533 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400534 } else {
535 *width = window->width + t->margin * 2;
536 *height = window->height + t->margin * 2;
537 }
538}
539
540static void
541weston_wm_window_get_child_position(struct weston_wm_window *window,
542 int *x, int *y)
543{
544 struct theme *t = window->wm->theme;
545
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500546 if (window->fullscreen) {
547 *x = 0;
548 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800549 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500550 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400551 } else {
552 *x = t->margin;
553 *y = t->margin;
554 }
555}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400556
557static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500558weston_wm_window_send_configure_notify(struct weston_wm_window *window)
559{
560 xcb_configure_notify_event_t configure_notify;
561 struct weston_wm *wm = window->wm;
562 int x, y;
563
564 weston_wm_window_get_child_position(window, &x, &y);
565 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
566 configure_notify.pad0 = 0;
567 configure_notify.event = window->id;
568 configure_notify.window = window->id;
569 configure_notify.above_sibling = XCB_WINDOW_NONE;
570 configure_notify.x = x;
571 configure_notify.y = y;
572 configure_notify.width = window->width;
573 configure_notify.height = window->height;
574 configure_notify.border_width = 0;
575 configure_notify.override_redirect = 0;
576 configure_notify.pad1 = 0;
577
578 xcb_send_event(wm->conn, 0, window->id,
579 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
580 (char *) &configure_notify);
581}
582
583static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400584weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
585{
586 xcb_configure_request_event_t *configure_request =
587 (xcb_configure_request_event_t *) event;
588 struct weston_wm_window *window;
589 uint32_t mask, values[16];
590 int x, y, width, height, i = 0;
591
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400592 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
593 configure_request->window,
594 configure_request->x, configure_request->y,
595 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400596
597 window = hash_table_lookup(wm->window_hash, configure_request->window);
598
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500599 if (window->fullscreen) {
600 weston_wm_window_send_configure_notify(window);
601 return;
602 }
603
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400604 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
605 window->width = configure_request->width;
606 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
607 window->height = configure_request->height;
608
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500609 if (window->frame)
610 frame_resize_inside(window->frame, window->width, window->height);
611
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400612 weston_wm_window_get_child_position(window, &x, &y);
613 values[i++] = x;
614 values[i++] = y;
615 values[i++] = window->width;
616 values[i++] = window->height;
617 values[i++] = 0;
618 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
619 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
620 XCB_CONFIG_WINDOW_BORDER_WIDTH;
621 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
622 values[i++] = configure_request->sibling;
623 mask |= XCB_CONFIG_WINDOW_SIBLING;
624 }
625 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
626 values[i++] = configure_request->stack_mode;
627 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
628 }
629
630 xcb_configure_window(wm->conn, window->id, mask, values);
631
632 weston_wm_window_get_frame_size(window, &width, &height);
633 values[0] = width;
634 values[1] = height;
635 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
636 xcb_configure_window(wm->conn, window->frame_id, mask, values);
637
638 weston_wm_window_schedule_repaint(window);
639}
640
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400641static int
642our_resource(struct weston_wm *wm, uint32_t id)
643{
644 const xcb_setup_t *setup;
645
646 setup = xcb_get_setup(wm->conn);
647
648 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
649}
650
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400651static void
652weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
653{
654 xcb_configure_notify_event_t *configure_notify =
655 (xcb_configure_notify_event_t *) event;
656 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400657
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400658 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400659 configure_notify->window,
660 configure_notify->x, configure_notify->y,
661 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300662
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400663 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700664 window->x = configure_notify->x;
665 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700666 if (window->override_redirect) {
667 window->width = configure_notify->width;
668 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500669 if (window->frame)
670 frame_resize_inside(window->frame,
671 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700672 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400673}
674
675static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300676weston_wm_kill_client(struct wl_listener *listener, void *data)
677{
678 struct weston_surface *surface = data;
679 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300680 if (!window)
681 return;
682
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200683 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300684 kill(window->pid, SIGKILL);
685}
686
687static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700688weston_wm_create_surface(struct wl_listener *listener, void *data)
689{
690 struct weston_surface *surface = data;
691 struct weston_wm *wm =
692 container_of(listener,
693 struct weston_wm, create_surface_listener);
694 struct weston_wm_window *window;
695
696 if (wl_resource_get_client(surface->resource) != wm->server->client)
697 return;
698
699 wl_list_for_each(window, &wm->unpaired_window_list, link)
700 if (window->surface_id ==
701 wl_resource_get_id(surface->resource)) {
702 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700703 window->surface_id = 0;
704 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700705 break;
706 }
707}
708
709static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400710weston_wm_window_activate(struct wl_listener *listener, void *data)
711{
712 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200713 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600714 struct weston_wm *wm =
715 container_of(listener, struct weston_wm, activate_listener);
716 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400717
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200718 if (surface) {
719 window = get_wm_window(surface);
720 }
721
Scott Moreau85ecac02012-05-21 15:49:14 -0600722 if (window) {
Boyan Dingb9f863c2014-08-30 10:33:23 +0800723 if (window->override_redirect)
724 return;
725
Scott Moreau85ecac02012-05-21 15:49:14 -0600726 client_message.response_type = XCB_CLIENT_MESSAGE;
727 client_message.format = 32;
728 client_message.window = window->id;
729 client_message.type = wm->atom.wm_protocols;
730 client_message.data.data32[0] = wm->atom.wm_take_focus;
731 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
732
733 xcb_send_event(wm->conn, 0, window->id,
734 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
735 (char *) &client_message);
736
737 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
738 window->id, XCB_TIME_CURRENT_TIME);
739 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400740 xcb_set_input_focus (wm->conn,
741 XCB_INPUT_FOCUS_POINTER_ROOT,
742 XCB_NONE,
743 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600744 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400745
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500746 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800747 if (wm->focus_window->frame)
748 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400749 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500750 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400751 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500752 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800753 if (wm->focus_window->frame)
754 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400755 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500756 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400757}
758
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300759static void
760weston_wm_window_transform(struct wl_listener *listener, void *data)
761{
762 struct weston_surface *surface = data;
763 struct weston_wm_window *window = get_wm_window(surface);
764 struct weston_wm *wm =
765 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300766 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300767
768 if (!window || !wm)
769 return;
770
Jason Ekstranda7af7042013-10-12 22:38:11 -0500771 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300772 return;
773
Jason Ekstranda7af7042013-10-12 22:38:11 -0500774 if (window->x != window->view->geometry.x ||
775 window->y != window->view->geometry.y) {
776 values[0] = window->view->geometry.x;
777 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700778 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300779
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700780 xcb_configure_window(wm->conn, window->frame_id, mask, values);
781 xcb_flush(wm->conn);
782 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300783}
784
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400785#define ICCCM_WITHDRAWN_STATE 0
786#define ICCCM_NORMAL_STATE 1
787#define ICCCM_ICONIC_STATE 3
788
789static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500790weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400791{
792 struct weston_wm *wm = window->wm;
793 uint32_t property[2];
794
795 property[0] = state;
796 property[1] = XCB_WINDOW_NONE;
797
798 xcb_change_property(wm->conn,
799 XCB_PROP_MODE_REPLACE,
800 window->id,
801 wm->atom.wm_state,
802 wm->atom.wm_state,
803 32, /* format */
804 2, property);
805}
806
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400807static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500808weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
809{
810 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200811 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500812 int i;
813
814 i = 0;
815 if (window->fullscreen)
816 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200817 if (window->maximized_vert)
818 property[i++] = wm->atom.net_wm_state_maximized_vert;
819 if (window->maximized_horz)
820 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500821
822 xcb_change_property(wm->conn,
823 XCB_PROP_MODE_REPLACE,
824 window->id,
825 wm->atom.net_wm_state,
826 XCB_ATOM_ATOM,
827 32, /* format */
828 i, property);
829}
830
831static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700832weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400833{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700834 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400835 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400836 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200837 int buttons = FRAME_BUTTON_CLOSE;
838
839 if (window->decorate & MWM_DECOR_MAXIMIZE)
840 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400841
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500842 window->frame = frame_create(window->wm->theme,
843 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200844 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500845 frame_resize_inside(window->frame, window->width, window->height);
846
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400847 weston_wm_window_get_frame_size(window, &width, &height);
848 weston_wm_window_get_child_position(window, &x, &y);
849
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400850 values[0] = wm->screen->black_pixel;
851 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400852 XCB_EVENT_MASK_KEY_PRESS |
853 XCB_EVENT_MASK_KEY_RELEASE |
854 XCB_EVENT_MASK_BUTTON_PRESS |
855 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400856 XCB_EVENT_MASK_POINTER_MOTION |
857 XCB_EVENT_MASK_ENTER_WINDOW |
858 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400859 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400860 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400861 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400862
863 window->frame_id = xcb_generate_id(wm->conn);
864 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400865 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400866 window->frame_id,
867 wm->screen->root,
868 0, 0,
869 width, height,
870 0,
871 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400872 wm->visual_id,
873 XCB_CW_BORDER_PIXEL |
874 XCB_CW_EVENT_MASK |
875 XCB_CW_COLORMAP, values);
876
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400877 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
878
879 values[0] = 0;
880 xcb_configure_window(wm->conn, window->id,
881 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
882
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400883 window->cairo_surface =
884 cairo_xcb_surface_create_with_xrender_format(wm->conn,
885 wm->screen,
886 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400887 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400888 width, height);
889
890 hash_table_insert(wm->window_hash, window->frame_id, window);
891}
892
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200893/*
894 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
895 * Passing a <0 desktop value deletes the property.
896 */
897static void
898weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
899 int desktop)
900{
901 if (desktop >= 0) {
902 xcb_change_property(window->wm->conn,
903 XCB_PROP_MODE_REPLACE,
904 window->id,
905 window->wm->atom.net_wm_desktop,
906 XCB_ATOM_CARDINAL,
907 32, /* format */
908 1, &desktop);
909 } else {
910 xcb_delete_property(window->wm->conn,
911 window->id,
912 window->wm->atom.net_wm_desktop);
913 }
914}
915
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400916static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700917weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
918{
919 xcb_map_request_event_t *map_request =
920 (xcb_map_request_event_t *) event;
921 struct weston_wm_window *window;
922
923 if (our_resource(wm, map_request->window)) {
924 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
925 map_request->window);
926 return;
927 }
928
929 window = hash_table_lookup(wm->window_hash, map_request->window);
930
931 weston_wm_window_read_properties(window);
932
933 if (window->frame_id == XCB_WINDOW_NONE)
934 weston_wm_window_create_frame(window);
935
936 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
937 window->id, window, window->frame_id);
938
939 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
940 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200941 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700942
943 xcb_map_window(wm->conn, map_request->window);
944 xcb_map_window(wm->conn, window->frame_id);
945}
946
947static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
949{
950 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
951
952 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400953 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
954 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400955 return;
956 }
957
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400958 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400959}
960
961static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400962weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
963{
964 xcb_unmap_notify_event_t *unmap_notify =
965 (xcb_unmap_notify_event_t *) event;
966 struct weston_wm_window *window;
967
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400968 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
969 unmap_notify->window,
970 unmap_notify->event,
971 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400972
973 if (our_resource(wm, unmap_notify->window))
974 return;
975
MoD31700122013-06-11 19:58:55 -0500976 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400977 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
978 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400979 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400980
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400981 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400982 if (wm->focus_window == window)
983 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400984 if (window->surface)
985 wl_list_remove(&window->surface_destroy_listener.link);
986 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200987 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -0800988 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200989
990 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
991 weston_wm_window_set_virtual_desktop(window, -1);
992
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700993 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400994}
995
996static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400997weston_wm_window_draw_decoration(void *data)
998{
999 struct weston_wm_window *window = data;
1000 struct weston_wm *wm = window->wm;
1001 struct theme *t = wm->theme;
1002 cairo_t *cr;
1003 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001004 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001005 struct weston_shell_interface *shell_interface =
1006 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001007 uint32_t flags = 0;
1008
1009 weston_wm_window_read_properties(window);
1010
1011 window->repaint_source = NULL;
1012
1013 weston_wm_window_get_frame_size(window, &width, &height);
1014 weston_wm_window_get_child_position(window, &x, &y);
1015
1016 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1017 cr = cairo_create(window->cairo_surface);
1018
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001019 if (window->fullscreen) {
1020 /* nothing */
1021 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001022 if (wm->focus_window == window)
1023 flags |= THEME_FRAME_ACTIVE;
1024
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001025 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001026 } else {
1027 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1028 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1029 cairo_paint(cr);
1030
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001031 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001032 }
1033
1034 cairo_destroy(cr);
1035
1036 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001037 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001038 if(window->has_alpha) {
1039 pixman_region32_init(&window->surface->pending.opaque);
1040 } else {
1041 /* We leave an extra pixel around the X window area to
1042 * make sure we don't sample from the undefined alpha
1043 * channel when filtering. */
1044 pixman_region32_init_rect(&window->surface->pending.opaque,
1045 x - 1, y - 1,
1046 window->width + 2,
1047 window->height + 2);
1048 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 if (window->view)
1050 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001051
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001052 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001053
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001054 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001055 frame_input_rect(window->frame, &input_x, &input_y,
1056 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001057 } else {
1058 input_x = x;
1059 input_y = y;
1060 input_w = width;
1061 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001062 }
1063
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001064 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001065 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001066
1067 shell_interface->set_window_geometry(window->shsurf,
1068 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001069 }
1070}
1071
1072static void
1073weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1074{
1075 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001076 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001077
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001078 if (window->frame_id == XCB_WINDOW_NONE) {
1079 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001080 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001081 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001082 if(window->has_alpha) {
1083 pixman_region32_init(&window->surface->pending.opaque);
1084 } else {
1085 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1086 width, height);
1087 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001088 if (window->view)
1089 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001090 }
1091 return;
1092 }
1093
1094 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001095 return;
1096
1097 window->repaint_source =
1098 wl_event_loop_add_idle(wm->server->loop,
1099 weston_wm_window_draw_decoration,
1100 window);
1101}
1102
1103static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001104weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1105{
1106 xcb_property_notify_event_t *property_notify =
1107 (xcb_property_notify_event_t *) event;
1108 struct weston_wm_window *window;
1109
1110 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001111 if (!window)
1112 return;
1113
1114 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001115
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001116 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001117 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001118 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001119 else
1120 read_and_dump_property(wm, property_notify->window,
1121 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001122
1123 if (property_notify->atom == wm->atom.net_wm_name ||
1124 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001125 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001126}
1127
1128static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001129weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001130 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001131{
1132 struct weston_wm_window *window;
1133 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001134 xcb_get_geometry_cookie_t geometry_cookie;
1135 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001136
Peter Huttererf3d62272013-08-08 11:57:05 +10001137 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001138 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001139 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001140 return;
1141 }
1142
MoD384a11a2013-06-22 11:04:21 -05001143 geometry_cookie = xcb_get_geometry(wm->conn, id);
1144
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001145 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1146 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1147
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001148 window->wm = wm;
1149 window->id = id;
1150 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001151 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001152 window->width = width;
1153 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001154 window->x = x;
1155 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001156
MoD384a11a2013-06-22 11:04:21 -05001157 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1158 /* technically we should use XRender and check the visual format's
1159 alpha_mask, but checking depth is simpler and works in all known cases */
1160 if(geometry_reply != NULL)
1161 window->has_alpha = geometry_reply->depth == 32;
1162 free(geometry_reply);
1163
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001164 hash_table_insert(wm->window_hash, id, window);
1165}
1166
1167static void
1168weston_wm_window_destroy(struct weston_wm_window *window)
1169{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001170 struct weston_wm *wm = window->wm;
1171
1172 if (window->repaint_source)
1173 wl_event_source_remove(window->repaint_source);
1174 if (window->cairo_surface)
1175 cairo_surface_destroy(window->cairo_surface);
1176
1177 if (window->frame_id) {
1178 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1179 xcb_destroy_window(wm->conn, window->frame_id);
1180 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001181 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001182 hash_table_remove(wm->window_hash, window->frame_id);
1183 window->frame_id = XCB_WINDOW_NONE;
1184 }
1185
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001186 if (window->surface_id)
1187 wl_list_remove(&window->link);
1188
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001189 hash_table_remove(window->wm->window_hash, window->id);
1190 free(window);
1191}
1192
1193static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001194weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1195{
1196 xcb_create_notify_event_t *create_notify =
1197 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001198
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001199 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1200 create_notify->window,
1201 create_notify->width, create_notify->height,
1202 create_notify->override_redirect ? ", override" : "",
1203 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001204
1205 if (our_resource(wm, create_notify->window))
1206 return;
1207
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001208 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001209 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001210 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001211 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001212}
1213
1214static void
1215weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1216{
1217 xcb_destroy_notify_event_t *destroy_notify =
1218 (xcb_destroy_notify_event_t *) event;
1219 struct weston_wm_window *window;
1220
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001221 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1222 destroy_notify->window,
1223 destroy_notify->event,
1224 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001225
1226 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001227 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001228
1229 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001230 weston_wm_window_destroy(window);
1231}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001232
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001233static void
1234weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1235{
1236 xcb_reparent_notify_event_t *reparent_notify =
1237 (xcb_reparent_notify_event_t *) event;
1238 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001239
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001240 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1241 reparent_notify->window,
1242 reparent_notify->parent,
1243 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001244
1245 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001246 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001247 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001248 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001249 } else if (!our_resource(wm, reparent_notify->parent)) {
1250 window = hash_table_lookup(wm->window_hash,
1251 reparent_notify->window);
1252 weston_wm_window_destroy(window);
1253 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001254}
1255
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001256struct weston_seat *
1257weston_wm_pick_seat(struct weston_wm *wm)
1258{
1259 return container_of(wm->server->compositor->seat_list.next,
1260 struct weston_seat, link);
1261}
1262
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001263static struct weston_seat *
1264weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1265{
1266 struct weston_wm *wm = window->wm;
1267 struct weston_seat *seat, *s;
1268
1269 seat = NULL;
1270 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001271 if (s->pointer != NULL && s->pointer->focus &&
1272 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001273 s->pointer->button_count > 0 &&
1274 (seat == NULL ||
1275 s->pointer->grab_serial -
1276 seat->pointer->grab_serial < (1 << 30)))
1277 seat = s;
1278 }
1279
1280 return seat;
1281}
1282
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001283static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001284weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1285 xcb_client_message_event_t *client_message)
1286{
1287 static const int map[] = {
1288 THEME_LOCATION_RESIZING_TOP_LEFT,
1289 THEME_LOCATION_RESIZING_TOP,
1290 THEME_LOCATION_RESIZING_TOP_RIGHT,
1291 THEME_LOCATION_RESIZING_RIGHT,
1292 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1293 THEME_LOCATION_RESIZING_BOTTOM,
1294 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1295 THEME_LOCATION_RESIZING_LEFT
1296 };
1297
1298 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001299 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001300 int detail;
1301 struct weston_shell_interface *shell_interface =
1302 &wm->server->compositor->shell_interface;
1303
Boyan Dingc06a1802014-07-06 11:44:58 +08001304 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001305 || !seat->pointer->focus
1306 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001307 return;
1308
1309 detail = client_message->data.data32[2];
1310 switch (detail) {
1311 case _NET_WM_MOVERESIZE_MOVE:
1312 shell_interface->move(window->shsurf, seat);
1313 break;
1314 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1315 case _NET_WM_MOVERESIZE_SIZE_TOP:
1316 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1317 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1318 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1319 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1320 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1321 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1322 shell_interface->resize(window->shsurf, seat, map[detail]);
1323 break;
1324 case _NET_WM_MOVERESIZE_CANCEL:
1325 break;
1326 }
1327}
1328
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001329#define _NET_WM_STATE_REMOVE 0
1330#define _NET_WM_STATE_ADD 1
1331#define _NET_WM_STATE_TOGGLE 2
1332
1333static int
1334update_state(int action, int *state)
1335{
1336 int new_state, changed;
1337
1338 switch (action) {
1339 case _NET_WM_STATE_REMOVE:
1340 new_state = 0;
1341 break;
1342 case _NET_WM_STATE_ADD:
1343 new_state = 1;
1344 break;
1345 case _NET_WM_STATE_TOGGLE:
1346 new_state = !*state;
1347 break;
1348 default:
1349 return 0;
1350 }
1351
1352 changed = (*state != new_state);
1353 *state = new_state;
1354
1355 return changed;
1356}
1357
1358static void
1359weston_wm_window_configure(void *data);
1360
1361static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001362weston_wm_window_set_toplevel(struct weston_wm_window *window)
1363{
1364 struct weston_shell_interface *shell_interface =
1365 &window->wm->server->compositor->shell_interface;
1366
1367 shell_interface->set_toplevel(window->shsurf);
1368 window->width = window->saved_width;
1369 window->height = window->saved_height;
1370 if (window->frame)
1371 frame_resize_inside(window->frame,
1372 window->width,
1373 window->height);
1374 weston_wm_window_configure(window);
1375}
1376
1377static inline bool
1378weston_wm_window_is_maximized(struct weston_wm_window *window)
1379{
1380 return window->maximized_horz && window->maximized_vert;
1381}
1382
1383static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001384weston_wm_window_handle_state(struct weston_wm_window *window,
1385 xcb_client_message_event_t *client_message)
1386{
1387 struct weston_wm *wm = window->wm;
1388 struct weston_shell_interface *shell_interface =
1389 &wm->server->compositor->shell_interface;
1390 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001391 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001392
1393 action = client_message->data.data32[0];
1394 property = client_message->data.data32[1];
1395
1396 if (property == wm->atom.net_wm_state_fullscreen &&
1397 update_state(action, &window->fullscreen)) {
1398 weston_wm_window_set_net_wm_state(window);
1399 if (window->fullscreen) {
1400 window->saved_width = window->width;
1401 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001402
1403 if (window->shsurf)
1404 shell_interface->set_fullscreen(window->shsurf,
1405 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1406 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001407 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001408 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001409 weston_wm_window_set_toplevel(window);
1410 }
1411 } else {
1412 if (property == wm->atom.net_wm_state_maximized_vert &&
1413 update_state(action, &window->maximized_vert))
1414 weston_wm_window_set_net_wm_state(window);
1415 if (property == wm->atom.net_wm_state_maximized_horz &&
1416 update_state(action, &window->maximized_horz))
1417 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001418
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001419 if (maximized != weston_wm_window_is_maximized(window)) {
1420 if (weston_wm_window_is_maximized(window)) {
1421 window->saved_width = window->width;
1422 window->saved_height = window->height;
1423
1424 if (window->shsurf)
1425 shell_interface->set_maximized(window->shsurf);
1426 } else if (window->shsurf) {
1427 weston_wm_window_set_toplevel(window);
1428 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001429 }
1430 }
1431}
1432
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001433static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001434surface_destroy(struct wl_listener *listener, void *data)
1435{
1436 struct weston_wm_window *window =
1437 container_of(listener,
1438 struct weston_wm_window, surface_destroy_listener);
1439
1440 wm_log("surface for xid %d destroyed\n", window->id);
1441
1442 /* This should have been freed by the shell.
1443 * Don't try to use it later. */
1444 window->shsurf = NULL;
1445 window->surface = NULL;
1446 window->view = NULL;
1447}
1448
1449static void
1450weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1451 xcb_client_message_event_t *client_message)
1452{
1453 struct weston_wm *wm = window->wm;
1454 struct wl_resource *resource;
1455
1456 if (window->surface_id != 0) {
1457 wm_log("already have surface id for window %d\n", window->id);
1458 return;
1459 }
1460
1461 /* Xwayland will send the wayland requests to create the
1462 * wl_surface before sending this client message. Even so, we
1463 * can end up handling the X event before the wayland requests
1464 * and thus when we try to look up the surface ID, the surface
1465 * hasn't been created yet. In that case put the window on
1466 * the unpaired window list and continue when the surface gets
1467 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001468 uint32_t id = client_message->data.data32[0];
1469 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001470 if (resource) {
1471 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001472 xserver_map_shell_surface(window,
1473 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001474 }
1475 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001476 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001477 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001478 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001479}
1480
1481static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001482weston_wm_handle_client_message(struct weston_wm *wm,
1483 xcb_generic_event_t *event)
1484{
1485 xcb_client_message_event_t *client_message =
1486 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001487 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001488
1489 window = hash_table_lookup(wm->window_hash, client_message->window);
1490
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001491 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1492 get_atom_name(wm->conn, client_message->type),
1493 client_message->data.data32[0],
1494 client_message->data.data32[1],
1495 client_message->data.data32[2],
1496 client_message->data.data32[3],
1497 client_message->data.data32[4],
1498 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001499
Jason Ekstrand0250a742014-07-24 13:17:47 -07001500 /* The window may get created and destroyed before we actually
1501 * handle the message. If it doesn't exist, bail.
1502 */
1503 if (!window)
1504 return;
1505
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001506 if (client_message->type == wm->atom.net_wm_moveresize)
1507 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001508 else if (client_message->type == wm->atom.net_wm_state)
1509 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001510 else if (client_message->type == wm->atom.wl_surface_id)
1511 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001512}
1513
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001514enum cursor_type {
1515 XWM_CURSOR_TOP,
1516 XWM_CURSOR_BOTTOM,
1517 XWM_CURSOR_LEFT,
1518 XWM_CURSOR_RIGHT,
1519 XWM_CURSOR_TOP_LEFT,
1520 XWM_CURSOR_TOP_RIGHT,
1521 XWM_CURSOR_BOTTOM_LEFT,
1522 XWM_CURSOR_BOTTOM_RIGHT,
1523 XWM_CURSOR_LEFT_PTR,
1524};
1525
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001526/*
1527 * The following correspondences between file names and cursors was copied
1528 * from: https://bugs.kde.org/attachment.cgi?id=67313
1529 */
1530
1531static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001532 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001533 "sw-resize",
1534 "size_bdiag"
1535};
1536
1537static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001538 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001539 "se-resize",
1540 "size_fdiag"
1541};
1542
1543static const char *bottom_sides[] = {
1544 "bottom_side",
1545 "s-resize",
1546 "size_ver"
1547};
1548
1549static const char *left_ptrs[] = {
1550 "left_ptr",
1551 "default",
1552 "top_left_arrow",
1553 "left-arrow"
1554};
1555
1556static const char *left_sides[] = {
1557 "left_side",
1558 "w-resize",
1559 "size_hor"
1560};
1561
1562static const char *right_sides[] = {
1563 "right_side",
1564 "e-resize",
1565 "size_hor"
1566};
1567
1568static const char *top_left_corners[] = {
1569 "top_left_corner",
1570 "nw-resize",
1571 "size_fdiag"
1572};
1573
1574static const char *top_right_corners[] = {
1575 "top_right_corner",
1576 "ne-resize",
1577 "size_bdiag"
1578};
1579
1580static const char *top_sides[] = {
1581 "top_side",
1582 "n-resize",
1583 "size_ver"
1584};
1585
1586struct cursor_alternatives {
1587 const char **names;
1588 size_t count;
1589};
1590
1591static const struct cursor_alternatives cursors[] = {
1592 {top_sides, ARRAY_LENGTH(top_sides)},
1593 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1594 {left_sides, ARRAY_LENGTH(left_sides)},
1595 {right_sides, ARRAY_LENGTH(right_sides)},
1596 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1597 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1598 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1599 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1600 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001601};
1602
1603static void
1604weston_wm_create_cursors(struct weston_wm *wm)
1605{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001606 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001607 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001608 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001609
1610 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1611 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001612 for (j = 0; j < cursors[i].count; j++) {
1613 name = cursors[i].names[j];
1614 wm->cursors[i] =
1615 xcb_cursor_library_load_cursor(wm, name);
1616 if (wm->cursors[i] != (xcb_cursor_t)-1)
1617 break;
1618 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001619 }
1620
1621 wm->last_cursor = -1;
1622}
1623
1624static void
1625weston_wm_destroy_cursors(struct weston_wm *wm)
1626{
1627 uint8_t i;
1628
1629 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1630 xcb_free_cursor(wm->conn, wm->cursors[i]);
1631
1632 free(wm->cursors);
1633}
1634
1635static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001636get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001637{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001638 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001639
1640 switch (location) {
1641 case THEME_LOCATION_RESIZING_TOP:
1642 return XWM_CURSOR_TOP;
1643 case THEME_LOCATION_RESIZING_BOTTOM:
1644 return XWM_CURSOR_BOTTOM;
1645 case THEME_LOCATION_RESIZING_LEFT:
1646 return XWM_CURSOR_LEFT;
1647 case THEME_LOCATION_RESIZING_RIGHT:
1648 return XWM_CURSOR_RIGHT;
1649 case THEME_LOCATION_RESIZING_TOP_LEFT:
1650 return XWM_CURSOR_TOP_LEFT;
1651 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1652 return XWM_CURSOR_TOP_RIGHT;
1653 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1654 return XWM_CURSOR_BOTTOM_LEFT;
1655 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1656 return XWM_CURSOR_BOTTOM_RIGHT;
1657 case THEME_LOCATION_EXTERIOR:
1658 case THEME_LOCATION_TITLEBAR:
1659 default:
1660 return XWM_CURSOR_LEFT_PTR;
1661 }
1662}
1663
1664static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001665weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1666 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001667{
1668 uint32_t cursor_value_list;
1669
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001670 if (wm->last_cursor == cursor)
1671 return;
1672
1673 wm->last_cursor = cursor;
1674
1675 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001676 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001677 XCB_CW_CURSOR, &cursor_value_list);
1678 xcb_flush(wm->conn);
1679}
1680
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001681static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001682weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001683{
1684 xcb_client_message_event_t client_message;
1685
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001686 if (window->delete_window) {
1687 client_message.response_type = XCB_CLIENT_MESSAGE;
1688 client_message.format = 32;
1689 client_message.window = window->id;
1690 client_message.type = window->wm->atom.wm_protocols;
1691 client_message.data.data32[0] =
1692 window->wm->atom.wm_delete_window;
1693 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001694
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001695 xcb_send_event(window->wm->conn, 0, window->id,
1696 XCB_EVENT_MASK_NO_EVENT,
1697 (char *) &client_message);
1698 } else {
1699 xcb_kill_client(window->wm->conn, window->id);
1700 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001701}
1702
1703static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001704weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1705{
1706 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1707 struct weston_shell_interface *shell_interface =
1708 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001709 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001710 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001711 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001712 enum frame_button_state button_state;
1713 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001714
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001715 wm_log("XCB_BUTTON_%s (detail %d)\n",
1716 button->response_type == XCB_BUTTON_PRESS ?
1717 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001718
1719 window = hash_table_lookup(wm->window_hash, button->event);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001720 if (!window || !window->decorate)
1721 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001722
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001723 if (button->detail != 1 && button->detail != 2)
1724 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001725
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001726 seat = weston_wm_pick_seat_for_window(window);
1727
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001728 button_state = button->response_type == XCB_BUTTON_PRESS ?
1729 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1730 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1731
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001732 /* Make sure we're looking at the right location. The frame
1733 * could have received a motion event from a pointer from a
1734 * different wl_seat, but under X it looks like our core
1735 * pointer moved. Move the frame pointer to the button press
1736 * location before deciding what to do. */
1737 location = frame_pointer_motion(window->frame, NULL,
1738 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001739 location = frame_pointer_button(window->frame, NULL,
1740 button_id, button_state);
1741 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1742 weston_wm_window_schedule_repaint(window);
1743
1744 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001745 if (seat != NULL)
1746 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001747 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1748 }
1749
1750 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001751 if (seat != NULL)
1752 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001753 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1754 }
1755
1756 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001757 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001758 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001759 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001760
1761 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1762 window->maximized_horz = !window->maximized_horz;
1763 window->maximized_vert = !window->maximized_vert;
1764 if (weston_wm_window_is_maximized(window)) {
1765 window->saved_width = window->width;
1766 window->saved_height = window->height;
1767 shell_interface->set_maximized(window->shsurf);
1768 } else {
1769 weston_wm_window_set_toplevel(window);
1770 }
1771 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1772 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001773}
1774
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001775static void
1776weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1777{
1778 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1779 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001780 enum theme_location location;
1781 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001782
1783 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001784 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001785 return;
1786
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001787 location = frame_pointer_motion(window->frame, NULL,
1788 motion->event_x, motion->event_y);
1789 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1790 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001791
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001792 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001793 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001794}
1795
1796static void
1797weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1798{
1799 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1800 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001801 enum theme_location location;
1802 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001803
1804 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001805 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001806 return;
1807
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001808 location = frame_pointer_enter(window->frame, NULL,
1809 enter->event_x, enter->event_y);
1810 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1811 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001812
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001813 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001814 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001815}
1816
1817static void
1818weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1819{
1820 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1821 struct weston_wm_window *window;
1822
1823 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001824 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001825 return;
1826
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001827 frame_pointer_leave(window->frame, NULL);
1828 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1829 weston_wm_window_schedule_repaint(window);
1830
Tiago Vignattic1903232012-07-16 12:15:37 -04001831 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001832}
1833
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001834static int
1835weston_wm_handle_event(int fd, uint32_t mask, void *data)
1836{
1837 struct weston_wm *wm = data;
1838 xcb_generic_event_t *event;
1839 int count = 0;
1840
1841 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1842 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001843 free(event);
1844 count++;
1845 continue;
1846 }
1847
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001848 if (weston_wm_handle_dnd_event(wm, event)) {
1849 free(event);
1850 count++;
1851 continue;
1852 }
1853
MoD31700122013-06-11 19:58:55 -05001854 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001855 case XCB_BUTTON_PRESS:
1856 case XCB_BUTTON_RELEASE:
1857 weston_wm_handle_button(wm, event);
1858 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001859 case XCB_ENTER_NOTIFY:
1860 weston_wm_handle_enter(wm, event);
1861 break;
1862 case XCB_LEAVE_NOTIFY:
1863 weston_wm_handle_leave(wm, event);
1864 break;
1865 case XCB_MOTION_NOTIFY:
1866 weston_wm_handle_motion(wm, event);
1867 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001868 case XCB_CREATE_NOTIFY:
1869 weston_wm_handle_create_notify(wm, event);
1870 break;
1871 case XCB_MAP_REQUEST:
1872 weston_wm_handle_map_request(wm, event);
1873 break;
1874 case XCB_MAP_NOTIFY:
1875 weston_wm_handle_map_notify(wm, event);
1876 break;
1877 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001878 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001879 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001880 case XCB_REPARENT_NOTIFY:
1881 weston_wm_handle_reparent_notify(wm, event);
1882 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001883 case XCB_CONFIGURE_REQUEST:
1884 weston_wm_handle_configure_request(wm, event);
1885 break;
1886 case XCB_CONFIGURE_NOTIFY:
1887 weston_wm_handle_configure_notify(wm, event);
1888 break;
1889 case XCB_DESTROY_NOTIFY:
1890 weston_wm_handle_destroy_notify(wm, event);
1891 break;
1892 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001893 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001894 break;
1895 case XCB_PROPERTY_NOTIFY:
1896 weston_wm_handle_property_notify(wm, event);
1897 break;
1898 case XCB_CLIENT_MESSAGE:
1899 weston_wm_handle_client_message(wm, event);
1900 break;
1901 }
1902
1903 free(event);
1904 count++;
1905 }
1906
1907 xcb_flush(wm->conn);
1908
1909 return count;
1910}
1911
1912static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001913weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1914{
1915 xcb_depth_iterator_t d_iter;
1916 xcb_visualtype_iterator_t vt_iter;
1917 xcb_visualtype_t *visualtype;
1918
1919 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1920 visualtype = NULL;
1921 while (d_iter.rem > 0) {
1922 if (d_iter.data->depth == 32) {
1923 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1924 visualtype = vt_iter.data;
1925 break;
1926 }
1927
1928 xcb_depth_next(&d_iter);
1929 }
1930
1931 if (visualtype == NULL) {
1932 weston_log("no 32 bit visualtype\n");
1933 return;
1934 }
1935
1936 wm->visual_id = visualtype->visual_id;
1937 wm->colormap = xcb_generate_id(wm->conn);
1938 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1939 wm->colormap, wm->screen->root, wm->visual_id);
1940}
1941
1942static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001943weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001944{
1945
1946#define F(field) offsetof(struct weston_wm, field)
1947
1948 static const struct { const char *name; int offset; } atoms[] = {
1949 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001950 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001951 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1952 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001953 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001954 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001955 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001956 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001957 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001958 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001959 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1960 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001961 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
1962 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001963 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1964 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1965 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001966 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001967 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1968
1969 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1970 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1971 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1972 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1973 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1974 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1975 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001976 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1977 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001978 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1979 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1980 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1981 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1982 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1983
1984 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1985 { "_NET_SUPPORTING_WM_CHECK",
1986 F(atom.net_supporting_wm_check) },
1987 { "_NET_SUPPORTED", F(atom.net_supported) },
1988 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1989 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001990 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001991 { "TARGETS", F(atom.targets) },
1992 { "UTF8_STRING", F(atom.utf8_string) },
1993 { "_WL_SELECTION", F(atom.wl_selection) },
1994 { "INCR", F(atom.incr) },
1995 { "TIMESTAMP", F(atom.timestamp) },
1996 { "MULTIPLE", F(atom.multiple) },
1997 { "UTF8_STRING" , F(atom.utf8_string) },
1998 { "COMPOUND_TEXT", F(atom.compound_text) },
1999 { "TEXT", F(atom.text) },
2000 { "STRING", F(atom.string) },
2001 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2002 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002003 { "XdndSelection", F(atom.xdnd_selection) },
2004 { "XdndAware", F(atom.xdnd_aware) },
2005 { "XdndEnter", F(atom.xdnd_enter) },
2006 { "XdndLeave", F(atom.xdnd_leave) },
2007 { "XdndDrop", F(atom.xdnd_drop) },
2008 { "XdndStatus", F(atom.xdnd_status) },
2009 { "XdndFinished", F(atom.xdnd_finished) },
2010 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002011 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2012 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002013 };
2014#undef F
2015
2016 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2017 xcb_xfixes_query_version_reply_t *xfixes_reply;
2018 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2019 xcb_intern_atom_reply_t *reply;
2020 xcb_render_query_pict_formats_reply_t *formats_reply;
2021 xcb_render_query_pict_formats_cookie_t formats_cookie;
2022 xcb_render_pictforminfo_t *formats;
2023 uint32_t i;
2024
2025 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002026 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002027
2028 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2029
2030 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2031 cookies[i] = xcb_intern_atom (wm->conn, 0,
2032 strlen(atoms[i].name),
2033 atoms[i].name);
2034
2035 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2036 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2037 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2038 free(reply);
2039 }
2040
2041 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2042 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002043 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002044
2045 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2046 XCB_XFIXES_MAJOR_VERSION,
2047 XCB_XFIXES_MINOR_VERSION);
2048 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2049 xfixes_cookie, NULL);
2050
Martin Minarik6d118362012-06-07 18:01:59 +02002051 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002052 xfixes_reply->major_version, xfixes_reply->minor_version);
2053
2054 free(xfixes_reply);
2055
2056 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2057 formats_cookie, 0);
2058 if (formats_reply == NULL)
2059 return;
2060
2061 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002062 for (i = 0; i < formats_reply->num_formats; i++) {
2063 if (formats[i].direct.red_mask != 0xff &&
2064 formats[i].direct.red_shift != 16)
2065 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002066 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2067 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002068 wm->format_rgb = formats[i];
2069 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2070 formats[i].depth == 32 &&
2071 formats[i].direct.alpha_mask == 0xff &&
2072 formats[i].direct.alpha_shift == 24)
2073 wm->format_rgba = formats[i];
2074 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002075
2076 free(formats_reply);
2077}
2078
2079static void
2080weston_wm_create_wm_window(struct weston_wm *wm)
2081{
2082 static const char name[] = "Weston WM";
2083
2084 wm->wm_window = xcb_generate_id(wm->conn);
2085 xcb_create_window(wm->conn,
2086 XCB_COPY_FROM_PARENT,
2087 wm->wm_window,
2088 wm->screen->root,
2089 0, 0,
2090 10, 10,
2091 0,
2092 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2093 wm->screen->root_visual,
2094 0, NULL);
2095
2096 xcb_change_property(wm->conn,
2097 XCB_PROP_MODE_REPLACE,
2098 wm->wm_window,
2099 wm->atom.net_supporting_wm_check,
2100 XCB_ATOM_WINDOW,
2101 32, /* format */
2102 1, &wm->wm_window);
2103
2104 xcb_change_property(wm->conn,
2105 XCB_PROP_MODE_REPLACE,
2106 wm->wm_window,
2107 wm->atom.net_wm_name,
2108 wm->atom.utf8_string,
2109 8, /* format */
2110 strlen(name), name);
2111
2112 xcb_change_property(wm->conn,
2113 XCB_PROP_MODE_REPLACE,
2114 wm->screen->root,
2115 wm->atom.net_supporting_wm_check,
2116 XCB_ATOM_WINDOW,
2117 32, /* format */
2118 1, &wm->wm_window);
2119
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002120 /* Claim the WM_S0 selection even though we don't suport
2121 * the --replace functionality. */
2122 xcb_set_selection_owner(wm->conn,
2123 wm->wm_window,
2124 wm->atom.wm_s0,
2125 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002126
2127 xcb_set_selection_owner(wm->conn,
2128 wm->wm_window,
2129 wm->atom.net_wm_cm_s0,
2130 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002131}
2132
2133struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002134weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002135{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002136 struct weston_wm *wm;
2137 struct wl_event_loop *loop;
2138 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002139 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002140 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002141
Peter Huttererf3d62272013-08-08 11:57:05 +10002142 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002143 if (wm == NULL)
2144 return NULL;
2145
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002146 wm->server = wxs;
2147 wm->window_hash = hash_table_create();
2148 if (wm->window_hash == NULL) {
2149 free(wm);
2150 return NULL;
2151 }
2152
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002153 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002154 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002155 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002156 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002157 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002158 hash_table_destroy(wm->window_hash);
2159 free(wm);
2160 return NULL;
2161 }
2162
2163 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2164 wm->screen = s.data;
2165
2166 loop = wl_display_get_event_loop(wxs->wl_display);
2167 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002168 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002169 WL_EVENT_READABLE,
2170 weston_wm_handle_event, wm);
2171 wl_event_source_check(wm->source);
2172
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002173 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002174 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002175
2176 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002177 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2178 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2179 XCB_EVENT_MASK_PROPERTY_CHANGE;
2180 xcb_change_window_attributes(wm->conn, wm->screen->root,
2181 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002182
2183 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2184 XCB_COMPOSITE_REDIRECT_MANUAL);
2185
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002186 wm->theme = theme_create();
2187
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002188 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002189 supported[1] = wm->atom.net_wm_state;
2190 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002191 supported[3] = wm->atom.net_wm_state_maximized_vert;
2192 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002193 xcb_change_property(wm->conn,
2194 XCB_PROP_MODE_REPLACE,
2195 wm->screen->root,
2196 wm->atom.net_supported,
2197 XCB_ATOM_ATOM,
2198 32, /* format */
2199 ARRAY_LENGTH(supported), supported);
2200
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002201 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002202
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002203 weston_wm_dnd_init(wm);
2204
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002205 xcb_flush(wm->conn);
2206
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002207 wm->create_surface_listener.notify = weston_wm_create_surface;
2208 wl_signal_add(&wxs->compositor->create_surface_signal,
2209 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002210 wm->activate_listener.notify = weston_wm_window_activate;
2211 wl_signal_add(&wxs->compositor->activate_signal,
2212 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002213 wm->transform_listener.notify = weston_wm_window_transform;
2214 wl_signal_add(&wxs->compositor->transform_signal,
2215 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002216 wm->kill_listener.notify = weston_wm_kill_client;
2217 wl_signal_add(&wxs->compositor->kill_signal,
2218 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002219 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002220
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002221 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002222 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002223
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002224 /* Create wm window and take WM_S0 selection last, which
2225 * signals to Xwayland that we're done with setup. */
2226 weston_wm_create_wm_window(wm);
2227
2228 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002229
2230 return wm;
2231}
2232
2233void
2234weston_wm_destroy(struct weston_wm *wm)
2235{
2236 /* FIXME: Free windows in hash. */
2237 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002238 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002239 xcb_disconnect(wm->conn);
2240 wl_event_source_remove(wm->source);
2241 wl_list_remove(&wm->selection_listener.link);
2242 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002243 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002244 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002245 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002246
2247 free(wm);
2248}
2249
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002250static struct weston_wm_window *
2251get_wm_window(struct weston_surface *surface)
2252{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002253 struct wl_listener *listener;
2254
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002255 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002256 if (listener)
2257 return container_of(listener, struct weston_wm_window,
2258 surface_destroy_listener);
2259
2260 return NULL;
2261}
2262
2263static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002264weston_wm_window_configure(void *data)
2265{
2266 struct weston_wm_window *window = data;
2267 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002268 uint32_t values[4];
2269 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002270
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002271 weston_wm_window_get_child_position(window, &x, &y);
2272 values[0] = x;
2273 values[1] = y;
2274 values[2] = window->width;
2275 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002276 xcb_configure_window(wm->conn,
2277 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002278 XCB_CONFIG_WINDOW_X |
2279 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002280 XCB_CONFIG_WINDOW_WIDTH |
2281 XCB_CONFIG_WINDOW_HEIGHT,
2282 values);
2283
2284 weston_wm_window_get_frame_size(window, &width, &height);
2285 values[0] = width;
2286 values[1] = height;
2287 xcb_configure_window(wm->conn,
2288 window->frame_id,
2289 XCB_CONFIG_WINDOW_WIDTH |
2290 XCB_CONFIG_WINDOW_HEIGHT,
2291 values);
2292
2293 window->configure_source = NULL;
2294
2295 weston_wm_window_schedule_repaint(window);
2296}
2297
2298static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002299send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002300{
2301 struct weston_wm_window *window = get_wm_window(surface);
2302 struct weston_wm *wm = window->wm;
2303 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002304 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002305
Marek Chalupae9fe4672014-10-29 13:44:44 +01002306 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002307 hborder = 2 * t->width;
2308 vborder = t->titlebar_height + t->width;
2309 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002310 hborder = 0;
2311 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002312 }
2313
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002314 if (width > hborder)
2315 window->width = width - hborder;
2316 else
2317 window->width = 1;
2318
2319 if (height > vborder)
2320 window->height = height - vborder;
2321 else
2322 window->height = 1;
2323
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002324 if (window->frame)
2325 frame_resize_inside(window->frame, window->width, window->height);
2326
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002327 if (window->configure_source)
2328 return;
2329
2330 window->configure_source =
2331 wl_event_loop_add_idle(wm->server->loop,
2332 weston_wm_window_configure, window);
2333}
2334
2335static const struct weston_shell_client shell_client = {
2336 send_configure
2337};
2338
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002339static int
2340legacy_fullscreen(struct weston_wm *wm,
2341 struct weston_wm_window *window,
2342 struct weston_output **output_ret)
2343{
2344 struct weston_compositor *compositor = wm->server->compositor;
2345 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002346 uint32_t minmax = PMinSize | PMaxSize;
2347 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002348
2349 /* Heuristics for detecting legacy fullscreen windows... */
2350
2351 wl_list_for_each(output, &compositor->output_list, link) {
2352 if (output->x == window->x &&
2353 output->y == window->y &&
2354 output->width == window->width &&
2355 output->height == window->height &&
2356 window->override_redirect) {
2357 *output_ret = output;
2358 return 1;
2359 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002360
2361 matching_size = 0;
2362 if ((window->size_hints.flags & (USSize |PSize)) &&
2363 window->size_hints.width == output->width &&
2364 window->size_hints.height == output->height)
2365 matching_size = 1;
2366 if ((window->size_hints.flags & minmax) == minmax &&
2367 window->size_hints.min_width == output->width &&
2368 window->size_hints.min_height == output->height &&
2369 window->size_hints.max_width == output->width &&
2370 window->size_hints.max_height == output->height)
2371 matching_size = 1;
2372
2373 if (matching_size && !window->decorate &&
2374 (window->size_hints.flags & (USPosition | PPosition)) &&
2375 window->size_hints.x == output->x &&
2376 window->size_hints.y == output->y) {
2377 *output_ret = output;
2378 return 1;
2379 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002380 }
2381
2382 return 0;
2383}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002384
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002385static bool
2386weston_wm_window_type_inactive(struct weston_wm_window *window)
2387{
2388 struct weston_wm *wm = window->wm;
2389
2390 return window->type == wm->atom.net_wm_window_type_tooltip ||
2391 window->type == wm->atom.net_wm_window_type_dropdown ||
2392 window->type == wm->atom.net_wm_window_type_dnd ||
2393 window->type == wm->atom.net_wm_window_type_combo ||
2394 window->type == wm->atom.net_wm_window_type_popup;
2395}
2396
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002397static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002398xserver_map_shell_surface(struct weston_wm_window *window,
2399 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002400{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002401 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002402 struct weston_shell_interface *shell_interface =
2403 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002404 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002405 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002406 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002407
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002408 weston_wm_window_read_properties(window);
2409
2410 /* A weston_wm_window may have many different surfaces assigned
2411 * throughout its life, so we must make sure to remove the listener
2412 * from the old surface signal list. */
2413 if (window->surface)
2414 wl_list_remove(&window->surface_destroy_listener.link);
2415
2416 window->surface = surface;
2417 window->surface_destroy_listener.notify = surface_destroy;
2418 wl_signal_add(&window->surface->destroy_signal,
2419 &window->surface_destroy_listener);
2420
2421 weston_wm_window_schedule_repaint(window);
2422
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002423 if (!shell_interface->create_shell_surface)
2424 return;
2425
Jason Ekstranda7af7042013-10-12 22:38:11 -05002426 if (!shell_interface->get_primary_view)
2427 return;
2428
Pekka Paalanen50b67472014-10-01 15:02:41 +03002429 if (window->surface->configure) {
2430 weston_log("warning, unexpected in %s: "
2431 "surface's configure hook is already set.\n",
2432 __func__);
2433 return;
2434 }
2435
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002436 window->shsurf =
2437 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002438 window->surface,
2439 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002440 window->view = shell_interface->get_primary_view(shell_interface->shell,
2441 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002442
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002443 if (window->name)
2444 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002445 if (window->pid > 0)
2446 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002447
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002448 if (window->fullscreen) {
2449 window->saved_width = window->width;
2450 window->saved_height = window->height;
2451 shell_interface->set_fullscreen(window->shsurf,
2452 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2453 0, NULL);
2454 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002455 } else if (legacy_fullscreen(wm, window, &output)) {
2456 window->fullscreen = 1;
2457 shell_interface->set_fullscreen(window->shsurf,
2458 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2459 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002460 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002461 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002462 window->x,
2463 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002464 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002465 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002466 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002467 if (weston_wm_window_type_inactive(window))
2468 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002469 shell_interface->set_transient(window->shsurf,
2470 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002471 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002472 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002473 } else if (weston_wm_window_is_maximized(window)) {
2474 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002475 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002476 if (weston_wm_window_type_inactive(window)) {
2477 shell_interface->set_xwayland(window->shsurf,
2478 window->x,
2479 window->y,
2480 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2481 } else {
2482 shell_interface->set_toplevel(window->shsurf);
2483 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002484 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002485}