blob: cab7e20e764ae90241a832e45654e02aa6312cdd [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
Derek Foreman49372142015-04-09 10:51:22 -0500198static bool __attribute__ ((warn_unused_result))
199wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
200 struct weston_wm_window **window)
201{
202 *window = hash_table_lookup(wm->window_hash, hash);
203 if (*window)
204 return true;
205 return false;
206}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400207
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400208const char *
209get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
210{
211 xcb_get_atom_name_cookie_t cookie;
212 xcb_get_atom_name_reply_t *reply;
213 xcb_generic_error_t *e;
214 static char buffer[64];
215
216 if (atom == XCB_ATOM_NONE)
217 return "None";
218
219 cookie = xcb_get_atom_name (c, atom);
220 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500221
222 if(reply) {
223 snprintf(buffer, sizeof buffer, "%.*s",
224 xcb_get_atom_name_name_length (reply),
225 xcb_get_atom_name_name (reply));
226 } else {
227 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
228 }
229
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400230 free(reply);
231
232 return buffer;
233}
234
Tiago Vignatti90fada42012-07-16 12:02:08 -0400235static xcb_cursor_t
236xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
237{
238 xcb_connection_t *c = wm->conn;
239 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
240 xcb_screen_t *screen = s.data;
241 xcb_gcontext_t gc;
242 xcb_pixmap_t pix;
243 xcb_render_picture_t pic;
244 xcb_cursor_t cursor;
245 int stride = img->width * 4;
246
247 pix = xcb_generate_id(c);
248 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
249
250 pic = xcb_generate_id(c);
251 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
252
253 gc = xcb_generate_id(c);
254 xcb_create_gc(c, gc, pix, 0, 0);
255
256 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
257 img->width, img->height, 0, 0, 0, 32,
258 stride * img->height, (uint8_t *) img->pixels);
259 xcb_free_gc(c, gc);
260
261 cursor = xcb_generate_id(c);
262 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
263
264 xcb_render_free_picture(c, pic);
265 xcb_free_pixmap(c, pix);
266
267 return cursor;
268}
269
270static xcb_cursor_t
271xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
272{
273 /* TODO: treat animated cursors as well */
274 if (images->nimage != 1)
275 return -1;
276
277 return xcb_cursor_image_load_cursor(wm, images->images[0]);
278}
279
280static xcb_cursor_t
281xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
282{
283 xcb_cursor_t cursor;
284 XcursorImages *images;
285 char *v = NULL;
286 int size = 0;
287
288 if (!file)
289 return 0;
290
291 v = getenv ("XCURSOR_SIZE");
292 if (v)
293 size = atoi(v);
294
295 if (!size)
296 size = 32;
297
298 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300299 if (!images)
300 return -1;
301
Tiago Vignatti90fada42012-07-16 12:02:08 -0400302 cursor = xcb_cursor_images_load_cursor (wm, images);
303 XcursorImagesDestroy (images);
304
305 return cursor;
306}
307
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400308void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400309dump_property(struct weston_wm *wm,
310 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400311{
312 int32_t *incr_value;
313 const char *text_value, *name;
314 xcb_atom_t *atom_value;
315 int width, len;
316 uint32_t i;
317
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400318 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400319 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400320 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400321 return;
322 }
323
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400324 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
325 get_atom_name(wm->conn, reply->type),
326 reply->format,
327 xcb_get_property_value_length(reply),
328 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400329
330 if (reply->type == wm->atom.incr) {
331 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400332 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400333 } else if (reply->type == wm->atom.utf8_string ||
334 reply->type == wm->atom.string) {
335 text_value = xcb_get_property_value(reply);
336 if (reply->value_len > 40)
337 len = 40;
338 else
339 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400340 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400341 } else if (reply->type == XCB_ATOM_ATOM) {
342 atom_value = xcb_get_property_value(reply);
343 for (i = 0; i < reply->value_len; i++) {
344 name = get_atom_name(wm->conn, atom_value[i]);
345 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400346 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400347 width = 4;
348 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400349 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400350 }
351
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400352 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400353 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400354 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400355 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400356 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357 }
358}
359
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200360static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400361read_and_dump_property(struct weston_wm *wm,
362 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400364 xcb_get_property_reply_t *reply;
365 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400366
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400367 cookie = xcb_get_property(wm->conn, 0, window,
368 property, XCB_ATOM_ANY, 0, 2048);
369 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400370
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400371 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400372
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400373 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400374}
375
376/* We reuse some predefined, but otherwise useles atoms */
377#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
378#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500379#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700380#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381
382static void
383weston_wm_window_read_properties(struct weston_wm_window *window)
384{
385 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200386 struct weston_shell_interface *shell_interface =
387 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400388
389#define F(field) offsetof(struct weston_wm_window, field)
390 const struct {
391 xcb_atom_t atom;
392 xcb_atom_t type;
393 int offset;
394 } props[] = {
395 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
396 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
397 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
398 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700399 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500400 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400401 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
402 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300403 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400404 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300405 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400406 };
407#undef F
408
409 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
410 xcb_get_property_reply_t *reply;
411 void *p;
412 uint32_t *xid;
413 xcb_atom_t *atom;
414 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200415 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400416
417 if (!window->properties_dirty)
418 return;
419 window->properties_dirty = 0;
420
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400421 for (i = 0; i < ARRAY_LENGTH(props); i++)
422 cookie[i] = xcb_get_property(wm->conn,
423 0, /* delete */
424 window->id,
425 props[i].atom,
426 XCB_ATOM_ANY, 0, 2048);
427
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300428 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700429 window->size_hints.flags = 0;
430 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700431 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700432
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400433 for (i = 0; i < ARRAY_LENGTH(props); i++) {
434 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
435 if (!reply)
436 /* Bad window, typically */
437 continue;
438 if (reply->type == XCB_ATOM_NONE) {
439 /* No such property */
440 free(reply);
441 continue;
442 }
443
444 p = ((char *) window + props[i].offset);
445
446 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300447 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400448 case XCB_ATOM_STRING:
449 /* FIXME: We're using this for both string and
450 utf8_string */
451 if (*(char **) p)
452 free(*(char **) p);
453
454 *(char **) p =
455 strndup(xcb_get_property_value(reply),
456 xcb_get_property_value_length(reply));
457 break;
458 case XCB_ATOM_WINDOW:
459 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500460 if (!wm_lookup_window(wm, *xid, p))
461 weston_log("XCB_ATOM_WINDOW contains window"
462 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400463 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300464 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400465 case XCB_ATOM_ATOM:
466 atom = xcb_get_property_value(reply);
467 *(xcb_atom_t *) p = *atom;
468 break;
469 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700470 atom = xcb_get_property_value(reply);
471 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500472 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700473 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500474 break;
475 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400476 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700477 case TYPE_WM_NORMAL_HINTS:
478 memcpy(&window->size_hints,
479 xcb_get_property_value(reply),
480 sizeof window->size_hints);
481 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500482 case TYPE_NET_WM_STATE:
483 window->fullscreen = 0;
484 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900485 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500486 if (atom[i] == wm->atom.net_wm_state_fullscreen)
487 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200488 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
489 window->maximized_vert = 1;
490 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
491 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900492 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500493 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400494 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700495 memcpy(&window->motif_hints,
496 xcb_get_property_value(reply),
497 sizeof window->motif_hints);
498 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
499 window->decorate =
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200500 window->motif_hints.decorations;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400501 break;
502 default:
503 break;
504 }
505 free(reply);
506 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200507
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200508 if (window->pid > 0) {
509 gethostname(name, sizeof(name));
510 for (i = 0; i < sizeof(name); i++) {
511 if (name[i] == '\0')
512 break;
513 }
514 if (i == sizeof(name))
515 name[0] = '\0'; /* ignore stupid hostnames */
516
517 /* this is only one heuristic to guess the PID of a client is
518 * valid, assuming it's compliant with icccm and ewmh.
519 * Non-compliants and remote applications of course fail. */
520 if (!window->machine || strcmp(window->machine, name))
521 window->pid = 0;
522 }
523
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200524 if (window->shsurf && window->name)
525 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500526 if (window->frame && window->name)
527 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200528 if (window->shsurf && window->pid > 0)
529 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400530}
531
532static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400533weston_wm_window_get_frame_size(struct weston_wm_window *window,
534 int *width, int *height)
535{
536 struct theme *t = window->wm->theme;
537
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500538 if (window->fullscreen) {
539 *width = window->width;
540 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800541 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500542 *width = frame_width(window->frame);
543 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400544 } else {
545 *width = window->width + t->margin * 2;
546 *height = window->height + t->margin * 2;
547 }
548}
549
550static void
551weston_wm_window_get_child_position(struct weston_wm_window *window,
552 int *x, int *y)
553{
554 struct theme *t = window->wm->theme;
555
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500556 if (window->fullscreen) {
557 *x = 0;
558 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800559 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500560 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400561 } else {
562 *x = t->margin;
563 *y = t->margin;
564 }
565}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400566
567static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500568weston_wm_window_send_configure_notify(struct weston_wm_window *window)
569{
570 xcb_configure_notify_event_t configure_notify;
571 struct weston_wm *wm = window->wm;
572 int x, y;
573
574 weston_wm_window_get_child_position(window, &x, &y);
575 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
576 configure_notify.pad0 = 0;
577 configure_notify.event = window->id;
578 configure_notify.window = window->id;
579 configure_notify.above_sibling = XCB_WINDOW_NONE;
580 configure_notify.x = x;
581 configure_notify.y = y;
582 configure_notify.width = window->width;
583 configure_notify.height = window->height;
584 configure_notify.border_width = 0;
585 configure_notify.override_redirect = 0;
586 configure_notify.pad1 = 0;
587
588 xcb_send_event(wm->conn, 0, window->id,
589 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
590 (char *) &configure_notify);
591}
592
593static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400594weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
595{
596 xcb_configure_request_event_t *configure_request =
597 (xcb_configure_request_event_t *) event;
598 struct weston_wm_window *window;
599 uint32_t mask, values[16];
600 int x, y, width, height, i = 0;
601
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400602 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
603 configure_request->window,
604 configure_request->x, configure_request->y,
605 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400606
Derek Foreman49372142015-04-09 10:51:22 -0500607 if (!wm_lookup_window(wm, configure_request->window, &window))
608 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400609
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500610 if (window->fullscreen) {
611 weston_wm_window_send_configure_notify(window);
612 return;
613 }
614
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400615 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
616 window->width = configure_request->width;
617 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
618 window->height = configure_request->height;
619
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500620 if (window->frame)
621 frame_resize_inside(window->frame, window->width, window->height);
622
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400623 weston_wm_window_get_child_position(window, &x, &y);
624 values[i++] = x;
625 values[i++] = y;
626 values[i++] = window->width;
627 values[i++] = window->height;
628 values[i++] = 0;
629 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
630 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
631 XCB_CONFIG_WINDOW_BORDER_WIDTH;
632 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
633 values[i++] = configure_request->sibling;
634 mask |= XCB_CONFIG_WINDOW_SIBLING;
635 }
636 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
637 values[i++] = configure_request->stack_mode;
638 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
639 }
640
641 xcb_configure_window(wm->conn, window->id, mask, values);
642
643 weston_wm_window_get_frame_size(window, &width, &height);
644 values[0] = width;
645 values[1] = height;
646 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
647 xcb_configure_window(wm->conn, window->frame_id, mask, values);
648
649 weston_wm_window_schedule_repaint(window);
650}
651
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400652static int
653our_resource(struct weston_wm *wm, uint32_t id)
654{
655 const xcb_setup_t *setup;
656
657 setup = xcb_get_setup(wm->conn);
658
659 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
660}
661
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400662static void
663weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
664{
665 xcb_configure_notify_event_t *configure_notify =
666 (xcb_configure_notify_event_t *) event;
667 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400668
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400669 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400670 configure_notify->window,
671 configure_notify->x, configure_notify->y,
672 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300673
Derek Foreman49372142015-04-09 10:51:22 -0500674 if (!wm_lookup_window(wm, configure_notify->window, &window))
675 return;
676
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700677 window->x = configure_notify->x;
678 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700679 if (window->override_redirect) {
680 window->width = configure_notify->width;
681 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500682 if (window->frame)
683 frame_resize_inside(window->frame,
684 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700685 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400686}
687
688static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300689weston_wm_kill_client(struct wl_listener *listener, void *data)
690{
691 struct weston_surface *surface = data;
692 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300693 if (!window)
694 return;
695
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200696 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300697 kill(window->pid, SIGKILL);
698}
699
700static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700701weston_wm_create_surface(struct wl_listener *listener, void *data)
702{
703 struct weston_surface *surface = data;
704 struct weston_wm *wm =
705 container_of(listener,
706 struct weston_wm, create_surface_listener);
707 struct weston_wm_window *window;
708
709 if (wl_resource_get_client(surface->resource) != wm->server->client)
710 return;
711
712 wl_list_for_each(window, &wm->unpaired_window_list, link)
713 if (window->surface_id ==
714 wl_resource_get_id(surface->resource)) {
715 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700716 window->surface_id = 0;
717 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700718 break;
719 }
720}
721
722static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400723weston_wm_window_activate(struct wl_listener *listener, void *data)
724{
725 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200726 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600727 struct weston_wm *wm =
728 container_of(listener, struct weston_wm, activate_listener);
729 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400730
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200731 if (surface) {
732 window = get_wm_window(surface);
733 }
734
Scott Moreau85ecac02012-05-21 15:49:14 -0600735 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700736 uint32_t values[1];
737
Boyan Dingb9f863c2014-08-30 10:33:23 +0800738 if (window->override_redirect)
739 return;
740
Scott Moreau85ecac02012-05-21 15:49:14 -0600741 client_message.response_type = XCB_CLIENT_MESSAGE;
742 client_message.format = 32;
743 client_message.window = window->id;
744 client_message.type = wm->atom.wm_protocols;
745 client_message.data.data32[0] = wm->atom.wm_take_focus;
746 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
747
748 xcb_send_event(wm->conn, 0, window->id,
749 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
750 (char *) &client_message);
751
752 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
753 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700754
755 values[0] = XCB_STACK_MODE_ABOVE;
756 xcb_configure_window (wm->conn, window->frame_id,
757 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600758 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400759 xcb_set_input_focus (wm->conn,
760 XCB_INPUT_FOCUS_POINTER_ROOT,
761 XCB_NONE,
762 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600763 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500765 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800766 if (wm->focus_window->frame)
767 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400768 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500769 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400770 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500771 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800772 if (wm->focus_window->frame)
773 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400774 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500775 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400776}
777
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300778static void
779weston_wm_window_transform(struct wl_listener *listener, void *data)
780{
781 struct weston_surface *surface = data;
782 struct weston_wm_window *window = get_wm_window(surface);
783 struct weston_wm *wm =
784 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300785 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300786
787 if (!window || !wm)
788 return;
789
Jason Ekstranda7af7042013-10-12 22:38:11 -0500790 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300791 return;
792
Jason Ekstranda7af7042013-10-12 22:38:11 -0500793 if (window->x != window->view->geometry.x ||
794 window->y != window->view->geometry.y) {
795 values[0] = window->view->geometry.x;
796 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700797 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300798
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700799 xcb_configure_window(wm->conn, window->frame_id, mask, values);
800 xcb_flush(wm->conn);
801 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300802}
803
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400804#define ICCCM_WITHDRAWN_STATE 0
805#define ICCCM_NORMAL_STATE 1
806#define ICCCM_ICONIC_STATE 3
807
808static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500809weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400810{
811 struct weston_wm *wm = window->wm;
812 uint32_t property[2];
813
814 property[0] = state;
815 property[1] = XCB_WINDOW_NONE;
816
817 xcb_change_property(wm->conn,
818 XCB_PROP_MODE_REPLACE,
819 window->id,
820 wm->atom.wm_state,
821 wm->atom.wm_state,
822 32, /* format */
823 2, property);
824}
825
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400826static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500827weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
828{
829 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200830 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500831 int i;
832
833 i = 0;
834 if (window->fullscreen)
835 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200836 if (window->maximized_vert)
837 property[i++] = wm->atom.net_wm_state_maximized_vert;
838 if (window->maximized_horz)
839 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500840
841 xcb_change_property(wm->conn,
842 XCB_PROP_MODE_REPLACE,
843 window->id,
844 wm->atom.net_wm_state,
845 XCB_ATOM_ATOM,
846 32, /* format */
847 i, property);
848}
849
850static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700851weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400852{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700853 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400854 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400855 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200856 int buttons = FRAME_BUTTON_CLOSE;
857
858 if (window->decorate & MWM_DECOR_MAXIMIZE)
859 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400860
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500861 window->frame = frame_create(window->wm->theme,
862 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200863 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500864 frame_resize_inside(window->frame, window->width, window->height);
865
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400866 weston_wm_window_get_frame_size(window, &width, &height);
867 weston_wm_window_get_child_position(window, &x, &y);
868
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400869 values[0] = wm->screen->black_pixel;
870 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400871 XCB_EVENT_MASK_KEY_PRESS |
872 XCB_EVENT_MASK_KEY_RELEASE |
873 XCB_EVENT_MASK_BUTTON_PRESS |
874 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400875 XCB_EVENT_MASK_POINTER_MOTION |
876 XCB_EVENT_MASK_ENTER_WINDOW |
877 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400878 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400879 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400880 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400881
882 window->frame_id = xcb_generate_id(wm->conn);
883 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400884 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400885 window->frame_id,
886 wm->screen->root,
887 0, 0,
888 width, height,
889 0,
890 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400891 wm->visual_id,
892 XCB_CW_BORDER_PIXEL |
893 XCB_CW_EVENT_MASK |
894 XCB_CW_COLORMAP, values);
895
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400896 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
897
898 values[0] = 0;
899 xcb_configure_window(wm->conn, window->id,
900 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
901
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400902 window->cairo_surface =
903 cairo_xcb_surface_create_with_xrender_format(wm->conn,
904 wm->screen,
905 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400906 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400907 width, height);
908
909 hash_table_insert(wm->window_hash, window->frame_id, window);
910}
911
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200912/*
913 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
914 * Passing a <0 desktop value deletes the property.
915 */
916static void
917weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
918 int desktop)
919{
920 if (desktop >= 0) {
921 xcb_change_property(window->wm->conn,
922 XCB_PROP_MODE_REPLACE,
923 window->id,
924 window->wm->atom.net_wm_desktop,
925 XCB_ATOM_CARDINAL,
926 32, /* format */
927 1, &desktop);
928 } else {
929 xcb_delete_property(window->wm->conn,
930 window->id,
931 window->wm->atom.net_wm_desktop);
932 }
933}
934
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400935static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700936weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
937{
938 xcb_map_request_event_t *map_request =
939 (xcb_map_request_event_t *) event;
940 struct weston_wm_window *window;
941
942 if (our_resource(wm, map_request->window)) {
943 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
944 map_request->window);
945 return;
946 }
947
Derek Foreman49372142015-04-09 10:51:22 -0500948 if (!wm_lookup_window(wm, map_request->window, &window))
949 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700950
951 weston_wm_window_read_properties(window);
952
953 if (window->frame_id == XCB_WINDOW_NONE)
954 weston_wm_window_create_frame(window);
955
956 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
957 window->id, window, window->frame_id);
958
959 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
960 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200961 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700962
963 xcb_map_window(wm->conn, map_request->window);
964 xcb_map_window(wm->conn, window->frame_id);
965}
966
967static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400968weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
969{
970 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
971
972 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400973 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
974 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400975 return;
976 }
977
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400978 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400979}
980
981static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400982weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
983{
984 xcb_unmap_notify_event_t *unmap_notify =
985 (xcb_unmap_notify_event_t *) event;
986 struct weston_wm_window *window;
987
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400988 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
989 unmap_notify->window,
990 unmap_notify->event,
991 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400992
993 if (our_resource(wm, unmap_notify->window))
994 return;
995
MoD31700122013-06-11 19:58:55 -0500996 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400997 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
998 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400999 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001000
Derek Foreman49372142015-04-09 10:51:22 -05001001 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1002 return;
1003
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001004 if (window->surface_id) {
1005 /* Make sure we're not on the unpaired surface list or we
1006 * could be assigned a surface during surface creation that
1007 * was mapped before this unmap request.
1008 */
1009 wl_list_remove(&window->link);
1010 window->surface_id = 0;
1011 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001012 if (wm->focus_window == window)
1013 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001014 if (window->surface)
1015 wl_list_remove(&window->surface_destroy_listener.link);
1016 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001017 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -08001018 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001019
1020 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1021 weston_wm_window_set_virtual_desktop(window, -1);
1022
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001023 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001024}
1025
1026static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001027weston_wm_window_draw_decoration(void *data)
1028{
1029 struct weston_wm_window *window = data;
1030 struct weston_wm *wm = window->wm;
1031 struct theme *t = wm->theme;
1032 cairo_t *cr;
1033 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001034 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001035 struct weston_shell_interface *shell_interface =
1036 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001037 uint32_t flags = 0;
1038
1039 weston_wm_window_read_properties(window);
1040
1041 window->repaint_source = NULL;
1042
1043 weston_wm_window_get_frame_size(window, &width, &height);
1044 weston_wm_window_get_child_position(window, &x, &y);
1045
1046 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1047 cr = cairo_create(window->cairo_surface);
1048
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001049 if (window->fullscreen) {
1050 /* nothing */
1051 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001052 if (wm->focus_window == window)
1053 flags |= THEME_FRAME_ACTIVE;
1054
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001055 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001056 } else {
1057 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1058 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1059 cairo_paint(cr);
1060
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001061 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001062 }
1063
1064 cairo_destroy(cr);
1065
1066 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001067 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001068 if(window->has_alpha) {
1069 pixman_region32_init(&window->surface->pending.opaque);
1070 } else {
1071 /* We leave an extra pixel around the X window area to
1072 * make sure we don't sample from the undefined alpha
1073 * channel when filtering. */
1074 pixman_region32_init_rect(&window->surface->pending.opaque,
1075 x - 1, y - 1,
1076 window->width + 2,
1077 window->height + 2);
1078 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 if (window->view)
1080 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001081
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001082 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001083
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001084 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001085 frame_input_rect(window->frame, &input_x, &input_y,
1086 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001087 } else {
1088 input_x = x;
1089 input_y = y;
1090 input_w = width;
1091 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001092 }
1093
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001094 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001095 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001096
1097 shell_interface->set_window_geometry(window->shsurf,
1098 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001099 }
1100}
1101
1102static void
1103weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1104{
1105 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001106 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001107
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001108 if (window->frame_id == XCB_WINDOW_NONE) {
1109 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001110 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001111 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001112 if(window->has_alpha) {
1113 pixman_region32_init(&window->surface->pending.opaque);
1114 } else {
1115 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1116 width, height);
1117 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 if (window->view)
1119 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001120 }
1121 return;
1122 }
1123
1124 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001125 return;
1126
1127 window->repaint_source =
1128 wl_event_loop_add_idle(wm->server->loop,
1129 weston_wm_window_draw_decoration,
1130 window);
1131}
1132
1133static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001134weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1135{
1136 xcb_property_notify_event_t *property_notify =
1137 (xcb_property_notify_event_t *) event;
1138 struct weston_wm_window *window;
1139
Derek Foreman49372142015-04-09 10:51:22 -05001140 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001141 return;
1142
1143 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001144
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001145 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001146 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001147 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001148 else
1149 read_and_dump_property(wm, property_notify->window,
1150 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001151
1152 if (property_notify->atom == wm->atom.net_wm_name ||
1153 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001154 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001155}
1156
1157static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001158weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001159 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001160{
1161 struct weston_wm_window *window;
1162 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001163 xcb_get_geometry_cookie_t geometry_cookie;
1164 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001165
Peter Huttererf3d62272013-08-08 11:57:05 +10001166 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001167 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001168 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001169 return;
1170 }
1171
MoD384a11a2013-06-22 11:04:21 -05001172 geometry_cookie = xcb_get_geometry(wm->conn, id);
1173
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001174 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1175 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1176
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001177 window->wm = wm;
1178 window->id = id;
1179 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001180 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001181 window->width = width;
1182 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001183 window->x = x;
1184 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001185
MoD384a11a2013-06-22 11:04:21 -05001186 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1187 /* technically we should use XRender and check the visual format's
1188 alpha_mask, but checking depth is simpler and works in all known cases */
1189 if(geometry_reply != NULL)
1190 window->has_alpha = geometry_reply->depth == 32;
1191 free(geometry_reply);
1192
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001193 hash_table_insert(wm->window_hash, id, window);
1194}
1195
1196static void
1197weston_wm_window_destroy(struct weston_wm_window *window)
1198{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001199 struct weston_wm *wm = window->wm;
1200
1201 if (window->repaint_source)
1202 wl_event_source_remove(window->repaint_source);
1203 if (window->cairo_surface)
1204 cairo_surface_destroy(window->cairo_surface);
1205
1206 if (window->frame_id) {
1207 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1208 xcb_destroy_window(wm->conn, window->frame_id);
1209 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001210 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001211 hash_table_remove(wm->window_hash, window->frame_id);
1212 window->frame_id = XCB_WINDOW_NONE;
1213 }
1214
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001215 if (window->surface_id)
1216 wl_list_remove(&window->link);
1217
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001218 if (window->surface)
1219 wl_list_remove(&window->surface_destroy_listener.link);
1220
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001221 hash_table_remove(window->wm->window_hash, window->id);
1222 free(window);
1223}
1224
1225static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001226weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1227{
1228 xcb_create_notify_event_t *create_notify =
1229 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001230
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001231 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1232 create_notify->window,
1233 create_notify->width, create_notify->height,
1234 create_notify->override_redirect ? ", override" : "",
1235 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001236
1237 if (our_resource(wm, create_notify->window))
1238 return;
1239
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001240 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001241 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001242 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001243 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001244}
1245
1246static void
1247weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1248{
1249 xcb_destroy_notify_event_t *destroy_notify =
1250 (xcb_destroy_notify_event_t *) event;
1251 struct weston_wm_window *window;
1252
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001253 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1254 destroy_notify->window,
1255 destroy_notify->event,
1256 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001257
1258 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001259 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001260
Derek Foreman49372142015-04-09 10:51:22 -05001261 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1262 return;
1263
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001264 weston_wm_window_destroy(window);
1265}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001266
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001267static void
1268weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1269{
1270 xcb_reparent_notify_event_t *reparent_notify =
1271 (xcb_reparent_notify_event_t *) event;
1272 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001273
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001274 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1275 reparent_notify->window,
1276 reparent_notify->parent,
1277 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001278
1279 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001280 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001281 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001282 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001283 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001284 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1285 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001286 weston_wm_window_destroy(window);
1287 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001288}
1289
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001290struct weston_seat *
1291weston_wm_pick_seat(struct weston_wm *wm)
1292{
1293 return container_of(wm->server->compositor->seat_list.next,
1294 struct weston_seat, link);
1295}
1296
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001297static struct weston_seat *
1298weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1299{
1300 struct weston_wm *wm = window->wm;
1301 struct weston_seat *seat, *s;
1302
1303 seat = NULL;
1304 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001305 if (s->pointer != NULL && s->pointer->focus &&
1306 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001307 s->pointer->button_count > 0 &&
1308 (seat == NULL ||
1309 s->pointer->grab_serial -
1310 seat->pointer->grab_serial < (1 << 30)))
1311 seat = s;
1312 }
1313
1314 return seat;
1315}
1316
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001317static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001318weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1319 xcb_client_message_event_t *client_message)
1320{
1321 static const int map[] = {
1322 THEME_LOCATION_RESIZING_TOP_LEFT,
1323 THEME_LOCATION_RESIZING_TOP,
1324 THEME_LOCATION_RESIZING_TOP_RIGHT,
1325 THEME_LOCATION_RESIZING_RIGHT,
1326 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1327 THEME_LOCATION_RESIZING_BOTTOM,
1328 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1329 THEME_LOCATION_RESIZING_LEFT
1330 };
1331
1332 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001333 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001334 int detail;
1335 struct weston_shell_interface *shell_interface =
1336 &wm->server->compositor->shell_interface;
1337
Boyan Dingc06a1802014-07-06 11:44:58 +08001338 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001339 || !seat->pointer->focus
1340 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001341 return;
1342
1343 detail = client_message->data.data32[2];
1344 switch (detail) {
1345 case _NET_WM_MOVERESIZE_MOVE:
1346 shell_interface->move(window->shsurf, seat);
1347 break;
1348 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1349 case _NET_WM_MOVERESIZE_SIZE_TOP:
1350 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1351 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1352 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1353 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1354 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1355 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1356 shell_interface->resize(window->shsurf, seat, map[detail]);
1357 break;
1358 case _NET_WM_MOVERESIZE_CANCEL:
1359 break;
1360 }
1361}
1362
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001363#define _NET_WM_STATE_REMOVE 0
1364#define _NET_WM_STATE_ADD 1
1365#define _NET_WM_STATE_TOGGLE 2
1366
1367static int
1368update_state(int action, int *state)
1369{
1370 int new_state, changed;
1371
1372 switch (action) {
1373 case _NET_WM_STATE_REMOVE:
1374 new_state = 0;
1375 break;
1376 case _NET_WM_STATE_ADD:
1377 new_state = 1;
1378 break;
1379 case _NET_WM_STATE_TOGGLE:
1380 new_state = !*state;
1381 break;
1382 default:
1383 return 0;
1384 }
1385
1386 changed = (*state != new_state);
1387 *state = new_state;
1388
1389 return changed;
1390}
1391
1392static void
1393weston_wm_window_configure(void *data);
1394
1395static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001396weston_wm_window_set_toplevel(struct weston_wm_window *window)
1397{
1398 struct weston_shell_interface *shell_interface =
1399 &window->wm->server->compositor->shell_interface;
1400
1401 shell_interface->set_toplevel(window->shsurf);
1402 window->width = window->saved_width;
1403 window->height = window->saved_height;
1404 if (window->frame)
1405 frame_resize_inside(window->frame,
1406 window->width,
1407 window->height);
1408 weston_wm_window_configure(window);
1409}
1410
1411static inline bool
1412weston_wm_window_is_maximized(struct weston_wm_window *window)
1413{
1414 return window->maximized_horz && window->maximized_vert;
1415}
1416
1417static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001418weston_wm_window_handle_state(struct weston_wm_window *window,
1419 xcb_client_message_event_t *client_message)
1420{
1421 struct weston_wm *wm = window->wm;
1422 struct weston_shell_interface *shell_interface =
1423 &wm->server->compositor->shell_interface;
1424 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001425 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001426
1427 action = client_message->data.data32[0];
1428 property = client_message->data.data32[1];
1429
1430 if (property == wm->atom.net_wm_state_fullscreen &&
1431 update_state(action, &window->fullscreen)) {
1432 weston_wm_window_set_net_wm_state(window);
1433 if (window->fullscreen) {
1434 window->saved_width = window->width;
1435 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001436
1437 if (window->shsurf)
1438 shell_interface->set_fullscreen(window->shsurf,
1439 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1440 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001441 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001442 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001443 weston_wm_window_set_toplevel(window);
1444 }
1445 } else {
1446 if (property == wm->atom.net_wm_state_maximized_vert &&
1447 update_state(action, &window->maximized_vert))
1448 weston_wm_window_set_net_wm_state(window);
1449 if (property == wm->atom.net_wm_state_maximized_horz &&
1450 update_state(action, &window->maximized_horz))
1451 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001452
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001453 if (maximized != weston_wm_window_is_maximized(window)) {
1454 if (weston_wm_window_is_maximized(window)) {
1455 window->saved_width = window->width;
1456 window->saved_height = window->height;
1457
1458 if (window->shsurf)
1459 shell_interface->set_maximized(window->shsurf);
1460 } else if (window->shsurf) {
1461 weston_wm_window_set_toplevel(window);
1462 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001463 }
1464 }
1465}
1466
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001467static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001468surface_destroy(struct wl_listener *listener, void *data)
1469{
1470 struct weston_wm_window *window =
1471 container_of(listener,
1472 struct weston_wm_window, surface_destroy_listener);
1473
1474 wm_log("surface for xid %d destroyed\n", window->id);
1475
1476 /* This should have been freed by the shell.
1477 * Don't try to use it later. */
1478 window->shsurf = NULL;
1479 window->surface = NULL;
1480 window->view = NULL;
1481}
1482
1483static void
1484weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1485 xcb_client_message_event_t *client_message)
1486{
1487 struct weston_wm *wm = window->wm;
1488 struct wl_resource *resource;
1489
1490 if (window->surface_id != 0) {
1491 wm_log("already have surface id for window %d\n", window->id);
1492 return;
1493 }
1494
1495 /* Xwayland will send the wayland requests to create the
1496 * wl_surface before sending this client message. Even so, we
1497 * can end up handling the X event before the wayland requests
1498 * and thus when we try to look up the surface ID, the surface
1499 * hasn't been created yet. In that case put the window on
1500 * the unpaired window list and continue when the surface gets
1501 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001502 uint32_t id = client_message->data.data32[0];
1503 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001504 if (resource) {
1505 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001506 xserver_map_shell_surface(window,
1507 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001508 }
1509 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001510 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001511 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001512 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001513}
1514
1515static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001516weston_wm_handle_client_message(struct weston_wm *wm,
1517 xcb_generic_event_t *event)
1518{
1519 xcb_client_message_event_t *client_message =
1520 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001521 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001522
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001523 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1524 get_atom_name(wm->conn, client_message->type),
1525 client_message->data.data32[0],
1526 client_message->data.data32[1],
1527 client_message->data.data32[2],
1528 client_message->data.data32[3],
1529 client_message->data.data32[4],
1530 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001531
Jason Ekstrand0250a742014-07-24 13:17:47 -07001532 /* The window may get created and destroyed before we actually
1533 * handle the message. If it doesn't exist, bail.
1534 */
Derek Foreman49372142015-04-09 10:51:22 -05001535 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001536 return;
1537
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001538 if (client_message->type == wm->atom.net_wm_moveresize)
1539 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001540 else if (client_message->type == wm->atom.net_wm_state)
1541 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001542 else if (client_message->type == wm->atom.wl_surface_id)
1543 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001544}
1545
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001546enum cursor_type {
1547 XWM_CURSOR_TOP,
1548 XWM_CURSOR_BOTTOM,
1549 XWM_CURSOR_LEFT,
1550 XWM_CURSOR_RIGHT,
1551 XWM_CURSOR_TOP_LEFT,
1552 XWM_CURSOR_TOP_RIGHT,
1553 XWM_CURSOR_BOTTOM_LEFT,
1554 XWM_CURSOR_BOTTOM_RIGHT,
1555 XWM_CURSOR_LEFT_PTR,
1556};
1557
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001558/*
1559 * The following correspondences between file names and cursors was copied
1560 * from: https://bugs.kde.org/attachment.cgi?id=67313
1561 */
1562
1563static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001564 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001565 "sw-resize",
1566 "size_bdiag"
1567};
1568
1569static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001570 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001571 "se-resize",
1572 "size_fdiag"
1573};
1574
1575static const char *bottom_sides[] = {
1576 "bottom_side",
1577 "s-resize",
1578 "size_ver"
1579};
1580
1581static const char *left_ptrs[] = {
1582 "left_ptr",
1583 "default",
1584 "top_left_arrow",
1585 "left-arrow"
1586};
1587
1588static const char *left_sides[] = {
1589 "left_side",
1590 "w-resize",
1591 "size_hor"
1592};
1593
1594static const char *right_sides[] = {
1595 "right_side",
1596 "e-resize",
1597 "size_hor"
1598};
1599
1600static const char *top_left_corners[] = {
1601 "top_left_corner",
1602 "nw-resize",
1603 "size_fdiag"
1604};
1605
1606static const char *top_right_corners[] = {
1607 "top_right_corner",
1608 "ne-resize",
1609 "size_bdiag"
1610};
1611
1612static const char *top_sides[] = {
1613 "top_side",
1614 "n-resize",
1615 "size_ver"
1616};
1617
1618struct cursor_alternatives {
1619 const char **names;
1620 size_t count;
1621};
1622
1623static const struct cursor_alternatives cursors[] = {
1624 {top_sides, ARRAY_LENGTH(top_sides)},
1625 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1626 {left_sides, ARRAY_LENGTH(left_sides)},
1627 {right_sides, ARRAY_LENGTH(right_sides)},
1628 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1629 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1630 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1631 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1632 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001633};
1634
1635static void
1636weston_wm_create_cursors(struct weston_wm *wm)
1637{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001638 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001639 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001640 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001641
1642 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1643 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001644 for (j = 0; j < cursors[i].count; j++) {
1645 name = cursors[i].names[j];
1646 wm->cursors[i] =
1647 xcb_cursor_library_load_cursor(wm, name);
1648 if (wm->cursors[i] != (xcb_cursor_t)-1)
1649 break;
1650 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001651 }
1652
1653 wm->last_cursor = -1;
1654}
1655
1656static void
1657weston_wm_destroy_cursors(struct weston_wm *wm)
1658{
1659 uint8_t i;
1660
1661 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1662 xcb_free_cursor(wm->conn, wm->cursors[i]);
1663
1664 free(wm->cursors);
1665}
1666
1667static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001668get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001669{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001670 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001671
1672 switch (location) {
1673 case THEME_LOCATION_RESIZING_TOP:
1674 return XWM_CURSOR_TOP;
1675 case THEME_LOCATION_RESIZING_BOTTOM:
1676 return XWM_CURSOR_BOTTOM;
1677 case THEME_LOCATION_RESIZING_LEFT:
1678 return XWM_CURSOR_LEFT;
1679 case THEME_LOCATION_RESIZING_RIGHT:
1680 return XWM_CURSOR_RIGHT;
1681 case THEME_LOCATION_RESIZING_TOP_LEFT:
1682 return XWM_CURSOR_TOP_LEFT;
1683 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1684 return XWM_CURSOR_TOP_RIGHT;
1685 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1686 return XWM_CURSOR_BOTTOM_LEFT;
1687 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1688 return XWM_CURSOR_BOTTOM_RIGHT;
1689 case THEME_LOCATION_EXTERIOR:
1690 case THEME_LOCATION_TITLEBAR:
1691 default:
1692 return XWM_CURSOR_LEFT_PTR;
1693 }
1694}
1695
1696static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001697weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1698 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001699{
1700 uint32_t cursor_value_list;
1701
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001702 if (wm->last_cursor == cursor)
1703 return;
1704
1705 wm->last_cursor = cursor;
1706
1707 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001708 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001709 XCB_CW_CURSOR, &cursor_value_list);
1710 xcb_flush(wm->conn);
1711}
1712
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001713static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001714weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001715{
1716 xcb_client_message_event_t client_message;
1717
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001718 if (window->delete_window) {
1719 client_message.response_type = XCB_CLIENT_MESSAGE;
1720 client_message.format = 32;
1721 client_message.window = window->id;
1722 client_message.type = window->wm->atom.wm_protocols;
1723 client_message.data.data32[0] =
1724 window->wm->atom.wm_delete_window;
1725 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001726
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001727 xcb_send_event(window->wm->conn, 0, window->id,
1728 XCB_EVENT_MASK_NO_EVENT,
1729 (char *) &client_message);
1730 } else {
1731 xcb_kill_client(window->wm->conn, window->id);
1732 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001733}
1734
1735static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001736weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1737{
1738 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1739 struct weston_shell_interface *shell_interface =
1740 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001741 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001742 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001743 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001744 enum frame_button_state button_state;
1745 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001746
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001747 wm_log("XCB_BUTTON_%s (detail %d)\n",
1748 button->response_type == XCB_BUTTON_PRESS ?
1749 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001750
Derek Foreman49372142015-04-09 10:51:22 -05001751 if (!wm_lookup_window(wm, button->event, &window) ||
1752 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001753 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001754
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001755 if (button->detail != 1 && button->detail != 2)
1756 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001757
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001758 seat = weston_wm_pick_seat_for_window(window);
1759
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001760 button_state = button->response_type == XCB_BUTTON_PRESS ?
1761 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1762 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1763
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001764 /* Make sure we're looking at the right location. The frame
1765 * could have received a motion event from a pointer from a
1766 * different wl_seat, but under X it looks like our core
1767 * pointer moved. Move the frame pointer to the button press
1768 * location before deciding what to do. */
1769 location = frame_pointer_motion(window->frame, NULL,
1770 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001771 location = frame_pointer_button(window->frame, NULL,
1772 button_id, button_state);
1773 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1774 weston_wm_window_schedule_repaint(window);
1775
1776 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001777 if (seat != NULL)
1778 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001779 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1780 }
1781
1782 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001783 if (seat != NULL)
1784 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001785 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1786 }
1787
1788 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001789 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001790 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001791 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001792
1793 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1794 window->maximized_horz = !window->maximized_horz;
1795 window->maximized_vert = !window->maximized_vert;
1796 if (weston_wm_window_is_maximized(window)) {
1797 window->saved_width = window->width;
1798 window->saved_height = window->height;
1799 shell_interface->set_maximized(window->shsurf);
1800 } else {
1801 weston_wm_window_set_toplevel(window);
1802 }
1803 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1804 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001805}
1806
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001807static void
1808weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1809{
1810 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1811 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001812 enum theme_location location;
1813 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001814
Derek Foreman49372142015-04-09 10:51:22 -05001815 if (!wm_lookup_window(wm, motion->event, &window) ||
1816 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001817 return;
1818
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001819 location = frame_pointer_motion(window->frame, NULL,
1820 motion->event_x, motion->event_y);
1821 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1822 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001823
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001824 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001825 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001826}
1827
1828static void
1829weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1830{
1831 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1832 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001833 enum theme_location location;
1834 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001835
Derek Foreman49372142015-04-09 10:51:22 -05001836 if (!wm_lookup_window(wm, enter->event, &window) ||
1837 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001838 return;
1839
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001840 location = frame_pointer_enter(window->frame, NULL,
1841 enter->event_x, enter->event_y);
1842 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1843 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001844
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001845 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001846 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001847}
1848
1849static void
1850weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1851{
1852 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1853 struct weston_wm_window *window;
1854
Derek Foreman49372142015-04-09 10:51:22 -05001855 if (!wm_lookup_window(wm, leave->event, &window) ||
1856 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001857 return;
1858
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001859 frame_pointer_leave(window->frame, NULL);
1860 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1861 weston_wm_window_schedule_repaint(window);
1862
Tiago Vignattic1903232012-07-16 12:15:37 -04001863 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001864}
1865
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001866static int
1867weston_wm_handle_event(int fd, uint32_t mask, void *data)
1868{
1869 struct weston_wm *wm = data;
1870 xcb_generic_event_t *event;
1871 int count = 0;
1872
1873 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1874 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001875 free(event);
1876 count++;
1877 continue;
1878 }
1879
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001880 if (weston_wm_handle_dnd_event(wm, event)) {
1881 free(event);
1882 count++;
1883 continue;
1884 }
1885
MoD31700122013-06-11 19:58:55 -05001886 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001887 case XCB_BUTTON_PRESS:
1888 case XCB_BUTTON_RELEASE:
1889 weston_wm_handle_button(wm, event);
1890 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001891 case XCB_ENTER_NOTIFY:
1892 weston_wm_handle_enter(wm, event);
1893 break;
1894 case XCB_LEAVE_NOTIFY:
1895 weston_wm_handle_leave(wm, event);
1896 break;
1897 case XCB_MOTION_NOTIFY:
1898 weston_wm_handle_motion(wm, event);
1899 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001900 case XCB_CREATE_NOTIFY:
1901 weston_wm_handle_create_notify(wm, event);
1902 break;
1903 case XCB_MAP_REQUEST:
1904 weston_wm_handle_map_request(wm, event);
1905 break;
1906 case XCB_MAP_NOTIFY:
1907 weston_wm_handle_map_notify(wm, event);
1908 break;
1909 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001910 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001911 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001912 case XCB_REPARENT_NOTIFY:
1913 weston_wm_handle_reparent_notify(wm, event);
1914 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001915 case XCB_CONFIGURE_REQUEST:
1916 weston_wm_handle_configure_request(wm, event);
1917 break;
1918 case XCB_CONFIGURE_NOTIFY:
1919 weston_wm_handle_configure_notify(wm, event);
1920 break;
1921 case XCB_DESTROY_NOTIFY:
1922 weston_wm_handle_destroy_notify(wm, event);
1923 break;
1924 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001925 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001926 break;
1927 case XCB_PROPERTY_NOTIFY:
1928 weston_wm_handle_property_notify(wm, event);
1929 break;
1930 case XCB_CLIENT_MESSAGE:
1931 weston_wm_handle_client_message(wm, event);
1932 break;
1933 }
1934
1935 free(event);
1936 count++;
1937 }
1938
1939 xcb_flush(wm->conn);
1940
1941 return count;
1942}
1943
1944static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001945weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1946{
1947 xcb_depth_iterator_t d_iter;
1948 xcb_visualtype_iterator_t vt_iter;
1949 xcb_visualtype_t *visualtype;
1950
1951 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1952 visualtype = NULL;
1953 while (d_iter.rem > 0) {
1954 if (d_iter.data->depth == 32) {
1955 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1956 visualtype = vt_iter.data;
1957 break;
1958 }
1959
1960 xcb_depth_next(&d_iter);
1961 }
1962
1963 if (visualtype == NULL) {
1964 weston_log("no 32 bit visualtype\n");
1965 return;
1966 }
1967
1968 wm->visual_id = visualtype->visual_id;
1969 wm->colormap = xcb_generate_id(wm->conn);
1970 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1971 wm->colormap, wm->screen->root, wm->visual_id);
1972}
1973
1974static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001975weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001976{
1977
1978#define F(field) offsetof(struct weston_wm, field)
1979
1980 static const struct { const char *name; int offset; } atoms[] = {
1981 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001982 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001983 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1984 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001985 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001986 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001987 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001988 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001989 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001990 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001991 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1992 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001993 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
1994 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001995 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1996 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1997 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001998 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001999 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2000
2001 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2002 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2003 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2004 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2005 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2006 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2007 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002008 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2009 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002010 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2011 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2012 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2013 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2014 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2015
2016 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2017 { "_NET_SUPPORTING_WM_CHECK",
2018 F(atom.net_supporting_wm_check) },
2019 { "_NET_SUPPORTED", F(atom.net_supported) },
2020 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2021 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002022 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002023 { "TARGETS", F(atom.targets) },
2024 { "UTF8_STRING", F(atom.utf8_string) },
2025 { "_WL_SELECTION", F(atom.wl_selection) },
2026 { "INCR", F(atom.incr) },
2027 { "TIMESTAMP", F(atom.timestamp) },
2028 { "MULTIPLE", F(atom.multiple) },
2029 { "UTF8_STRING" , F(atom.utf8_string) },
2030 { "COMPOUND_TEXT", F(atom.compound_text) },
2031 { "TEXT", F(atom.text) },
2032 { "STRING", F(atom.string) },
2033 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2034 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002035 { "XdndSelection", F(atom.xdnd_selection) },
2036 { "XdndAware", F(atom.xdnd_aware) },
2037 { "XdndEnter", F(atom.xdnd_enter) },
2038 { "XdndLeave", F(atom.xdnd_leave) },
2039 { "XdndDrop", F(atom.xdnd_drop) },
2040 { "XdndStatus", F(atom.xdnd_status) },
2041 { "XdndFinished", F(atom.xdnd_finished) },
2042 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002043 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2044 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002045 };
2046#undef F
2047
2048 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2049 xcb_xfixes_query_version_reply_t *xfixes_reply;
2050 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2051 xcb_intern_atom_reply_t *reply;
2052 xcb_render_query_pict_formats_reply_t *formats_reply;
2053 xcb_render_query_pict_formats_cookie_t formats_cookie;
2054 xcb_render_pictforminfo_t *formats;
2055 uint32_t i;
2056
2057 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002058 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002059
2060 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2061
2062 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2063 cookies[i] = xcb_intern_atom (wm->conn, 0,
2064 strlen(atoms[i].name),
2065 atoms[i].name);
2066
2067 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2068 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2069 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2070 free(reply);
2071 }
2072
2073 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2074 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002075 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002076
2077 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2078 XCB_XFIXES_MAJOR_VERSION,
2079 XCB_XFIXES_MINOR_VERSION);
2080 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2081 xfixes_cookie, NULL);
2082
Martin Minarik6d118362012-06-07 18:01:59 +02002083 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002084 xfixes_reply->major_version, xfixes_reply->minor_version);
2085
2086 free(xfixes_reply);
2087
2088 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2089 formats_cookie, 0);
2090 if (formats_reply == NULL)
2091 return;
2092
2093 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002094 for (i = 0; i < formats_reply->num_formats; i++) {
2095 if (formats[i].direct.red_mask != 0xff &&
2096 formats[i].direct.red_shift != 16)
2097 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002098 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2099 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002100 wm->format_rgb = formats[i];
2101 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2102 formats[i].depth == 32 &&
2103 formats[i].direct.alpha_mask == 0xff &&
2104 formats[i].direct.alpha_shift == 24)
2105 wm->format_rgba = formats[i];
2106 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002107
2108 free(formats_reply);
2109}
2110
2111static void
2112weston_wm_create_wm_window(struct weston_wm *wm)
2113{
2114 static const char name[] = "Weston WM";
2115
2116 wm->wm_window = xcb_generate_id(wm->conn);
2117 xcb_create_window(wm->conn,
2118 XCB_COPY_FROM_PARENT,
2119 wm->wm_window,
2120 wm->screen->root,
2121 0, 0,
2122 10, 10,
2123 0,
2124 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2125 wm->screen->root_visual,
2126 0, NULL);
2127
2128 xcb_change_property(wm->conn,
2129 XCB_PROP_MODE_REPLACE,
2130 wm->wm_window,
2131 wm->atom.net_supporting_wm_check,
2132 XCB_ATOM_WINDOW,
2133 32, /* format */
2134 1, &wm->wm_window);
2135
2136 xcb_change_property(wm->conn,
2137 XCB_PROP_MODE_REPLACE,
2138 wm->wm_window,
2139 wm->atom.net_wm_name,
2140 wm->atom.utf8_string,
2141 8, /* format */
2142 strlen(name), name);
2143
2144 xcb_change_property(wm->conn,
2145 XCB_PROP_MODE_REPLACE,
2146 wm->screen->root,
2147 wm->atom.net_supporting_wm_check,
2148 XCB_ATOM_WINDOW,
2149 32, /* format */
2150 1, &wm->wm_window);
2151
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002152 /* Claim the WM_S0 selection even though we don't suport
2153 * the --replace functionality. */
2154 xcb_set_selection_owner(wm->conn,
2155 wm->wm_window,
2156 wm->atom.wm_s0,
2157 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002158
2159 xcb_set_selection_owner(wm->conn,
2160 wm->wm_window,
2161 wm->atom.net_wm_cm_s0,
2162 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002163}
2164
2165struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002166weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002167{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002168 struct weston_wm *wm;
2169 struct wl_event_loop *loop;
2170 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002171 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002172 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002173
Peter Huttererf3d62272013-08-08 11:57:05 +10002174 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002175 if (wm == NULL)
2176 return NULL;
2177
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002178 wm->server = wxs;
2179 wm->window_hash = hash_table_create();
2180 if (wm->window_hash == NULL) {
2181 free(wm);
2182 return NULL;
2183 }
2184
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002185 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002186 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002187 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002188 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002189 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002190 hash_table_destroy(wm->window_hash);
2191 free(wm);
2192 return NULL;
2193 }
2194
2195 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2196 wm->screen = s.data;
2197
2198 loop = wl_display_get_event_loop(wxs->wl_display);
2199 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002200 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002201 WL_EVENT_READABLE,
2202 weston_wm_handle_event, wm);
2203 wl_event_source_check(wm->source);
2204
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002205 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002206 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002207
2208 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002209 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2210 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2211 XCB_EVENT_MASK_PROPERTY_CHANGE;
2212 xcb_change_window_attributes(wm->conn, wm->screen->root,
2213 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002214
2215 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2216 XCB_COMPOSITE_REDIRECT_MANUAL);
2217
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002218 wm->theme = theme_create();
2219
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002220 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002221 supported[1] = wm->atom.net_wm_state;
2222 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002223 supported[3] = wm->atom.net_wm_state_maximized_vert;
2224 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002225 xcb_change_property(wm->conn,
2226 XCB_PROP_MODE_REPLACE,
2227 wm->screen->root,
2228 wm->atom.net_supported,
2229 XCB_ATOM_ATOM,
2230 32, /* format */
2231 ARRAY_LENGTH(supported), supported);
2232
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002233 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002234
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002235 weston_wm_dnd_init(wm);
2236
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002237 xcb_flush(wm->conn);
2238
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002239 wm->create_surface_listener.notify = weston_wm_create_surface;
2240 wl_signal_add(&wxs->compositor->create_surface_signal,
2241 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002242 wm->activate_listener.notify = weston_wm_window_activate;
2243 wl_signal_add(&wxs->compositor->activate_signal,
2244 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002245 wm->transform_listener.notify = weston_wm_window_transform;
2246 wl_signal_add(&wxs->compositor->transform_signal,
2247 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002248 wm->kill_listener.notify = weston_wm_kill_client;
2249 wl_signal_add(&wxs->compositor->kill_signal,
2250 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002251 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002252
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002253 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002254 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002255
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002256 /* Create wm window and take WM_S0 selection last, which
2257 * signals to Xwayland that we're done with setup. */
2258 weston_wm_create_wm_window(wm);
2259
2260 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002261
2262 return wm;
2263}
2264
2265void
2266weston_wm_destroy(struct weston_wm *wm)
2267{
2268 /* FIXME: Free windows in hash. */
2269 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002270 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002271 xcb_disconnect(wm->conn);
2272 wl_event_source_remove(wm->source);
2273 wl_list_remove(&wm->selection_listener.link);
2274 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002275 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002276 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002277 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002278
2279 free(wm);
2280}
2281
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002282static struct weston_wm_window *
2283get_wm_window(struct weston_surface *surface)
2284{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002285 struct wl_listener *listener;
2286
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002287 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002288 if (listener)
2289 return container_of(listener, struct weston_wm_window,
2290 surface_destroy_listener);
2291
2292 return NULL;
2293}
2294
2295static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002296weston_wm_window_configure(void *data)
2297{
2298 struct weston_wm_window *window = data;
2299 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002300 uint32_t values[4];
2301 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002302
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002303 weston_wm_window_get_child_position(window, &x, &y);
2304 values[0] = x;
2305 values[1] = y;
2306 values[2] = window->width;
2307 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002308 xcb_configure_window(wm->conn,
2309 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002310 XCB_CONFIG_WINDOW_X |
2311 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002312 XCB_CONFIG_WINDOW_WIDTH |
2313 XCB_CONFIG_WINDOW_HEIGHT,
2314 values);
2315
2316 weston_wm_window_get_frame_size(window, &width, &height);
2317 values[0] = width;
2318 values[1] = height;
2319 xcb_configure_window(wm->conn,
2320 window->frame_id,
2321 XCB_CONFIG_WINDOW_WIDTH |
2322 XCB_CONFIG_WINDOW_HEIGHT,
2323 values);
2324
2325 window->configure_source = NULL;
2326
2327 weston_wm_window_schedule_repaint(window);
2328}
2329
2330static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002331send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002332{
2333 struct weston_wm_window *window = get_wm_window(surface);
2334 struct weston_wm *wm = window->wm;
2335 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002336 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002337
Marek Chalupae9fe4672014-10-29 13:44:44 +01002338 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002339 hborder = 2 * t->width;
2340 vborder = t->titlebar_height + t->width;
2341 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002342 hborder = 0;
2343 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002344 }
2345
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002346 if (width > hborder)
2347 window->width = width - hborder;
2348 else
2349 window->width = 1;
2350
2351 if (height > vborder)
2352 window->height = height - vborder;
2353 else
2354 window->height = 1;
2355
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002356 if (window->frame)
2357 frame_resize_inside(window->frame, window->width, window->height);
2358
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002359 if (window->configure_source)
2360 return;
2361
2362 window->configure_source =
2363 wl_event_loop_add_idle(wm->server->loop,
2364 weston_wm_window_configure, window);
2365}
2366
2367static const struct weston_shell_client shell_client = {
2368 send_configure
2369};
2370
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002371static int
2372legacy_fullscreen(struct weston_wm *wm,
2373 struct weston_wm_window *window,
2374 struct weston_output **output_ret)
2375{
2376 struct weston_compositor *compositor = wm->server->compositor;
2377 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002378 uint32_t minmax = PMinSize | PMaxSize;
2379 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002380
2381 /* Heuristics for detecting legacy fullscreen windows... */
2382
2383 wl_list_for_each(output, &compositor->output_list, link) {
2384 if (output->x == window->x &&
2385 output->y == window->y &&
2386 output->width == window->width &&
2387 output->height == window->height &&
2388 window->override_redirect) {
2389 *output_ret = output;
2390 return 1;
2391 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002392
2393 matching_size = 0;
2394 if ((window->size_hints.flags & (USSize |PSize)) &&
2395 window->size_hints.width == output->width &&
2396 window->size_hints.height == output->height)
2397 matching_size = 1;
2398 if ((window->size_hints.flags & minmax) == minmax &&
2399 window->size_hints.min_width == output->width &&
2400 window->size_hints.min_height == output->height &&
2401 window->size_hints.max_width == output->width &&
2402 window->size_hints.max_height == output->height)
2403 matching_size = 1;
2404
2405 if (matching_size && !window->decorate &&
2406 (window->size_hints.flags & (USPosition | PPosition)) &&
2407 window->size_hints.x == output->x &&
2408 window->size_hints.y == output->y) {
2409 *output_ret = output;
2410 return 1;
2411 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002412 }
2413
2414 return 0;
2415}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002416
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002417static bool
2418weston_wm_window_type_inactive(struct weston_wm_window *window)
2419{
2420 struct weston_wm *wm = window->wm;
2421
2422 return window->type == wm->atom.net_wm_window_type_tooltip ||
2423 window->type == wm->atom.net_wm_window_type_dropdown ||
2424 window->type == wm->atom.net_wm_window_type_dnd ||
2425 window->type == wm->atom.net_wm_window_type_combo ||
2426 window->type == wm->atom.net_wm_window_type_popup;
2427}
2428
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002429static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002430xserver_map_shell_surface(struct weston_wm_window *window,
2431 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002432{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002433 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002434 struct weston_shell_interface *shell_interface =
2435 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002436 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002437 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002438 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002439
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002440 weston_wm_window_read_properties(window);
2441
2442 /* A weston_wm_window may have many different surfaces assigned
2443 * throughout its life, so we must make sure to remove the listener
2444 * from the old surface signal list. */
2445 if (window->surface)
2446 wl_list_remove(&window->surface_destroy_listener.link);
2447
2448 window->surface = surface;
2449 window->surface_destroy_listener.notify = surface_destroy;
2450 wl_signal_add(&window->surface->destroy_signal,
2451 &window->surface_destroy_listener);
2452
2453 weston_wm_window_schedule_repaint(window);
2454
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002455 if (!shell_interface->create_shell_surface)
2456 return;
2457
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458 if (!shell_interface->get_primary_view)
2459 return;
2460
Pekka Paalanen50b67472014-10-01 15:02:41 +03002461 if (window->surface->configure) {
2462 weston_log("warning, unexpected in %s: "
2463 "surface's configure hook is already set.\n",
2464 __func__);
2465 return;
2466 }
2467
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002468 window->shsurf =
2469 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002470 window->surface,
2471 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472 window->view = shell_interface->get_primary_view(shell_interface->shell,
2473 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002474
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002475 if (window->name)
2476 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002477 if (window->pid > 0)
2478 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002479
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002480 if (window->fullscreen) {
2481 window->saved_width = window->width;
2482 window->saved_height = window->height;
2483 shell_interface->set_fullscreen(window->shsurf,
2484 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2485 0, NULL);
2486 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002487 } else if (legacy_fullscreen(wm, window, &output)) {
2488 window->fullscreen = 1;
2489 shell_interface->set_fullscreen(window->shsurf,
2490 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2491 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002492 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002493 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002494 window->x,
2495 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002496 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002497 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002498 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002499 if (weston_wm_window_type_inactive(window))
2500 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002501 shell_interface->set_transient(window->shsurf,
2502 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002503 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002504 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002505 } else if (weston_wm_window_is_maximized(window)) {
2506 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002507 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002508 if (weston_wm_window_type_inactive(window)) {
2509 shell_interface->set_xwayland(window->shsurf,
2510 window->x,
2511 window->y,
2512 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2513 } else {
2514 shell_interface->set_toplevel(window->shsurf);
2515 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002516 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002517}