blob: 9c500e13d578156805d8261253cb8e2dd059b106 [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
39#include "../../shared/cairo-util.h"
40#include "../compositor.h"
41#include "xserver-server-protocol.h"
42#include "hash.h"
43
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070044struct wm_size_hints {
45 uint32_t flags;
46 int32_t x, y;
47 int32_t width, height; /* should set so old wm's don't mess up */
48 int32_t min_width, min_height;
49 int32_t max_width, max_height;
50 int32_t width_inc, height_inc;
51 struct {
52 int32_t x;
53 int32_t y;
54 } min_aspect, max_aspect;
55 int32_t base_width, base_height;
56 int32_t win_gravity;
57};
58
59#define USPosition (1L << 0)
60#define USSize (1L << 1)
61#define PPosition (1L << 2)
62#define PSize (1L << 3)
63#define PMinSize (1L << 4)
64#define PMaxSize (1L << 5)
65#define PResizeInc (1L << 6)
66#define PAspect (1L << 7)
67#define PBaseSize (1L << 8)
68#define PWinGravity (1L << 9)
69
Kristian Høgsberg380deee2012-05-21 17:12:41 -040070struct motif_wm_hints {
71 uint32_t flags;
72 uint32_t functions;
73 uint32_t decorations;
74 int32_t input_mode;
75 uint32_t status;
76};
77
78#define MWM_HINTS_FUNCTIONS (1L << 0)
79#define MWM_HINTS_DECORATIONS (1L << 1)
80#define MWM_HINTS_INPUT_MODE (1L << 2)
81#define MWM_HINTS_STATUS (1L << 3)
82
83#define MWM_FUNC_ALL (1L << 0)
84#define MWM_FUNC_RESIZE (1L << 1)
85#define MWM_FUNC_MOVE (1L << 2)
86#define MWM_FUNC_MINIMIZE (1L << 3)
87#define MWM_FUNC_MAXIMIZE (1L << 4)
88#define MWM_FUNC_CLOSE (1L << 5)
89
90#define MWM_DECOR_ALL (1L << 0)
91#define MWM_DECOR_BORDER (1L << 1)
92#define MWM_DECOR_RESIZEH (1L << 2)
93#define MWM_DECOR_TITLE (1L << 3)
94#define MWM_DECOR_MENU (1L << 4)
95#define MWM_DECOR_MINIMIZE (1L << 5)
96#define MWM_DECOR_MAXIMIZE (1L << 6)
97
98#define MWM_INPUT_MODELESS 0
99#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
100#define MWM_INPUT_SYSTEM_MODAL 2
101#define MWM_INPUT_FULL_APPLICATION_MODAL 3
102#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
103
104#define MWM_TEAROFF_WINDOW (1L<<0)
105
106#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
107#define _NET_WM_MOVERESIZE_SIZE_TOP 1
108#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
109#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
110#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
111#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
112#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
113#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
114#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
115#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
116#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
117#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
118
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400119struct weston_wm_window {
120 struct weston_wm *wm;
121 xcb_window_t id;
122 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500123 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400124 cairo_surface_t *cairo_surface;
125 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;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700147 struct wm_size_hints size_hints;
148 struct motif_wm_hints motif_hints;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400149};
150
151static struct weston_wm_window *
152get_wm_window(struct weston_surface *surface);
153
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400154static void
155weston_wm_window_schedule_repaint(struct weston_wm_window *window);
156
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400157static int __attribute__ ((format (printf, 1, 2)))
158wm_log(const char *fmt, ...)
159{
160#ifdef WM_DEBUG
161 int l;
162 va_list argp;
163
164 va_start(argp, fmt);
165 l = weston_vlog(fmt, argp);
166 va_end(argp);
167
168 return l;
169#else
170 return 0;
171#endif
172}
173
174static int __attribute__ ((format (printf, 1, 2)))
175wm_log_continue(const char *fmt, ...)
176{
177#ifdef WM_DEBUG
178 int l;
179 va_list argp;
180
181 va_start(argp, fmt);
182 l = weston_vlog_continue(fmt, argp);
183 va_end(argp);
184
185 return l;
186#else
187 return 0;
188#endif
189}
190
191
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400192const char *
193get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
194{
195 xcb_get_atom_name_cookie_t cookie;
196 xcb_get_atom_name_reply_t *reply;
197 xcb_generic_error_t *e;
198 static char buffer[64];
199
200 if (atom == XCB_ATOM_NONE)
201 return "None";
202
203 cookie = xcb_get_atom_name (c, atom);
204 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500205
206 if(reply) {
207 snprintf(buffer, sizeof buffer, "%.*s",
208 xcb_get_atom_name_name_length (reply),
209 xcb_get_atom_name_name (reply));
210 } else {
211 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
212 }
213
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400214 free(reply);
215
216 return buffer;
217}
218
Tiago Vignatti90fada42012-07-16 12:02:08 -0400219static xcb_cursor_t
220xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
221{
222 xcb_connection_t *c = wm->conn;
223 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
224 xcb_screen_t *screen = s.data;
225 xcb_gcontext_t gc;
226 xcb_pixmap_t pix;
227 xcb_render_picture_t pic;
228 xcb_cursor_t cursor;
229 int stride = img->width * 4;
230
231 pix = xcb_generate_id(c);
232 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
233
234 pic = xcb_generate_id(c);
235 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
236
237 gc = xcb_generate_id(c);
238 xcb_create_gc(c, gc, pix, 0, 0);
239
240 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
241 img->width, img->height, 0, 0, 0, 32,
242 stride * img->height, (uint8_t *) img->pixels);
243 xcb_free_gc(c, gc);
244
245 cursor = xcb_generate_id(c);
246 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
247
248 xcb_render_free_picture(c, pic);
249 xcb_free_pixmap(c, pix);
250
251 return cursor;
252}
253
254static xcb_cursor_t
255xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
256{
257 /* TODO: treat animated cursors as well */
258 if (images->nimage != 1)
259 return -1;
260
261 return xcb_cursor_image_load_cursor(wm, images->images[0]);
262}
263
264static xcb_cursor_t
265xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
266{
267 xcb_cursor_t cursor;
268 XcursorImages *images;
269 char *v = NULL;
270 int size = 0;
271
272 if (!file)
273 return 0;
274
275 v = getenv ("XCURSOR_SIZE");
276 if (v)
277 size = atoi(v);
278
279 if (!size)
280 size = 32;
281
282 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300283 if (!images)
284 return -1;
285
Tiago Vignatti90fada42012-07-16 12:02:08 -0400286 cursor = xcb_cursor_images_load_cursor (wm, images);
287 XcursorImagesDestroy (images);
288
289 return cursor;
290}
291
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400292void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400293dump_property(struct weston_wm *wm,
294 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400295{
296 int32_t *incr_value;
297 const char *text_value, *name;
298 xcb_atom_t *atom_value;
299 int width, len;
300 uint32_t i;
301
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400302 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400303 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400304 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400305 return;
306 }
307
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400308 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
309 get_atom_name(wm->conn, reply->type),
310 reply->format,
311 xcb_get_property_value_length(reply),
312 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400313
314 if (reply->type == wm->atom.incr) {
315 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400316 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400317 } else if (reply->type == wm->atom.utf8_string ||
318 reply->type == wm->atom.string) {
319 text_value = xcb_get_property_value(reply);
320 if (reply->value_len > 40)
321 len = 40;
322 else
323 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400324 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400325 } else if (reply->type == XCB_ATOM_ATOM) {
326 atom_value = xcb_get_property_value(reply);
327 for (i = 0; i < reply->value_len; i++) {
328 name = get_atom_name(wm->conn, atom_value[i]);
329 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400330 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400331 width = 4;
332 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400333 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400334 }
335
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400336 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400337 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400338 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400339 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400340 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400341 }
342}
343
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200344static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400345read_and_dump_property(struct weston_wm *wm,
346 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400347{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400348 xcb_get_property_reply_t *reply;
349 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400350
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400351 cookie = xcb_get_property(wm->conn, 0, window,
352 property, XCB_ATOM_ANY, 0, 2048);
353 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400355 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400356
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400357 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400358}
359
360/* We reuse some predefined, but otherwise useles atoms */
361#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
362#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500363#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700364#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400365
366static void
367weston_wm_window_read_properties(struct weston_wm_window *window)
368{
369 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200370 struct weston_shell_interface *shell_interface =
371 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400372
373#define F(field) offsetof(struct weston_wm_window, field)
374 const struct {
375 xcb_atom_t atom;
376 xcb_atom_t type;
377 int offset;
378 } props[] = {
379 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
380 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
381 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
382 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700383 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500384 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
386 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300387 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400388 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300389 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400390 };
391#undef F
392
393 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
394 xcb_get_property_reply_t *reply;
395 void *p;
396 uint32_t *xid;
397 xcb_atom_t *atom;
398 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399
400 if (!window->properties_dirty)
401 return;
402 window->properties_dirty = 0;
403
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400404 for (i = 0; i < ARRAY_LENGTH(props); i++)
405 cookie[i] = xcb_get_property(wm->conn,
406 0, /* delete */
407 window->id,
408 props[i].atom,
409 XCB_ATOM_ANY, 0, 2048);
410
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300411 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700412 window->size_hints.flags = 0;
413 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700414 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700415
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400416 for (i = 0; i < ARRAY_LENGTH(props); i++) {
417 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
418 if (!reply)
419 /* Bad window, typically */
420 continue;
421 if (reply->type == XCB_ATOM_NONE) {
422 /* No such property */
423 free(reply);
424 continue;
425 }
426
427 p = ((char *) window + props[i].offset);
428
429 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300430 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400431 case XCB_ATOM_STRING:
432 /* FIXME: We're using this for both string and
433 utf8_string */
434 if (*(char **) p)
435 free(*(char **) p);
436
437 *(char **) p =
438 strndup(xcb_get_property_value(reply),
439 xcb_get_property_value_length(reply));
440 break;
441 case XCB_ATOM_WINDOW:
442 xid = xcb_get_property_value(reply);
443 *(struct weston_wm_window **) p =
444 hash_table_lookup(wm->window_hash, *xid);
445 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300446 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400447 case XCB_ATOM_ATOM:
448 atom = xcb_get_property_value(reply);
449 *(xcb_atom_t *) p = *atom;
450 break;
451 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700452 atom = xcb_get_property_value(reply);
453 for (i = 0; i < reply->value_len; i++)
454 if (atom[i] == wm->atom.wm_delete_window)
455 window->delete_window = 1;
456 break;
457
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400458 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700459 case TYPE_WM_NORMAL_HINTS:
460 memcpy(&window->size_hints,
461 xcb_get_property_value(reply),
462 sizeof window->size_hints);
463 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500464 case TYPE_NET_WM_STATE:
465 window->fullscreen = 0;
466 atom = xcb_get_property_value(reply);
467 for (i = 0; i < reply->value_len; i++)
468 if (atom[i] == wm->atom.net_wm_state_fullscreen)
469 window->fullscreen = 1;
470 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400471 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700472 memcpy(&window->motif_hints,
473 xcb_get_property_value(reply),
474 sizeof window->motif_hints);
475 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
476 window->decorate =
477 window->motif_hints.decorations > 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400478 break;
479 default:
480 break;
481 }
482 free(reply);
483 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200484
485 if (window->shsurf && window->name)
486 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500487 if (window->frame && window->name)
488 frame_set_title(window->frame, window->name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400489}
490
491static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400492weston_wm_window_get_frame_size(struct weston_wm_window *window,
493 int *width, int *height)
494{
495 struct theme *t = window->wm->theme;
496
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500497 if (window->fullscreen) {
498 *width = window->width;
499 *height = window->height;
500 } else if (window->decorate) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500501 *width = frame_width(window->frame);
502 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400503 } else {
504 *width = window->width + t->margin * 2;
505 *height = window->height + t->margin * 2;
506 }
507}
508
509static void
510weston_wm_window_get_child_position(struct weston_wm_window *window,
511 int *x, int *y)
512{
513 struct theme *t = window->wm->theme;
514
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500515 if (window->fullscreen) {
516 *x = 0;
517 *y = 0;
518 } else if (window->decorate) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500519 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400520 } else {
521 *x = t->margin;
522 *y = t->margin;
523 }
524}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400525
526static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500527weston_wm_window_send_configure_notify(struct weston_wm_window *window)
528{
529 xcb_configure_notify_event_t configure_notify;
530 struct weston_wm *wm = window->wm;
531 int x, y;
532
533 weston_wm_window_get_child_position(window, &x, &y);
534 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
535 configure_notify.pad0 = 0;
536 configure_notify.event = window->id;
537 configure_notify.window = window->id;
538 configure_notify.above_sibling = XCB_WINDOW_NONE;
539 configure_notify.x = x;
540 configure_notify.y = y;
541 configure_notify.width = window->width;
542 configure_notify.height = window->height;
543 configure_notify.border_width = 0;
544 configure_notify.override_redirect = 0;
545 configure_notify.pad1 = 0;
546
547 xcb_send_event(wm->conn, 0, window->id,
548 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
549 (char *) &configure_notify);
550}
551
552static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400553weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
554{
555 xcb_configure_request_event_t *configure_request =
556 (xcb_configure_request_event_t *) event;
557 struct weston_wm_window *window;
558 uint32_t mask, values[16];
559 int x, y, width, height, i = 0;
560
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400561 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
562 configure_request->window,
563 configure_request->x, configure_request->y,
564 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400565
566 window = hash_table_lookup(wm->window_hash, configure_request->window);
567
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500568 if (window->fullscreen) {
569 weston_wm_window_send_configure_notify(window);
570 return;
571 }
572
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400573 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
574 window->width = configure_request->width;
575 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
576 window->height = configure_request->height;
577
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500578 if (window->frame)
579 frame_resize_inside(window->frame, window->width, window->height);
580
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400581 weston_wm_window_get_child_position(window, &x, &y);
582 values[i++] = x;
583 values[i++] = y;
584 values[i++] = window->width;
585 values[i++] = window->height;
586 values[i++] = 0;
587 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
588 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
589 XCB_CONFIG_WINDOW_BORDER_WIDTH;
590 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
591 values[i++] = configure_request->sibling;
592 mask |= XCB_CONFIG_WINDOW_SIBLING;
593 }
594 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
595 values[i++] = configure_request->stack_mode;
596 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
597 }
598
599 xcb_configure_window(wm->conn, window->id, mask, values);
600
601 weston_wm_window_get_frame_size(window, &width, &height);
602 values[0] = width;
603 values[1] = height;
604 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
605 xcb_configure_window(wm->conn, window->frame_id, mask, values);
606
607 weston_wm_window_schedule_repaint(window);
608}
609
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400610static int
611our_resource(struct weston_wm *wm, uint32_t id)
612{
613 const xcb_setup_t *setup;
614
615 setup = xcb_get_setup(wm->conn);
616
617 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
618}
619
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400620static void
621weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
622{
623 xcb_configure_notify_event_t *configure_notify =
624 (xcb_configure_notify_event_t *) event;
625 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400626
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400627 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400628 configure_notify->window,
629 configure_notify->x, configure_notify->y,
630 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300631
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400632 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700633 window->x = configure_notify->x;
634 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700635 if (window->override_redirect) {
636 window->width = configure_notify->width;
637 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500638 if (window->frame)
639 frame_resize_inside(window->frame,
640 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700641 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400642}
643
644static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300645weston_wm_kill_client(struct wl_listener *listener, void *data)
646{
647 struct weston_surface *surface = data;
648 struct weston_wm_window *window = get_wm_window(surface);
649 char name[1024];
650
651 if (!window)
652 return;
653
654 gethostname(name, 1024);
655
656 /* this is only one heuristic to guess the PID of a client is valid,
657 * assuming it's compliant with icccm and ewmh. Non-compliants and
658 * remote applications of course fail. */
659 if (!strcmp(window->machine, name) && window->pid != 0)
660 kill(window->pid, SIGKILL);
661}
662
663static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400664weston_wm_window_activate(struct wl_listener *listener, void *data)
665{
666 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200667 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600668 struct weston_wm *wm =
669 container_of(listener, struct weston_wm, activate_listener);
670 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400671
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200672 if (surface) {
673 window = get_wm_window(surface);
674 }
675
Scott Moreau85ecac02012-05-21 15:49:14 -0600676 if (window) {
677 client_message.response_type = XCB_CLIENT_MESSAGE;
678 client_message.format = 32;
679 client_message.window = window->id;
680 client_message.type = wm->atom.wm_protocols;
681 client_message.data.data32[0] = wm->atom.wm_take_focus;
682 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
683
684 xcb_send_event(wm->conn, 0, window->id,
685 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
686 (char *) &client_message);
687
688 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
689 window->id, XCB_TIME_CURRENT_TIME);
690 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400691 xcb_set_input_focus (wm->conn,
692 XCB_INPUT_FOCUS_POINTER_ROOT,
693 XCB_NONE,
694 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600695 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400696
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500697 if (wm->focus_window) {
698 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400699 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500700 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400701 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500702 if (wm->focus_window) {
703 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400704 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500705 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400706}
707
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300708static void
709weston_wm_window_transform(struct wl_listener *listener, void *data)
710{
711 struct weston_surface *surface = data;
712 struct weston_wm_window *window = get_wm_window(surface);
713 struct weston_wm *wm =
714 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300715 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300716
717 if (!window || !wm)
718 return;
719
Jason Ekstranda7af7042013-10-12 22:38:11 -0500720 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300721 return;
722
Jason Ekstranda7af7042013-10-12 22:38:11 -0500723 if (window->x != window->view->geometry.x ||
724 window->y != window->view->geometry.y) {
725 values[0] = window->view->geometry.x;
726 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700727 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300728
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700729 xcb_configure_window(wm->conn, window->frame_id, mask, values);
730 xcb_flush(wm->conn);
731 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300732}
733
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400734#define ICCCM_WITHDRAWN_STATE 0
735#define ICCCM_NORMAL_STATE 1
736#define ICCCM_ICONIC_STATE 3
737
738static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500739weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400740{
741 struct weston_wm *wm = window->wm;
742 uint32_t property[2];
743
744 property[0] = state;
745 property[1] = XCB_WINDOW_NONE;
746
747 xcb_change_property(wm->conn,
748 XCB_PROP_MODE_REPLACE,
749 window->id,
750 wm->atom.wm_state,
751 wm->atom.wm_state,
752 32, /* format */
753 2, property);
754}
755
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400756static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500757weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
758{
759 struct weston_wm *wm = window->wm;
760 uint32_t property[1];
761 int i;
762
763 i = 0;
764 if (window->fullscreen)
765 property[i++] = wm->atom.net_wm_state_fullscreen;
766
767 xcb_change_property(wm->conn,
768 XCB_PROP_MODE_REPLACE,
769 window->id,
770 wm->atom.net_wm_state,
771 XCB_ATOM_ATOM,
772 32, /* format */
773 i, property);
774}
775
776static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700777weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400778{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700779 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400780 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400781 int x, y, width, height;
782
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500783 window->frame = frame_create(window->wm->theme,
784 window->width, window->height,
785 FRAME_BUTTON_CLOSE, window->name);
786 frame_resize_inside(window->frame, window->width, window->height);
787
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400788 weston_wm_window_get_frame_size(window, &width, &height);
789 weston_wm_window_get_child_position(window, &x, &y);
790
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400791 values[0] = wm->screen->black_pixel;
792 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400793 XCB_EVENT_MASK_KEY_PRESS |
794 XCB_EVENT_MASK_KEY_RELEASE |
795 XCB_EVENT_MASK_BUTTON_PRESS |
796 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400797 XCB_EVENT_MASK_POINTER_MOTION |
798 XCB_EVENT_MASK_ENTER_WINDOW |
799 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400800 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400801 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400802 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400803
804 window->frame_id = xcb_generate_id(wm->conn);
805 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400806 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400807 window->frame_id,
808 wm->screen->root,
809 0, 0,
810 width, height,
811 0,
812 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400813 wm->visual_id,
814 XCB_CW_BORDER_PIXEL |
815 XCB_CW_EVENT_MASK |
816 XCB_CW_COLORMAP, values);
817
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400818 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
819
820 values[0] = 0;
821 xcb_configure_window(wm->conn, window->id,
822 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
823
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400824 window->cairo_surface =
825 cairo_xcb_surface_create_with_xrender_format(wm->conn,
826 wm->screen,
827 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400828 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400829 width, height);
830
831 hash_table_insert(wm->window_hash, window->frame_id, window);
832}
833
834static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700835weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
836{
837 xcb_map_request_event_t *map_request =
838 (xcb_map_request_event_t *) event;
839 struct weston_wm_window *window;
840
841 if (our_resource(wm, map_request->window)) {
842 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
843 map_request->window);
844 return;
845 }
846
847 window = hash_table_lookup(wm->window_hash, map_request->window);
848
849 weston_wm_window_read_properties(window);
850
851 if (window->frame_id == XCB_WINDOW_NONE)
852 weston_wm_window_create_frame(window);
853
854 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
855 window->id, window, window->frame_id);
856
857 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
858 weston_wm_window_set_net_wm_state(window);
859
860 xcb_map_window(wm->conn, map_request->window);
861 xcb_map_window(wm->conn, window->frame_id);
862}
863
864static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
866{
867 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
868
869 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400870 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
871 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400872 return;
873 }
874
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400875 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400876}
877
878static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400879weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
880{
881 xcb_unmap_notify_event_t *unmap_notify =
882 (xcb_unmap_notify_event_t *) event;
883 struct weston_wm_window *window;
884
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400885 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
886 unmap_notify->window,
887 unmap_notify->event,
888 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400889
890 if (our_resource(wm, unmap_notify->window))
891 return;
892
MoD31700122013-06-11 19:58:55 -0500893 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400894 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
895 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400896 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400897
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400898 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400899 if (wm->focus_window == window)
900 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400901 if (window->surface)
902 wl_list_remove(&window->surface_destroy_listener.link);
903 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200904 window->shsurf = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700905 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400906}
907
908static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400909weston_wm_window_draw_decoration(void *data)
910{
911 struct weston_wm_window *window = data;
912 struct weston_wm *wm = window->wm;
913 struct theme *t = wm->theme;
914 cairo_t *cr;
915 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500916 int32_t input_x, input_y, input_w, input_h;
917
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400918 uint32_t flags = 0;
919
920 weston_wm_window_read_properties(window);
921
922 window->repaint_source = NULL;
923
924 weston_wm_window_get_frame_size(window, &width, &height);
925 weston_wm_window_get_child_position(window, &x, &y);
926
927 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
928 cr = cairo_create(window->cairo_surface);
929
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500930 if (window->fullscreen) {
931 /* nothing */
932 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400933 if (wm->focus_window == window)
934 flags |= THEME_FRAME_ACTIVE;
935
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500936 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400937 } else {
938 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
939 cairo_set_source_rgba(cr, 0, 0, 0, 0);
940 cairo_paint(cr);
941
942 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
943 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
944 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
945 }
946
947 cairo_destroy(cr);
948
949 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700950 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500951 if(window->has_alpha) {
952 pixman_region32_init(&window->surface->pending.opaque);
953 } else {
954 /* We leave an extra pixel around the X window area to
955 * make sure we don't sample from the undefined alpha
956 * channel when filtering. */
957 pixman_region32_init_rect(&window->surface->pending.opaque,
958 x - 1, y - 1,
959 window->width + 2,
960 window->height + 2);
961 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962 if (window->view)
963 weston_view_geometry_dirty(window->view);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500964 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400965
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500966 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500967 pixman_region32_fini(&window->surface->pending.input);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500968 frame_input_rect(window->frame, &input_x, &input_y,
969 &input_w, &input_h);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500970 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500971 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400972 }
973}
974
975static void
976weston_wm_window_schedule_repaint(struct weston_wm_window *window)
977{
978 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300979 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400980
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400981 if (window->frame_id == XCB_WINDOW_NONE) {
982 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300983 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700984 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500985 if(window->has_alpha) {
986 pixman_region32_init(&window->surface->pending.opaque);
987 } else {
988 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
989 width, height);
990 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 if (window->view)
992 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400993 }
994 return;
995 }
996
997 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400998 return;
999
1000 window->repaint_source =
1001 wl_event_loop_add_idle(wm->server->loop,
1002 weston_wm_window_draw_decoration,
1003 window);
1004}
1005
1006static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001007weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1008{
1009 xcb_property_notify_event_t *property_notify =
1010 (xcb_property_notify_event_t *) event;
1011 struct weston_wm_window *window;
1012
1013 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001014 if (!window)
1015 return;
1016
1017 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001018
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001019 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001020 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001021 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001022 else
1023 read_and_dump_property(wm, property_notify->window,
1024 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001025
1026 if (property_notify->atom == wm->atom.net_wm_name ||
1027 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001028 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001029}
1030
1031static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001032weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001033 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001034{
1035 struct weston_wm_window *window;
1036 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001037 xcb_get_geometry_cookie_t geometry_cookie;
1038 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001039
Peter Huttererf3d62272013-08-08 11:57:05 +10001040 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001041 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001042 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001043 return;
1044 }
1045
MoD384a11a2013-06-22 11:04:21 -05001046 geometry_cookie = xcb_get_geometry(wm->conn, id);
1047
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001048 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1049 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1050
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001051 window->wm = wm;
1052 window->id = id;
1053 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001054 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001055 window->width = width;
1056 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001057 window->x = x;
1058 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001059
MoD384a11a2013-06-22 11:04:21 -05001060 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1061 /* technically we should use XRender and check the visual format's
1062 alpha_mask, but checking depth is simpler and works in all known cases */
1063 if(geometry_reply != NULL)
1064 window->has_alpha = geometry_reply->depth == 32;
1065 free(geometry_reply);
1066
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001067 hash_table_insert(wm->window_hash, id, window);
1068}
1069
1070static void
1071weston_wm_window_destroy(struct weston_wm_window *window)
1072{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001073 struct weston_wm *wm = window->wm;
1074
1075 if (window->repaint_source)
1076 wl_event_source_remove(window->repaint_source);
1077 if (window->cairo_surface)
1078 cairo_surface_destroy(window->cairo_surface);
1079
1080 if (window->frame_id) {
1081 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1082 xcb_destroy_window(wm->conn, window->frame_id);
1083 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1084 hash_table_remove(wm->window_hash, window->frame_id);
1085 window->frame_id = XCB_WINDOW_NONE;
1086 }
1087
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001088 hash_table_remove(window->wm->window_hash, window->id);
1089 free(window);
1090}
1091
1092static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001093weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1094{
1095 xcb_create_notify_event_t *create_notify =
1096 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001097
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001098 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1099 create_notify->window,
1100 create_notify->width, create_notify->height,
1101 create_notify->override_redirect ? ", override" : "",
1102 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001103
1104 if (our_resource(wm, create_notify->window))
1105 return;
1106
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001107 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001108 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001109 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001110 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001111}
1112
1113static void
1114weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1115{
1116 xcb_destroy_notify_event_t *destroy_notify =
1117 (xcb_destroy_notify_event_t *) event;
1118 struct weston_wm_window *window;
1119
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001120 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1121 destroy_notify->window,
1122 destroy_notify->event,
1123 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001124
1125 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001126 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001127
1128 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001129 weston_wm_window_destroy(window);
1130}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001131
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001132static void
1133weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1134{
1135 xcb_reparent_notify_event_t *reparent_notify =
1136 (xcb_reparent_notify_event_t *) event;
1137 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001138
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001139 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1140 reparent_notify->window,
1141 reparent_notify->parent,
1142 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001143
1144 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001145 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001146 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001147 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001148 } else if (!our_resource(wm, reparent_notify->parent)) {
1149 window = hash_table_lookup(wm->window_hash,
1150 reparent_notify->window);
1151 weston_wm_window_destroy(window);
1152 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001153}
1154
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001155struct weston_seat *
1156weston_wm_pick_seat(struct weston_wm *wm)
1157{
1158 return container_of(wm->server->compositor->seat_list.next,
1159 struct weston_seat, link);
1160}
1161
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001162static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001163weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1164 xcb_client_message_event_t *client_message)
1165{
1166 static const int map[] = {
1167 THEME_LOCATION_RESIZING_TOP_LEFT,
1168 THEME_LOCATION_RESIZING_TOP,
1169 THEME_LOCATION_RESIZING_TOP_RIGHT,
1170 THEME_LOCATION_RESIZING_RIGHT,
1171 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1172 THEME_LOCATION_RESIZING_BOTTOM,
1173 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1174 THEME_LOCATION_RESIZING_LEFT
1175 };
1176
1177 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001178 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001179 int detail;
1180 struct weston_shell_interface *shell_interface =
1181 &wm->server->compositor->shell_interface;
1182
Jason Ekstranda7af7042013-10-12 22:38:11 -05001183 if (seat->pointer->button_count != 1 || !window->view
1184 || seat->pointer->focus != window->view)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001185 return;
1186
1187 detail = client_message->data.data32[2];
1188 switch (detail) {
1189 case _NET_WM_MOVERESIZE_MOVE:
1190 shell_interface->move(window->shsurf, seat);
1191 break;
1192 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1193 case _NET_WM_MOVERESIZE_SIZE_TOP:
1194 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1195 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1196 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1197 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1198 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1199 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1200 shell_interface->resize(window->shsurf, seat, map[detail]);
1201 break;
1202 case _NET_WM_MOVERESIZE_CANCEL:
1203 break;
1204 }
1205}
1206
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001207#define _NET_WM_STATE_REMOVE 0
1208#define _NET_WM_STATE_ADD 1
1209#define _NET_WM_STATE_TOGGLE 2
1210
1211static int
1212update_state(int action, int *state)
1213{
1214 int new_state, changed;
1215
1216 switch (action) {
1217 case _NET_WM_STATE_REMOVE:
1218 new_state = 0;
1219 break;
1220 case _NET_WM_STATE_ADD:
1221 new_state = 1;
1222 break;
1223 case _NET_WM_STATE_TOGGLE:
1224 new_state = !*state;
1225 break;
1226 default:
1227 return 0;
1228 }
1229
1230 changed = (*state != new_state);
1231 *state = new_state;
1232
1233 return changed;
1234}
1235
1236static void
1237weston_wm_window_configure(void *data);
1238
1239static void
1240weston_wm_window_handle_state(struct weston_wm_window *window,
1241 xcb_client_message_event_t *client_message)
1242{
1243 struct weston_wm *wm = window->wm;
1244 struct weston_shell_interface *shell_interface =
1245 &wm->server->compositor->shell_interface;
1246 uint32_t action, property;
1247
1248 action = client_message->data.data32[0];
1249 property = client_message->data.data32[1];
1250
1251 if (property == wm->atom.net_wm_state_fullscreen &&
1252 update_state(action, &window->fullscreen)) {
1253 weston_wm_window_set_net_wm_state(window);
1254 if (window->fullscreen) {
1255 window->saved_width = window->width;
1256 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001257
1258 if (window->shsurf)
1259 shell_interface->set_fullscreen(window->shsurf,
1260 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1261 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001262 } else {
1263 shell_interface->set_toplevel(window->shsurf);
1264 window->width = window->saved_width;
1265 window->height = window->saved_height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001266 if (window->frame)
1267 frame_resize_inside(window->frame,
1268 window->width,
1269 window->height);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001270 weston_wm_window_configure(window);
1271 }
1272 }
1273}
1274
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001275static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001276weston_wm_handle_client_message(struct weston_wm *wm,
1277 xcb_generic_event_t *event)
1278{
1279 xcb_client_message_event_t *client_message =
1280 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001281 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001282
1283 window = hash_table_lookup(wm->window_hash, client_message->window);
1284
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001285 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1286 get_atom_name(wm->conn, client_message->type),
1287 client_message->data.data32[0],
1288 client_message->data.data32[1],
1289 client_message->data.data32[2],
1290 client_message->data.data32[3],
1291 client_message->data.data32[4],
1292 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001293
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001294 if (client_message->type == wm->atom.net_wm_moveresize)
1295 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001296 else if (client_message->type == wm->atom.net_wm_state)
1297 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001298}
1299
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001300enum cursor_type {
1301 XWM_CURSOR_TOP,
1302 XWM_CURSOR_BOTTOM,
1303 XWM_CURSOR_LEFT,
1304 XWM_CURSOR_RIGHT,
1305 XWM_CURSOR_TOP_LEFT,
1306 XWM_CURSOR_TOP_RIGHT,
1307 XWM_CURSOR_BOTTOM_LEFT,
1308 XWM_CURSOR_BOTTOM_RIGHT,
1309 XWM_CURSOR_LEFT_PTR,
1310};
1311
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001312/*
1313 * The following correspondences between file names and cursors was copied
1314 * from: https://bugs.kde.org/attachment.cgi?id=67313
1315 */
1316
1317static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001318 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001319 "sw-resize",
1320 "size_bdiag"
1321};
1322
1323static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001324 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001325 "se-resize",
1326 "size_fdiag"
1327};
1328
1329static const char *bottom_sides[] = {
1330 "bottom_side",
1331 "s-resize",
1332 "size_ver"
1333};
1334
1335static const char *left_ptrs[] = {
1336 "left_ptr",
1337 "default",
1338 "top_left_arrow",
1339 "left-arrow"
1340};
1341
1342static const char *left_sides[] = {
1343 "left_side",
1344 "w-resize",
1345 "size_hor"
1346};
1347
1348static const char *right_sides[] = {
1349 "right_side",
1350 "e-resize",
1351 "size_hor"
1352};
1353
1354static const char *top_left_corners[] = {
1355 "top_left_corner",
1356 "nw-resize",
1357 "size_fdiag"
1358};
1359
1360static const char *top_right_corners[] = {
1361 "top_right_corner",
1362 "ne-resize",
1363 "size_bdiag"
1364};
1365
1366static const char *top_sides[] = {
1367 "top_side",
1368 "n-resize",
1369 "size_ver"
1370};
1371
1372struct cursor_alternatives {
1373 const char **names;
1374 size_t count;
1375};
1376
1377static const struct cursor_alternatives cursors[] = {
1378 {top_sides, ARRAY_LENGTH(top_sides)},
1379 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1380 {left_sides, ARRAY_LENGTH(left_sides)},
1381 {right_sides, ARRAY_LENGTH(right_sides)},
1382 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1383 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1384 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1385 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1386 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001387};
1388
1389static void
1390weston_wm_create_cursors(struct weston_wm *wm)
1391{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001392 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001393 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001394 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001395
1396 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1397 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001398 for (j = 0; j < cursors[i].count; j++) {
1399 name = cursors[i].names[j];
1400 wm->cursors[i] =
1401 xcb_cursor_library_load_cursor(wm, name);
1402 if (wm->cursors[i] != (xcb_cursor_t)-1)
1403 break;
1404 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001405 }
1406
1407 wm->last_cursor = -1;
1408}
1409
1410static void
1411weston_wm_destroy_cursors(struct weston_wm *wm)
1412{
1413 uint8_t i;
1414
1415 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1416 xcb_free_cursor(wm->conn, wm->cursors[i]);
1417
1418 free(wm->cursors);
1419}
1420
1421static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001422get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001423{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001424 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001425
1426 switch (location) {
1427 case THEME_LOCATION_RESIZING_TOP:
1428 return XWM_CURSOR_TOP;
1429 case THEME_LOCATION_RESIZING_BOTTOM:
1430 return XWM_CURSOR_BOTTOM;
1431 case THEME_LOCATION_RESIZING_LEFT:
1432 return XWM_CURSOR_LEFT;
1433 case THEME_LOCATION_RESIZING_RIGHT:
1434 return XWM_CURSOR_RIGHT;
1435 case THEME_LOCATION_RESIZING_TOP_LEFT:
1436 return XWM_CURSOR_TOP_LEFT;
1437 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1438 return XWM_CURSOR_TOP_RIGHT;
1439 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1440 return XWM_CURSOR_BOTTOM_LEFT;
1441 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1442 return XWM_CURSOR_BOTTOM_RIGHT;
1443 case THEME_LOCATION_EXTERIOR:
1444 case THEME_LOCATION_TITLEBAR:
1445 default:
1446 return XWM_CURSOR_LEFT_PTR;
1447 }
1448}
1449
1450static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001451weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1452 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001453{
1454 uint32_t cursor_value_list;
1455
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001456 if (wm->last_cursor == cursor)
1457 return;
1458
1459 wm->last_cursor = cursor;
1460
1461 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001462 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001463 XCB_CW_CURSOR, &cursor_value_list);
1464 xcb_flush(wm->conn);
1465}
1466
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001467static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001468weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001469{
1470 xcb_client_message_event_t client_message;
1471
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001472 if (window->delete_window) {
1473 client_message.response_type = XCB_CLIENT_MESSAGE;
1474 client_message.format = 32;
1475 client_message.window = window->id;
1476 client_message.type = window->wm->atom.wm_protocols;
1477 client_message.data.data32[0] =
1478 window->wm->atom.wm_delete_window;
1479 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001480
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001481 xcb_send_event(window->wm->conn, 0, window->id,
1482 XCB_EVENT_MASK_NO_EVENT,
1483 (char *) &client_message);
1484 } else {
1485 xcb_kill_client(window->wm->conn, window->id);
1486 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001487}
1488
1489static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001490weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1491{
1492 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1493 struct weston_shell_interface *shell_interface =
1494 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001495 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001496 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001497 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001498 enum frame_button_state button_state;
1499 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001500
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001501 wm_log("XCB_BUTTON_%s (detail %d)\n",
1502 button->response_type == XCB_BUTTON_PRESS ?
1503 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001504
1505 window = hash_table_lookup(wm->window_hash, button->event);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001506 if (!window || !window->decorate)
1507 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001508
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001509 if (button->detail != 1 && button->detail != 2)
1510 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001511
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001512 button_state = button->response_type == XCB_BUTTON_PRESS ?
1513 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1514 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1515
1516 location = frame_pointer_button(window->frame, NULL,
1517 button_id, button_state);
1518 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1519 weston_wm_window_schedule_repaint(window);
1520
1521 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
1522 shell_interface->move(window->shsurf, seat);
1523 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1524 }
1525
1526 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
1527 shell_interface->resize(window->shsurf, seat, location);
1528 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1529 }
1530
1531 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001532 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001533 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001535}
1536
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001537static void
1538weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1539{
1540 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1541 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001542 enum theme_location location;
1543 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001544
1545 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001546 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001547 return;
1548
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001549 location = frame_pointer_motion(window->frame, NULL,
1550 motion->event_x, motion->event_y);
1551 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1552 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001553
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001554 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001555 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001556}
1557
1558static void
1559weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1560{
1561 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1562 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001563 enum theme_location location;
1564 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001565
1566 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001567 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001568 return;
1569
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001570 location = frame_pointer_enter(window->frame, NULL,
1571 enter->event_x, enter->event_y);
1572 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1573 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001574
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001575 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001576 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001577}
1578
1579static void
1580weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1581{
1582 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1583 struct weston_wm_window *window;
1584
1585 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001586 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001587 return;
1588
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001589 frame_pointer_leave(window->frame, NULL);
1590 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1591 weston_wm_window_schedule_repaint(window);
1592
Tiago Vignattic1903232012-07-16 12:15:37 -04001593 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001594}
1595
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001596static int
1597weston_wm_handle_event(int fd, uint32_t mask, void *data)
1598{
1599 struct weston_wm *wm = data;
1600 xcb_generic_event_t *event;
1601 int count = 0;
1602
1603 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1604 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001605 free(event);
1606 count++;
1607 continue;
1608 }
1609
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001610 if (weston_wm_handle_dnd_event(wm, event)) {
1611 free(event);
1612 count++;
1613 continue;
1614 }
1615
MoD31700122013-06-11 19:58:55 -05001616 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001617 case XCB_BUTTON_PRESS:
1618 case XCB_BUTTON_RELEASE:
1619 weston_wm_handle_button(wm, event);
1620 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001621 case XCB_ENTER_NOTIFY:
1622 weston_wm_handle_enter(wm, event);
1623 break;
1624 case XCB_LEAVE_NOTIFY:
1625 weston_wm_handle_leave(wm, event);
1626 break;
1627 case XCB_MOTION_NOTIFY:
1628 weston_wm_handle_motion(wm, event);
1629 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001630 case XCB_CREATE_NOTIFY:
1631 weston_wm_handle_create_notify(wm, event);
1632 break;
1633 case XCB_MAP_REQUEST:
1634 weston_wm_handle_map_request(wm, event);
1635 break;
1636 case XCB_MAP_NOTIFY:
1637 weston_wm_handle_map_notify(wm, event);
1638 break;
1639 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001640 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001641 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001642 case XCB_REPARENT_NOTIFY:
1643 weston_wm_handle_reparent_notify(wm, event);
1644 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001645 case XCB_CONFIGURE_REQUEST:
1646 weston_wm_handle_configure_request(wm, event);
1647 break;
1648 case XCB_CONFIGURE_NOTIFY:
1649 weston_wm_handle_configure_notify(wm, event);
1650 break;
1651 case XCB_DESTROY_NOTIFY:
1652 weston_wm_handle_destroy_notify(wm, event);
1653 break;
1654 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001655 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001656 break;
1657 case XCB_PROPERTY_NOTIFY:
1658 weston_wm_handle_property_notify(wm, event);
1659 break;
1660 case XCB_CLIENT_MESSAGE:
1661 weston_wm_handle_client_message(wm, event);
1662 break;
1663 }
1664
1665 free(event);
1666 count++;
1667 }
1668
1669 xcb_flush(wm->conn);
1670
1671 return count;
1672}
1673
1674static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001675weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1676{
1677 xcb_depth_iterator_t d_iter;
1678 xcb_visualtype_iterator_t vt_iter;
1679 xcb_visualtype_t *visualtype;
1680
1681 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1682 visualtype = NULL;
1683 while (d_iter.rem > 0) {
1684 if (d_iter.data->depth == 32) {
1685 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1686 visualtype = vt_iter.data;
1687 break;
1688 }
1689
1690 xcb_depth_next(&d_iter);
1691 }
1692
1693 if (visualtype == NULL) {
1694 weston_log("no 32 bit visualtype\n");
1695 return;
1696 }
1697
1698 wm->visual_id = visualtype->visual_id;
1699 wm->colormap = xcb_generate_id(wm->conn);
1700 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1701 wm->colormap, wm->screen->root, wm->visual_id);
1702}
1703
1704static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001705weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001706{
1707
1708#define F(field) offsetof(struct weston_wm, field)
1709
1710 static const struct { const char *name; int offset; } atoms[] = {
1711 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001712 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001713 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1714 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001715 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001716 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001717 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001718 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001719 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001720 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001721 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1722 { "_NET_WM_STATE", F(atom.net_wm_state) },
1723 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1724 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1725 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1726 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1727
1728 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1729 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1730 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1731 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1732 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1733 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1734 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001735 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1736 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001737 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1738 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1739 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1740 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1741 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1742
1743 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1744 { "_NET_SUPPORTING_WM_CHECK",
1745 F(atom.net_supporting_wm_check) },
1746 { "_NET_SUPPORTED", F(atom.net_supported) },
1747 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1748 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001749 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001750 { "TARGETS", F(atom.targets) },
1751 { "UTF8_STRING", F(atom.utf8_string) },
1752 { "_WL_SELECTION", F(atom.wl_selection) },
1753 { "INCR", F(atom.incr) },
1754 { "TIMESTAMP", F(atom.timestamp) },
1755 { "MULTIPLE", F(atom.multiple) },
1756 { "UTF8_STRING" , F(atom.utf8_string) },
1757 { "COMPOUND_TEXT", F(atom.compound_text) },
1758 { "TEXT", F(atom.text) },
1759 { "STRING", F(atom.string) },
1760 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1761 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001762 { "XdndSelection", F(atom.xdnd_selection) },
1763 { "XdndAware", F(atom.xdnd_aware) },
1764 { "XdndEnter", F(atom.xdnd_enter) },
1765 { "XdndLeave", F(atom.xdnd_leave) },
1766 { "XdndDrop", F(atom.xdnd_drop) },
1767 { "XdndStatus", F(atom.xdnd_status) },
1768 { "XdndFinished", F(atom.xdnd_finished) },
1769 { "XdndTypeList", F(atom.xdnd_type_list) },
1770 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001771 };
1772#undef F
1773
1774 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1775 xcb_xfixes_query_version_reply_t *xfixes_reply;
1776 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1777 xcb_intern_atom_reply_t *reply;
1778 xcb_render_query_pict_formats_reply_t *formats_reply;
1779 xcb_render_query_pict_formats_cookie_t formats_cookie;
1780 xcb_render_pictforminfo_t *formats;
1781 uint32_t i;
1782
1783 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07001784 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001785
1786 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1787
1788 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1789 cookies[i] = xcb_intern_atom (wm->conn, 0,
1790 strlen(atoms[i].name),
1791 atoms[i].name);
1792
1793 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1794 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1795 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1796 free(reply);
1797 }
1798
1799 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1800 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001801 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001802
1803 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1804 XCB_XFIXES_MAJOR_VERSION,
1805 XCB_XFIXES_MINOR_VERSION);
1806 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1807 xfixes_cookie, NULL);
1808
Martin Minarik6d118362012-06-07 18:01:59 +02001809 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001810 xfixes_reply->major_version, xfixes_reply->minor_version);
1811
1812 free(xfixes_reply);
1813
1814 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1815 formats_cookie, 0);
1816 if (formats_reply == NULL)
1817 return;
1818
1819 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001820 for (i = 0; i < formats_reply->num_formats; i++) {
1821 if (formats[i].direct.red_mask != 0xff &&
1822 formats[i].direct.red_shift != 16)
1823 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001824 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1825 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001826 wm->format_rgb = formats[i];
1827 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1828 formats[i].depth == 32 &&
1829 formats[i].direct.alpha_mask == 0xff &&
1830 formats[i].direct.alpha_shift == 24)
1831 wm->format_rgba = formats[i];
1832 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001833
1834 free(formats_reply);
1835}
1836
1837static void
1838weston_wm_create_wm_window(struct weston_wm *wm)
1839{
1840 static const char name[] = "Weston WM";
1841
1842 wm->wm_window = xcb_generate_id(wm->conn);
1843 xcb_create_window(wm->conn,
1844 XCB_COPY_FROM_PARENT,
1845 wm->wm_window,
1846 wm->screen->root,
1847 0, 0,
1848 10, 10,
1849 0,
1850 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1851 wm->screen->root_visual,
1852 0, NULL);
1853
1854 xcb_change_property(wm->conn,
1855 XCB_PROP_MODE_REPLACE,
1856 wm->wm_window,
1857 wm->atom.net_supporting_wm_check,
1858 XCB_ATOM_WINDOW,
1859 32, /* format */
1860 1, &wm->wm_window);
1861
1862 xcb_change_property(wm->conn,
1863 XCB_PROP_MODE_REPLACE,
1864 wm->wm_window,
1865 wm->atom.net_wm_name,
1866 wm->atom.utf8_string,
1867 8, /* format */
1868 strlen(name), name);
1869
1870 xcb_change_property(wm->conn,
1871 XCB_PROP_MODE_REPLACE,
1872 wm->screen->root,
1873 wm->atom.net_supporting_wm_check,
1874 XCB_ATOM_WINDOW,
1875 32, /* format */
1876 1, &wm->wm_window);
1877
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001878 /* Claim the WM_S0 selection even though we don't suport
1879 * the --replace functionality. */
1880 xcb_set_selection_owner(wm->conn,
1881 wm->wm_window,
1882 wm->atom.wm_s0,
1883 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001884
1885 xcb_set_selection_owner(wm->conn,
1886 wm->wm_window,
1887 wm->atom.net_wm_cm_s0,
1888 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001889}
1890
1891struct weston_wm *
1892weston_wm_create(struct weston_xserver *wxs)
1893{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001894 struct weston_wm *wm;
1895 struct wl_event_loop *loop;
1896 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001897 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001898 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001899 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001900
Peter Huttererf3d62272013-08-08 11:57:05 +10001901 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001902 if (wm == NULL)
1903 return NULL;
1904
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001905 wm->server = wxs;
1906 wm->window_hash = hash_table_create();
1907 if (wm->window_hash == NULL) {
1908 free(wm);
1909 return NULL;
1910 }
1911
1912 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001913 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001914 hash_table_destroy(wm->window_hash);
1915 free(wm);
1916 return NULL;
1917 }
1918
1919 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001920 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001921 close(sv[1]);
1922
1923 /* xcb_connect_to_fd takes ownership of the fd. */
1924 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1925 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001926 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001927 close(sv[0]);
1928 hash_table_destroy(wm->window_hash);
1929 free(wm);
1930 return NULL;
1931 }
1932
1933 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1934 wm->screen = s.data;
1935
1936 loop = wl_display_get_event_loop(wxs->wl_display);
1937 wm->source =
1938 wl_event_loop_add_fd(loop, sv[0],
1939 WL_EVENT_READABLE,
1940 weston_wm_handle_event, wm);
1941 wl_event_source_check(wm->source);
1942
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001943 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001944 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001945
1946 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001947 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1948 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1949 XCB_EVENT_MASK_PROPERTY_CHANGE;
1950 xcb_change_window_attributes(wm->conn, wm->screen->root,
1951 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07001952
1953 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
1954 XCB_COMPOSITE_REDIRECT_MANUAL);
1955
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001956 wm->theme = theme_create();
1957
1958 weston_wm_create_wm_window(wm);
1959
1960 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001961 supported[1] = wm->atom.net_wm_state;
1962 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001963 xcb_change_property(wm->conn,
1964 XCB_PROP_MODE_REPLACE,
1965 wm->screen->root,
1966 wm->atom.net_supported,
1967 XCB_ATOM_ATOM,
1968 32, /* format */
1969 ARRAY_LENGTH(supported), supported);
1970
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001971 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001972
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001973 weston_wm_dnd_init(wm);
1974
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001975 xcb_flush(wm->conn);
1976
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001977 wm->activate_listener.notify = weston_wm_window_activate;
1978 wl_signal_add(&wxs->compositor->activate_signal,
1979 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001980 wm->transform_listener.notify = weston_wm_window_transform;
1981 wl_signal_add(&wxs->compositor->transform_signal,
1982 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001983 wm->kill_listener.notify = weston_wm_kill_client;
1984 wl_signal_add(&wxs->compositor->kill_signal,
1985 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001986
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001987 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001988 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001989
Martin Minarik6d118362012-06-07 18:01:59 +02001990 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001991
1992 return wm;
1993}
1994
1995void
1996weston_wm_destroy(struct weston_wm *wm)
1997{
1998 /* FIXME: Free windows in hash. */
1999 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002000 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002001 xcb_disconnect(wm->conn);
2002 wl_event_source_remove(wm->source);
2003 wl_list_remove(&wm->selection_listener.link);
2004 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002005 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002006 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002007
2008 free(wm);
2009}
2010
2011static void
2012surface_destroy(struct wl_listener *listener, void *data)
2013{
2014 struct weston_wm_window *window =
2015 container_of(listener,
2016 struct weston_wm_window, surface_destroy_listener);
2017
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002018 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07002019
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002020 /* This should have been freed by the shell.
2021 Don't try to use it later. */
2022 window->shsurf = NULL;
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07002023 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002024}
2025
2026static struct weston_wm_window *
2027get_wm_window(struct weston_surface *surface)
2028{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002029 struct wl_listener *listener;
2030
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002031 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002032 if (listener)
2033 return container_of(listener, struct weston_wm_window,
2034 surface_destroy_listener);
2035
2036 return NULL;
2037}
2038
2039static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002040weston_wm_window_configure(void *data)
2041{
2042 struct weston_wm_window *window = data;
2043 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002044 uint32_t values[4];
2045 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002046
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002047 weston_wm_window_get_child_position(window, &x, &y);
2048 values[0] = x;
2049 values[1] = y;
2050 values[2] = window->width;
2051 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002052 xcb_configure_window(wm->conn,
2053 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002054 XCB_CONFIG_WINDOW_X |
2055 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002056 XCB_CONFIG_WINDOW_WIDTH |
2057 XCB_CONFIG_WINDOW_HEIGHT,
2058 values);
2059
2060 weston_wm_window_get_frame_size(window, &width, &height);
2061 values[0] = width;
2062 values[1] = height;
2063 xcb_configure_window(wm->conn,
2064 window->frame_id,
2065 XCB_CONFIG_WINDOW_WIDTH |
2066 XCB_CONFIG_WINDOW_HEIGHT,
2067 values);
2068
2069 window->configure_source = NULL;
2070
2071 weston_wm_window_schedule_repaint(window);
2072}
2073
2074static void
2075send_configure(struct weston_surface *surface,
2076 uint32_t edges, int32_t width, int32_t height)
2077{
2078 struct weston_wm_window *window = get_wm_window(surface);
2079 struct weston_wm *wm = window->wm;
2080 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002081 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002082
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002083 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002084 hborder = 0;
2085 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002086 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002087 hborder = 2 * (t->margin + t->width);
2088 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002089 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002090 hborder = 2 * t->margin;
2091 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002092 }
2093
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002094 if (width > hborder)
2095 window->width = width - hborder;
2096 else
2097 window->width = 1;
2098
2099 if (height > vborder)
2100 window->height = height - vborder;
2101 else
2102 window->height = 1;
2103
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002104 if (window->frame)
2105 frame_resize_inside(window->frame, window->width, window->height);
2106
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002107 if (window->configure_source)
2108 return;
2109
2110 window->configure_source =
2111 wl_event_loop_add_idle(wm->server->loop,
2112 weston_wm_window_configure, window);
2113}
2114
2115static const struct weston_shell_client shell_client = {
2116 send_configure
2117};
2118
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002119static int
2120legacy_fullscreen(struct weston_wm *wm,
2121 struct weston_wm_window *window,
2122 struct weston_output **output_ret)
2123{
2124 struct weston_compositor *compositor = wm->server->compositor;
2125 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002126 uint32_t minmax = PMinSize | PMaxSize;
2127 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002128
2129 /* Heuristics for detecting legacy fullscreen windows... */
2130
2131 wl_list_for_each(output, &compositor->output_list, link) {
2132 if (output->x == window->x &&
2133 output->y == window->y &&
2134 output->width == window->width &&
2135 output->height == window->height &&
2136 window->override_redirect) {
2137 *output_ret = output;
2138 return 1;
2139 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002140
2141 matching_size = 0;
2142 if ((window->size_hints.flags & (USSize |PSize)) &&
2143 window->size_hints.width == output->width &&
2144 window->size_hints.height == output->height)
2145 matching_size = 1;
2146 if ((window->size_hints.flags & minmax) == minmax &&
2147 window->size_hints.min_width == output->width &&
2148 window->size_hints.min_height == output->height &&
2149 window->size_hints.max_width == output->width &&
2150 window->size_hints.max_height == output->height)
2151 matching_size = 1;
2152
2153 if (matching_size && !window->decorate &&
2154 (window->size_hints.flags & (USPosition | PPosition)) &&
2155 window->size_hints.x == output->x &&
2156 window->size_hints.y == output->y) {
2157 *output_ret = output;
2158 return 1;
2159 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002160 }
2161
2162 return 0;
2163}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002164static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002165xserver_map_shell_surface(struct weston_wm *wm,
2166 struct weston_wm_window *window)
2167{
2168 struct weston_shell_interface *shell_interface =
2169 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002170 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002171
2172 if (!shell_interface->create_shell_surface)
2173 return;
2174
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 if (!shell_interface->get_primary_view)
2176 return;
2177
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002178 window->shsurf =
2179 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002180 window->surface,
2181 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182 window->view = shell_interface->get_primary_view(shell_interface->shell,
2183 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002184
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002185 if (window->name)
2186 shell_interface->set_title(window->shsurf, window->name);
2187
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002188 if (window->fullscreen) {
2189 window->saved_width = window->width;
2190 window->saved_height = window->height;
2191 shell_interface->set_fullscreen(window->shsurf,
2192 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2193 0, NULL);
2194 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002195 } else if (legacy_fullscreen(wm, window, &output)) {
2196 window->fullscreen = 1;
2197 shell_interface->set_fullscreen(window->shsurf,
2198 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2199 0, output);
Giulio Camuffoca43f092013-09-11 17:49:13 +02002200 } else if (!window->override_redirect && !window->transient_for) {
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002201 shell_interface->set_toplevel(window->shsurf);
2202 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002203 } else {
2204 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002205 window->x,
2206 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002207 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002208 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002209}
2210
2211static void
2212xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2213 struct wl_resource *surface_resource, uint32_t id)
2214{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002215 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002216 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002217 struct weston_surface *surface =
2218 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219 struct weston_wm_window *window;
2220
2221 if (client != wxs->client)
2222 return;
2223
2224 window = hash_table_lookup(wm->window_hash, id);
2225 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002226 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002227 return;
2228 }
2229
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002230 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002231
2232 weston_wm_window_read_properties(window);
2233
Giulio Camuffo1cf329b2013-09-20 16:16:06 +02002234 /* A weston_wm_window may have many different surfaces assigned
2235 * throughout its life, so we must make sure to remove the listener
2236 * from the old surface signal list. */
2237 if (window->surface)
2238 wl_list_remove(&window->surface_destroy_listener.link);
2239
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002240 window->surface = (struct weston_surface *) surface;
2241 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002242 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002243 &window->surface_destroy_listener);
2244
2245 weston_wm_window_schedule_repaint(window);
2246 xserver_map_shell_surface(wm, window);
2247}
2248
2249const struct xserver_interface xserver_implementation = {
2250 xserver_set_window_id
2251};