blob: 7662a9b1a5ae568743f6033188e83a552526dbda [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;
127 struct wl_listener surface_destroy_listener;
128 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400129 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400130 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300131 int pid;
132 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400133 char *class;
134 char *name;
135 struct weston_wm_window *transient_for;
136 uint32_t protocols;
137 xcb_atom_t type;
138 int width, height;
139 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500140 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400141 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300142 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500143 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500144 int has_alpha;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700145 struct wm_size_hints size_hints;
146 struct motif_wm_hints motif_hints;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400147};
148
149static struct weston_wm_window *
150get_wm_window(struct weston_surface *surface);
151
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400152static void
153weston_wm_window_schedule_repaint(struct weston_wm_window *window);
154
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400155static int __attribute__ ((format (printf, 1, 2)))
156wm_log(const char *fmt, ...)
157{
158#ifdef WM_DEBUG
159 int l;
160 va_list argp;
161
162 va_start(argp, fmt);
163 l = weston_vlog(fmt, argp);
164 va_end(argp);
165
166 return l;
167#else
168 return 0;
169#endif
170}
171
172static int __attribute__ ((format (printf, 1, 2)))
173wm_log_continue(const char *fmt, ...)
174{
175#ifdef WM_DEBUG
176 int l;
177 va_list argp;
178
179 va_start(argp, fmt);
180 l = weston_vlog_continue(fmt, argp);
181 va_end(argp);
182
183 return l;
184#else
185 return 0;
186#endif
187}
188
189
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400190const char *
191get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
192{
193 xcb_get_atom_name_cookie_t cookie;
194 xcb_get_atom_name_reply_t *reply;
195 xcb_generic_error_t *e;
196 static char buffer[64];
197
198 if (atom == XCB_ATOM_NONE)
199 return "None";
200
201 cookie = xcb_get_atom_name (c, atom);
202 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500203
204 if(reply) {
205 snprintf(buffer, sizeof buffer, "%.*s",
206 xcb_get_atom_name_name_length (reply),
207 xcb_get_atom_name_name (reply));
208 } else {
209 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
210 }
211
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400212 free(reply);
213
214 return buffer;
215}
216
Tiago Vignatti90fada42012-07-16 12:02:08 -0400217static xcb_cursor_t
218xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
219{
220 xcb_connection_t *c = wm->conn;
221 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
222 xcb_screen_t *screen = s.data;
223 xcb_gcontext_t gc;
224 xcb_pixmap_t pix;
225 xcb_render_picture_t pic;
226 xcb_cursor_t cursor;
227 int stride = img->width * 4;
228
229 pix = xcb_generate_id(c);
230 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
231
232 pic = xcb_generate_id(c);
233 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
234
235 gc = xcb_generate_id(c);
236 xcb_create_gc(c, gc, pix, 0, 0);
237
238 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
239 img->width, img->height, 0, 0, 0, 32,
240 stride * img->height, (uint8_t *) img->pixels);
241 xcb_free_gc(c, gc);
242
243 cursor = xcb_generate_id(c);
244 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
245
246 xcb_render_free_picture(c, pic);
247 xcb_free_pixmap(c, pix);
248
249 return cursor;
250}
251
252static xcb_cursor_t
253xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
254{
255 /* TODO: treat animated cursors as well */
256 if (images->nimage != 1)
257 return -1;
258
259 return xcb_cursor_image_load_cursor(wm, images->images[0]);
260}
261
262static xcb_cursor_t
263xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
264{
265 xcb_cursor_t cursor;
266 XcursorImages *images;
267 char *v = NULL;
268 int size = 0;
269
270 if (!file)
271 return 0;
272
273 v = getenv ("XCURSOR_SIZE");
274 if (v)
275 size = atoi(v);
276
277 if (!size)
278 size = 32;
279
280 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300281 if (!images)
282 return -1;
283
Tiago Vignatti90fada42012-07-16 12:02:08 -0400284 cursor = xcb_cursor_images_load_cursor (wm, images);
285 XcursorImagesDestroy (images);
286
287 return cursor;
288}
289
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400290void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400291dump_property(struct weston_wm *wm,
292 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400293{
294 int32_t *incr_value;
295 const char *text_value, *name;
296 xcb_atom_t *atom_value;
297 int width, len;
298 uint32_t i;
299
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400300 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400301 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400302 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400303 return;
304 }
305
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400306 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
307 get_atom_name(wm->conn, reply->type),
308 reply->format,
309 xcb_get_property_value_length(reply),
310 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400311
312 if (reply->type == wm->atom.incr) {
313 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400314 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400315 } else if (reply->type == wm->atom.utf8_string ||
316 reply->type == wm->atom.string) {
317 text_value = xcb_get_property_value(reply);
318 if (reply->value_len > 40)
319 len = 40;
320 else
321 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400322 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400323 } else if (reply->type == XCB_ATOM_ATOM) {
324 atom_value = xcb_get_property_value(reply);
325 for (i = 0; i < reply->value_len; i++) {
326 name = get_atom_name(wm->conn, atom_value[i]);
327 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400328 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400329 width = 4;
330 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400331 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400332 }
333
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400334 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400335 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400336 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400337 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400338 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400339 }
340}
341
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200342static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400343read_and_dump_property(struct weston_wm *wm,
344 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400345{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400346 xcb_get_property_reply_t *reply;
347 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400348
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400349 cookie = xcb_get_property(wm->conn, 0, window,
350 property, XCB_ATOM_ANY, 0, 2048);
351 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400352
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400353 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400355 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400356}
357
358/* We reuse some predefined, but otherwise useles atoms */
359#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
360#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500361#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700362#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363
364static void
365weston_wm_window_read_properties(struct weston_wm_window *window)
366{
367 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200368 struct weston_shell_interface *shell_interface =
369 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400370
371#define F(field) offsetof(struct weston_wm_window, field)
372 const struct {
373 xcb_atom_t atom;
374 xcb_atom_t type;
375 int offset;
376 } props[] = {
377 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
378 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
379 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
380 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700381 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500382 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400383 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
384 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300385 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400386 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300387 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400388 };
389#undef F
390
391 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
392 xcb_get_property_reply_t *reply;
393 void *p;
394 uint32_t *xid;
395 xcb_atom_t *atom;
396 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400397
398 if (!window->properties_dirty)
399 return;
400 window->properties_dirty = 0;
401
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400402 for (i = 0; i < ARRAY_LENGTH(props); i++)
403 cookie[i] = xcb_get_property(wm->conn,
404 0, /* delete */
405 window->id,
406 props[i].atom,
407 XCB_ATOM_ANY, 0, 2048);
408
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300409 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700410 window->size_hints.flags = 0;
411 window->motif_hints.flags = 0;
412
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400413 for (i = 0; i < ARRAY_LENGTH(props); i++) {
414 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
415 if (!reply)
416 /* Bad window, typically */
417 continue;
418 if (reply->type == XCB_ATOM_NONE) {
419 /* No such property */
420 free(reply);
421 continue;
422 }
423
424 p = ((char *) window + props[i].offset);
425
426 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300427 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400428 case XCB_ATOM_STRING:
429 /* FIXME: We're using this for both string and
430 utf8_string */
431 if (*(char **) p)
432 free(*(char **) p);
433
434 *(char **) p =
435 strndup(xcb_get_property_value(reply),
436 xcb_get_property_value_length(reply));
437 break;
438 case XCB_ATOM_WINDOW:
439 xid = xcb_get_property_value(reply);
440 *(struct weston_wm_window **) p =
441 hash_table_lookup(wm->window_hash, *xid);
442 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300443 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400444 case XCB_ATOM_ATOM:
445 atom = xcb_get_property_value(reply);
446 *(xcb_atom_t *) p = *atom;
447 break;
448 case TYPE_WM_PROTOCOLS:
449 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700450 case TYPE_WM_NORMAL_HINTS:
451 memcpy(&window->size_hints,
452 xcb_get_property_value(reply),
453 sizeof window->size_hints);
454 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500455 case TYPE_NET_WM_STATE:
456 window->fullscreen = 0;
457 atom = xcb_get_property_value(reply);
458 for (i = 0; i < reply->value_len; i++)
459 if (atom[i] == wm->atom.net_wm_state_fullscreen)
460 window->fullscreen = 1;
461 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400462 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700463 memcpy(&window->motif_hints,
464 xcb_get_property_value(reply),
465 sizeof window->motif_hints);
466 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
467 window->decorate =
468 window->motif_hints.decorations > 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400469 break;
470 default:
471 break;
472 }
473 free(reply);
474 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200475
476 if (window->shsurf && window->name)
477 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500478 if (window->frame && window->name)
479 frame_set_title(window->frame, window->name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400480}
481
482static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400483weston_wm_window_get_frame_size(struct weston_wm_window *window,
484 int *width, int *height)
485{
486 struct theme *t = window->wm->theme;
487
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500488 if (window->fullscreen) {
489 *width = window->width;
490 *height = window->height;
491 } else if (window->decorate) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500492 *width = frame_width(window->frame);
493 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400494 } else {
495 *width = window->width + t->margin * 2;
496 *height = window->height + t->margin * 2;
497 }
498}
499
500static void
501weston_wm_window_get_child_position(struct weston_wm_window *window,
502 int *x, int *y)
503{
504 struct theme *t = window->wm->theme;
505
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500506 if (window->fullscreen) {
507 *x = 0;
508 *y = 0;
509 } else if (window->decorate) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500510 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400511 } else {
512 *x = t->margin;
513 *y = t->margin;
514 }
515}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400516
517static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500518weston_wm_window_send_configure_notify(struct weston_wm_window *window)
519{
520 xcb_configure_notify_event_t configure_notify;
521 struct weston_wm *wm = window->wm;
522 int x, y;
523
524 weston_wm_window_get_child_position(window, &x, &y);
525 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
526 configure_notify.pad0 = 0;
527 configure_notify.event = window->id;
528 configure_notify.window = window->id;
529 configure_notify.above_sibling = XCB_WINDOW_NONE;
530 configure_notify.x = x;
531 configure_notify.y = y;
532 configure_notify.width = window->width;
533 configure_notify.height = window->height;
534 configure_notify.border_width = 0;
535 configure_notify.override_redirect = 0;
536 configure_notify.pad1 = 0;
537
538 xcb_send_event(wm->conn, 0, window->id,
539 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
540 (char *) &configure_notify);
541}
542
543static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400544weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
545{
546 xcb_configure_request_event_t *configure_request =
547 (xcb_configure_request_event_t *) event;
548 struct weston_wm_window *window;
549 uint32_t mask, values[16];
550 int x, y, width, height, i = 0;
551
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400552 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
553 configure_request->window,
554 configure_request->x, configure_request->y,
555 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400556
557 window = hash_table_lookup(wm->window_hash, configure_request->window);
558
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500559 if (window->fullscreen) {
560 weston_wm_window_send_configure_notify(window);
561 return;
562 }
563
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400564 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
565 window->width = configure_request->width;
566 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
567 window->height = configure_request->height;
568
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500569 if (window->frame)
570 frame_resize_inside(window->frame, window->width, window->height);
571
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400572 weston_wm_window_get_child_position(window, &x, &y);
573 values[i++] = x;
574 values[i++] = y;
575 values[i++] = window->width;
576 values[i++] = window->height;
577 values[i++] = 0;
578 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
579 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
580 XCB_CONFIG_WINDOW_BORDER_WIDTH;
581 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
582 values[i++] = configure_request->sibling;
583 mask |= XCB_CONFIG_WINDOW_SIBLING;
584 }
585 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
586 values[i++] = configure_request->stack_mode;
587 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
588 }
589
590 xcb_configure_window(wm->conn, window->id, mask, values);
591
592 weston_wm_window_get_frame_size(window, &width, &height);
593 values[0] = width;
594 values[1] = height;
595 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
596 xcb_configure_window(wm->conn, window->frame_id, mask, values);
597
598 weston_wm_window_schedule_repaint(window);
599}
600
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400601static int
602our_resource(struct weston_wm *wm, uint32_t id)
603{
604 const xcb_setup_t *setup;
605
606 setup = xcb_get_setup(wm->conn);
607
608 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
609}
610
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400611static void
612weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
613{
614 xcb_configure_notify_event_t *configure_notify =
615 (xcb_configure_notify_event_t *) event;
616 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400617
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400618 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400619 configure_notify->window,
620 configure_notify->x, configure_notify->y,
621 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300622
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400623 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700624 window->x = configure_notify->x;
625 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700626 if (window->override_redirect) {
627 window->width = configure_notify->width;
628 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500629 if (window->frame)
630 frame_resize_inside(window->frame,
631 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700632 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400633}
634
635static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300636weston_wm_kill_client(struct wl_listener *listener, void *data)
637{
638 struct weston_surface *surface = data;
639 struct weston_wm_window *window = get_wm_window(surface);
640 char name[1024];
641
642 if (!window)
643 return;
644
645 gethostname(name, 1024);
646
647 /* this is only one heuristic to guess the PID of a client is valid,
648 * assuming it's compliant with icccm and ewmh. Non-compliants and
649 * remote applications of course fail. */
650 if (!strcmp(window->machine, name) && window->pid != 0)
651 kill(window->pid, SIGKILL);
652}
653
654static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400655weston_wm_window_activate(struct wl_listener *listener, void *data)
656{
657 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200658 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600659 struct weston_wm *wm =
660 container_of(listener, struct weston_wm, activate_listener);
661 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400662
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200663 if (surface) {
664 window = get_wm_window(surface);
665 }
666
Scott Moreau85ecac02012-05-21 15:49:14 -0600667 if (window) {
668 client_message.response_type = XCB_CLIENT_MESSAGE;
669 client_message.format = 32;
670 client_message.window = window->id;
671 client_message.type = wm->atom.wm_protocols;
672 client_message.data.data32[0] = wm->atom.wm_take_focus;
673 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
674
675 xcb_send_event(wm->conn, 0, window->id,
676 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
677 (char *) &client_message);
678
679 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
680 window->id, XCB_TIME_CURRENT_TIME);
681 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400682 xcb_set_input_focus (wm->conn,
683 XCB_INPUT_FOCUS_POINTER_ROOT,
684 XCB_NONE,
685 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600686 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400687
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500688 if (wm->focus_window) {
689 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400690 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500691 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400692 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500693 if (wm->focus_window) {
694 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400695 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500696 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400697}
698
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300699static void
700weston_wm_window_transform(struct wl_listener *listener, void *data)
701{
702 struct weston_surface *surface = data;
703 struct weston_wm_window *window = get_wm_window(surface);
704 struct weston_wm *wm =
705 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300706 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300707
708 if (!window || !wm)
709 return;
710
711 if (!weston_surface_is_mapped(surface))
712 return;
713
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700714 if (window->x != surface->geometry.x ||
715 window->y != surface->geometry.y) {
716 values[0] = surface->geometry.x;
717 values[1] = surface->geometry.y;
718 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300719
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700720 xcb_configure_window(wm->conn, window->frame_id, mask, values);
721 xcb_flush(wm->conn);
722 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300723}
724
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400725#define ICCCM_WITHDRAWN_STATE 0
726#define ICCCM_NORMAL_STATE 1
727#define ICCCM_ICONIC_STATE 3
728
729static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500730weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400731{
732 struct weston_wm *wm = window->wm;
733 uint32_t property[2];
734
735 property[0] = state;
736 property[1] = XCB_WINDOW_NONE;
737
738 xcb_change_property(wm->conn,
739 XCB_PROP_MODE_REPLACE,
740 window->id,
741 wm->atom.wm_state,
742 wm->atom.wm_state,
743 32, /* format */
744 2, property);
745}
746
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400747static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500748weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
749{
750 struct weston_wm *wm = window->wm;
751 uint32_t property[1];
752 int i;
753
754 i = 0;
755 if (window->fullscreen)
756 property[i++] = wm->atom.net_wm_state_fullscreen;
757
758 xcb_change_property(wm->conn,
759 XCB_PROP_MODE_REPLACE,
760 window->id,
761 wm->atom.net_wm_state,
762 XCB_ATOM_ATOM,
763 32, /* format */
764 i, property);
765}
766
767static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700768weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400769{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700770 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400771 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400772 int x, y, width, height;
773
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500774 window->frame = frame_create(window->wm->theme,
775 window->width, window->height,
776 FRAME_BUTTON_CLOSE, window->name);
777 frame_resize_inside(window->frame, window->width, window->height);
778
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400779 weston_wm_window_get_frame_size(window, &width, &height);
780 weston_wm_window_get_child_position(window, &x, &y);
781
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400782 values[0] = wm->screen->black_pixel;
783 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400784 XCB_EVENT_MASK_KEY_PRESS |
785 XCB_EVENT_MASK_KEY_RELEASE |
786 XCB_EVENT_MASK_BUTTON_PRESS |
787 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400788 XCB_EVENT_MASK_POINTER_MOTION |
789 XCB_EVENT_MASK_ENTER_WINDOW |
790 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400791 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400792 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400793 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400794
795 window->frame_id = xcb_generate_id(wm->conn);
796 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400797 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400798 window->frame_id,
799 wm->screen->root,
800 0, 0,
801 width, height,
802 0,
803 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400804 wm->visual_id,
805 XCB_CW_BORDER_PIXEL |
806 XCB_CW_EVENT_MASK |
807 XCB_CW_COLORMAP, values);
808
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400809 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
810
811 values[0] = 0;
812 xcb_configure_window(wm->conn, window->id,
813 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
814
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400815 window->cairo_surface =
816 cairo_xcb_surface_create_with_xrender_format(wm->conn,
817 wm->screen,
818 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400819 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400820 width, height);
821
822 hash_table_insert(wm->window_hash, window->frame_id, window);
823}
824
825static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700826weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
827{
828 xcb_map_request_event_t *map_request =
829 (xcb_map_request_event_t *) event;
830 struct weston_wm_window *window;
831
832 if (our_resource(wm, map_request->window)) {
833 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
834 map_request->window);
835 return;
836 }
837
838 window = hash_table_lookup(wm->window_hash, map_request->window);
839
840 weston_wm_window_read_properties(window);
841
842 if (window->frame_id == XCB_WINDOW_NONE)
843 weston_wm_window_create_frame(window);
844
845 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
846 window->id, window, window->frame_id);
847
848 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
849 weston_wm_window_set_net_wm_state(window);
850
851 xcb_map_window(wm->conn, map_request->window);
852 xcb_map_window(wm->conn, window->frame_id);
853}
854
855static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400856weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
857{
858 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
859
860 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400861 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
862 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400863 return;
864 }
865
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400866 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867}
868
869static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400870weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
871{
872 xcb_unmap_notify_event_t *unmap_notify =
873 (xcb_unmap_notify_event_t *) event;
874 struct weston_wm_window *window;
875
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400876 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
877 unmap_notify->window,
878 unmap_notify->event,
879 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400880
881 if (our_resource(wm, unmap_notify->window))
882 return;
883
MoD31700122013-06-11 19:58:55 -0500884 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400885 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
886 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400887 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400888
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400889 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400890 if (wm->focus_window == window)
891 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400892 if (window->surface)
893 wl_list_remove(&window->surface_destroy_listener.link);
894 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200895 window->shsurf = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700896 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400897}
898
899static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400900weston_wm_window_draw_decoration(void *data)
901{
902 struct weston_wm_window *window = data;
903 struct weston_wm *wm = window->wm;
904 struct theme *t = wm->theme;
905 cairo_t *cr;
906 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500907 int32_t input_x, input_y, input_w, input_h;
908
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400909 uint32_t flags = 0;
910
911 weston_wm_window_read_properties(window);
912
913 window->repaint_source = NULL;
914
915 weston_wm_window_get_frame_size(window, &width, &height);
916 weston_wm_window_get_child_position(window, &x, &y);
917
918 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
919 cr = cairo_create(window->cairo_surface);
920
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500921 if (window->fullscreen) {
922 /* nothing */
923 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400924 if (wm->focus_window == window)
925 flags |= THEME_FRAME_ACTIVE;
926
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500927 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400928 } else {
929 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
930 cairo_set_source_rgba(cr, 0, 0, 0, 0);
931 cairo_paint(cr);
932
933 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
934 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
935 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
936 }
937
938 cairo_destroy(cr);
939
940 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700941 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500942 if(window->has_alpha) {
943 pixman_region32_init(&window->surface->pending.opaque);
944 } else {
945 /* We leave an extra pixel around the X window area to
946 * make sure we don't sample from the undefined alpha
947 * channel when filtering. */
948 pixman_region32_init_rect(&window->surface->pending.opaque,
949 x - 1, y - 1,
950 window->width + 2,
951 window->height + 2);
952 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200953 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500954 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400955
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500956 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500957 pixman_region32_fini(&window->surface->pending.input);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500958 frame_input_rect(window->frame, &input_x, &input_y,
959 &input_w, &input_h);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500960 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500961 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400962 }
963}
964
965static void
966weston_wm_window_schedule_repaint(struct weston_wm_window *window)
967{
968 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300969 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400970
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400971 if (window->frame_id == XCB_WINDOW_NONE) {
972 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300973 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700974 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500975 if(window->has_alpha) {
976 pixman_region32_init(&window->surface->pending.opaque);
977 } else {
978 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
979 width, height);
980 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200981 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400982 }
983 return;
984 }
985
986 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400987 return;
988
989 window->repaint_source =
990 wl_event_loop_add_idle(wm->server->loop,
991 weston_wm_window_draw_decoration,
992 window);
993}
994
995static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400996weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
997{
998 xcb_property_notify_event_t *property_notify =
999 (xcb_property_notify_event_t *) event;
1000 struct weston_wm_window *window;
1001
1002 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001003 if (!window)
1004 return;
1005
1006 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001007
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001008 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001009 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001010 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001011 else
1012 read_and_dump_property(wm, property_notify->window,
1013 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001014
1015 if (property_notify->atom == wm->atom.net_wm_name ||
1016 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001017 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001018}
1019
1020static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001021weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001022 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001023{
1024 struct weston_wm_window *window;
1025 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001026 xcb_get_geometry_cookie_t geometry_cookie;
1027 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001028
Peter Huttererf3d62272013-08-08 11:57:05 +10001029 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001030 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001031 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001032 return;
1033 }
1034
MoD384a11a2013-06-22 11:04:21 -05001035 geometry_cookie = xcb_get_geometry(wm->conn, id);
1036
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001037 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1038 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1039
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001040 window->wm = wm;
1041 window->id = id;
1042 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001043 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001044 window->width = width;
1045 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001046 window->x = x;
1047 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001048
MoD384a11a2013-06-22 11:04:21 -05001049 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1050 /* technically we should use XRender and check the visual format's
1051 alpha_mask, but checking depth is simpler and works in all known cases */
1052 if(geometry_reply != NULL)
1053 window->has_alpha = geometry_reply->depth == 32;
1054 free(geometry_reply);
1055
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001056 hash_table_insert(wm->window_hash, id, window);
1057}
1058
1059static void
1060weston_wm_window_destroy(struct weston_wm_window *window)
1061{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001062 struct weston_wm *wm = window->wm;
1063
1064 if (window->repaint_source)
1065 wl_event_source_remove(window->repaint_source);
1066 if (window->cairo_surface)
1067 cairo_surface_destroy(window->cairo_surface);
1068
1069 if (window->frame_id) {
1070 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1071 xcb_destroy_window(wm->conn, window->frame_id);
1072 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1073 hash_table_remove(wm->window_hash, window->frame_id);
1074 window->frame_id = XCB_WINDOW_NONE;
1075 }
1076
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001077 hash_table_remove(window->wm->window_hash, window->id);
1078 free(window);
1079}
1080
1081static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001082weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1083{
1084 xcb_create_notify_event_t *create_notify =
1085 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001086
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001087 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1088 create_notify->window,
1089 create_notify->width, create_notify->height,
1090 create_notify->override_redirect ? ", override" : "",
1091 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001092
1093 if (our_resource(wm, create_notify->window))
1094 return;
1095
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001096 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001097 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001098 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001099 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001100}
1101
1102static void
1103weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1104{
1105 xcb_destroy_notify_event_t *destroy_notify =
1106 (xcb_destroy_notify_event_t *) event;
1107 struct weston_wm_window *window;
1108
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001109 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1110 destroy_notify->window,
1111 destroy_notify->event,
1112 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001113
1114 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001115 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001116
1117 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001118 weston_wm_window_destroy(window);
1119}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001120
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001121static void
1122weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1123{
1124 xcb_reparent_notify_event_t *reparent_notify =
1125 (xcb_reparent_notify_event_t *) event;
1126 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001127
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001128 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1129 reparent_notify->window,
1130 reparent_notify->parent,
1131 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001132
1133 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001134 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001135 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001136 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001137 } else if (!our_resource(wm, reparent_notify->parent)) {
1138 window = hash_table_lookup(wm->window_hash,
1139 reparent_notify->window);
1140 weston_wm_window_destroy(window);
1141 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001142}
1143
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001144struct weston_seat *
1145weston_wm_pick_seat(struct weston_wm *wm)
1146{
1147 return container_of(wm->server->compositor->seat_list.next,
1148 struct weston_seat, link);
1149}
1150
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001151static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001152weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1153 xcb_client_message_event_t *client_message)
1154{
1155 static const int map[] = {
1156 THEME_LOCATION_RESIZING_TOP_LEFT,
1157 THEME_LOCATION_RESIZING_TOP,
1158 THEME_LOCATION_RESIZING_TOP_RIGHT,
1159 THEME_LOCATION_RESIZING_RIGHT,
1160 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1161 THEME_LOCATION_RESIZING_BOTTOM,
1162 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1163 THEME_LOCATION_RESIZING_LEFT
1164 };
1165
1166 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001167 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001168 int detail;
1169 struct weston_shell_interface *shell_interface =
1170 &wm->server->compositor->shell_interface;
1171
Kristian Høgsberge3148752013-05-06 23:19:49 -04001172 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001173 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001174 return;
1175
1176 detail = client_message->data.data32[2];
1177 switch (detail) {
1178 case _NET_WM_MOVERESIZE_MOVE:
1179 shell_interface->move(window->shsurf, seat);
1180 break;
1181 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1182 case _NET_WM_MOVERESIZE_SIZE_TOP:
1183 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1184 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1185 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1186 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1187 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1188 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1189 shell_interface->resize(window->shsurf, seat, map[detail]);
1190 break;
1191 case _NET_WM_MOVERESIZE_CANCEL:
1192 break;
1193 }
1194}
1195
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001196#define _NET_WM_STATE_REMOVE 0
1197#define _NET_WM_STATE_ADD 1
1198#define _NET_WM_STATE_TOGGLE 2
1199
1200static int
1201update_state(int action, int *state)
1202{
1203 int new_state, changed;
1204
1205 switch (action) {
1206 case _NET_WM_STATE_REMOVE:
1207 new_state = 0;
1208 break;
1209 case _NET_WM_STATE_ADD:
1210 new_state = 1;
1211 break;
1212 case _NET_WM_STATE_TOGGLE:
1213 new_state = !*state;
1214 break;
1215 default:
1216 return 0;
1217 }
1218
1219 changed = (*state != new_state);
1220 *state = new_state;
1221
1222 return changed;
1223}
1224
1225static void
1226weston_wm_window_configure(void *data);
1227
1228static void
1229weston_wm_window_handle_state(struct weston_wm_window *window,
1230 xcb_client_message_event_t *client_message)
1231{
1232 struct weston_wm *wm = window->wm;
1233 struct weston_shell_interface *shell_interface =
1234 &wm->server->compositor->shell_interface;
1235 uint32_t action, property;
1236
1237 action = client_message->data.data32[0];
1238 property = client_message->data.data32[1];
1239
1240 if (property == wm->atom.net_wm_state_fullscreen &&
1241 update_state(action, &window->fullscreen)) {
1242 weston_wm_window_set_net_wm_state(window);
1243 if (window->fullscreen) {
1244 window->saved_width = window->width;
1245 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001246
1247 if (window->shsurf)
1248 shell_interface->set_fullscreen(window->shsurf,
1249 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1250 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001251 } else {
1252 shell_interface->set_toplevel(window->shsurf);
1253 window->width = window->saved_width;
1254 window->height = window->saved_height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001255 if (window->frame)
1256 frame_resize_inside(window->frame,
1257 window->width,
1258 window->height);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001259 weston_wm_window_configure(window);
1260 }
1261 }
1262}
1263
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001264static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001265weston_wm_handle_client_message(struct weston_wm *wm,
1266 xcb_generic_event_t *event)
1267{
1268 xcb_client_message_event_t *client_message =
1269 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001270 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001271
1272 window = hash_table_lookup(wm->window_hash, client_message->window);
1273
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001274 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1275 get_atom_name(wm->conn, client_message->type),
1276 client_message->data.data32[0],
1277 client_message->data.data32[1],
1278 client_message->data.data32[2],
1279 client_message->data.data32[3],
1280 client_message->data.data32[4],
1281 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001282
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001283 if (client_message->type == wm->atom.net_wm_moveresize)
1284 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001285 else if (client_message->type == wm->atom.net_wm_state)
1286 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001287}
1288
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001289enum cursor_type {
1290 XWM_CURSOR_TOP,
1291 XWM_CURSOR_BOTTOM,
1292 XWM_CURSOR_LEFT,
1293 XWM_CURSOR_RIGHT,
1294 XWM_CURSOR_TOP_LEFT,
1295 XWM_CURSOR_TOP_RIGHT,
1296 XWM_CURSOR_BOTTOM_LEFT,
1297 XWM_CURSOR_BOTTOM_RIGHT,
1298 XWM_CURSOR_LEFT_PTR,
1299};
1300
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001301/*
1302 * The following correspondences between file names and cursors was copied
1303 * from: https://bugs.kde.org/attachment.cgi?id=67313
1304 */
1305
1306static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001307 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001308 "sw-resize",
1309 "size_bdiag"
1310};
1311
1312static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001313 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001314 "se-resize",
1315 "size_fdiag"
1316};
1317
1318static const char *bottom_sides[] = {
1319 "bottom_side",
1320 "s-resize",
1321 "size_ver"
1322};
1323
1324static const char *left_ptrs[] = {
1325 "left_ptr",
1326 "default",
1327 "top_left_arrow",
1328 "left-arrow"
1329};
1330
1331static const char *left_sides[] = {
1332 "left_side",
1333 "w-resize",
1334 "size_hor"
1335};
1336
1337static const char *right_sides[] = {
1338 "right_side",
1339 "e-resize",
1340 "size_hor"
1341};
1342
1343static const char *top_left_corners[] = {
1344 "top_left_corner",
1345 "nw-resize",
1346 "size_fdiag"
1347};
1348
1349static const char *top_right_corners[] = {
1350 "top_right_corner",
1351 "ne-resize",
1352 "size_bdiag"
1353};
1354
1355static const char *top_sides[] = {
1356 "top_side",
1357 "n-resize",
1358 "size_ver"
1359};
1360
1361struct cursor_alternatives {
1362 const char **names;
1363 size_t count;
1364};
1365
1366static const struct cursor_alternatives cursors[] = {
1367 {top_sides, ARRAY_LENGTH(top_sides)},
1368 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1369 {left_sides, ARRAY_LENGTH(left_sides)},
1370 {right_sides, ARRAY_LENGTH(right_sides)},
1371 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1372 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1373 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1374 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1375 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001376};
1377
1378static void
1379weston_wm_create_cursors(struct weston_wm *wm)
1380{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001381 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001382 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001383 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001384
1385 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1386 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001387 for (j = 0; j < cursors[i].count; j++) {
1388 name = cursors[i].names[j];
1389 wm->cursors[i] =
1390 xcb_cursor_library_load_cursor(wm, name);
1391 if (wm->cursors[i] != (xcb_cursor_t)-1)
1392 break;
1393 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001394 }
1395
1396 wm->last_cursor = -1;
1397}
1398
1399static void
1400weston_wm_destroy_cursors(struct weston_wm *wm)
1401{
1402 uint8_t i;
1403
1404 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1405 xcb_free_cursor(wm->conn, wm->cursors[i]);
1406
1407 free(wm->cursors);
1408}
1409
1410static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001411get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001412{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001413 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001414
1415 switch (location) {
1416 case THEME_LOCATION_RESIZING_TOP:
1417 return XWM_CURSOR_TOP;
1418 case THEME_LOCATION_RESIZING_BOTTOM:
1419 return XWM_CURSOR_BOTTOM;
1420 case THEME_LOCATION_RESIZING_LEFT:
1421 return XWM_CURSOR_LEFT;
1422 case THEME_LOCATION_RESIZING_RIGHT:
1423 return XWM_CURSOR_RIGHT;
1424 case THEME_LOCATION_RESIZING_TOP_LEFT:
1425 return XWM_CURSOR_TOP_LEFT;
1426 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1427 return XWM_CURSOR_TOP_RIGHT;
1428 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1429 return XWM_CURSOR_BOTTOM_LEFT;
1430 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1431 return XWM_CURSOR_BOTTOM_RIGHT;
1432 case THEME_LOCATION_EXTERIOR:
1433 case THEME_LOCATION_TITLEBAR:
1434 default:
1435 return XWM_CURSOR_LEFT_PTR;
1436 }
1437}
1438
1439static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001440weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1441 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001442{
1443 uint32_t cursor_value_list;
1444
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001445 if (wm->last_cursor == cursor)
1446 return;
1447
1448 wm->last_cursor = cursor;
1449
1450 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001451 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001452 XCB_CW_CURSOR, &cursor_value_list);
1453 xcb_flush(wm->conn);
1454}
1455
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001456static void
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001457weston_wm_window_close(struct weston_wm_window *window)
1458{
1459 xcb_client_message_event_t client_message;
1460
1461 client_message.response_type = XCB_CLIENT_MESSAGE;
1462 client_message.format = 32;
1463 client_message.window = window->id;
1464 client_message.type = window->wm->atom.wm_protocols;
1465 client_message.data.data32[0] = window->wm->atom.wm_delete_window;
1466 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
1467
1468 xcb_send_event(window->wm->conn, 0, window->id,
1469 XCB_EVENT_MASK_NO_EVENT,
1470 (char *) &client_message);
1471}
1472
1473static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001474weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1475{
1476 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1477 struct weston_shell_interface *shell_interface =
1478 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001479 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001480 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001481 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001482 enum frame_button_state button_state;
1483 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001484
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001485 wm_log("XCB_BUTTON_%s (detail %d)\n",
1486 button->response_type == XCB_BUTTON_PRESS ?
1487 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001488
1489 window = hash_table_lookup(wm->window_hash, button->event);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001490 if (!window || !window->decorate)
1491 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001492
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001493 if (button->detail != 1 && button->detail != 2)
1494 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001495
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001496 button_state = button->response_type == XCB_BUTTON_PRESS ?
1497 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1498 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1499
1500 location = frame_pointer_button(window->frame, NULL,
1501 button_id, button_state);
1502 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1503 weston_wm_window_schedule_repaint(window);
1504
1505 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
1506 shell_interface->move(window->shsurf, seat);
1507 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1508 }
1509
1510 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
1511 shell_interface->resize(window->shsurf, seat, location);
1512 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1513 }
1514
1515 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
1516 weston_wm_window_close(window);
1517 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001518 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001519}
1520
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001521static void
1522weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1523{
1524 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1525 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001526 enum theme_location location;
1527 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001528
1529 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001530 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001531 return;
1532
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001533 location = frame_pointer_motion(window->frame, NULL,
1534 motion->event_x, motion->event_y);
1535 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1536 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001537
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001538 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001539 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001540}
1541
1542static void
1543weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1544{
1545 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1546 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001547 enum theme_location location;
1548 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001549
1550 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001551 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001552 return;
1553
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001554 location = frame_pointer_enter(window->frame, NULL,
1555 enter->event_x, enter->event_y);
1556 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1557 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001558
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001559 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001560 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001561}
1562
1563static void
1564weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1565{
1566 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1567 struct weston_wm_window *window;
1568
1569 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001570 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001571 return;
1572
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001573 frame_pointer_leave(window->frame, NULL);
1574 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1575 weston_wm_window_schedule_repaint(window);
1576
Tiago Vignattic1903232012-07-16 12:15:37 -04001577 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001578}
1579
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001580static int
1581weston_wm_handle_event(int fd, uint32_t mask, void *data)
1582{
1583 struct weston_wm *wm = data;
1584 xcb_generic_event_t *event;
1585 int count = 0;
1586
1587 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1588 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001589 free(event);
1590 count++;
1591 continue;
1592 }
1593
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001594 if (weston_wm_handle_dnd_event(wm, event)) {
1595 free(event);
1596 count++;
1597 continue;
1598 }
1599
MoD31700122013-06-11 19:58:55 -05001600 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001601 case XCB_BUTTON_PRESS:
1602 case XCB_BUTTON_RELEASE:
1603 weston_wm_handle_button(wm, event);
1604 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001605 case XCB_ENTER_NOTIFY:
1606 weston_wm_handle_enter(wm, event);
1607 break;
1608 case XCB_LEAVE_NOTIFY:
1609 weston_wm_handle_leave(wm, event);
1610 break;
1611 case XCB_MOTION_NOTIFY:
1612 weston_wm_handle_motion(wm, event);
1613 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001614 case XCB_CREATE_NOTIFY:
1615 weston_wm_handle_create_notify(wm, event);
1616 break;
1617 case XCB_MAP_REQUEST:
1618 weston_wm_handle_map_request(wm, event);
1619 break;
1620 case XCB_MAP_NOTIFY:
1621 weston_wm_handle_map_notify(wm, event);
1622 break;
1623 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001624 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001625 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001626 case XCB_REPARENT_NOTIFY:
1627 weston_wm_handle_reparent_notify(wm, event);
1628 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001629 case XCB_CONFIGURE_REQUEST:
1630 weston_wm_handle_configure_request(wm, event);
1631 break;
1632 case XCB_CONFIGURE_NOTIFY:
1633 weston_wm_handle_configure_notify(wm, event);
1634 break;
1635 case XCB_DESTROY_NOTIFY:
1636 weston_wm_handle_destroy_notify(wm, event);
1637 break;
1638 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001639 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001640 break;
1641 case XCB_PROPERTY_NOTIFY:
1642 weston_wm_handle_property_notify(wm, event);
1643 break;
1644 case XCB_CLIENT_MESSAGE:
1645 weston_wm_handle_client_message(wm, event);
1646 break;
1647 }
1648
1649 free(event);
1650 count++;
1651 }
1652
1653 xcb_flush(wm->conn);
1654
1655 return count;
1656}
1657
1658static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001659weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1660{
1661 xcb_depth_iterator_t d_iter;
1662 xcb_visualtype_iterator_t vt_iter;
1663 xcb_visualtype_t *visualtype;
1664
1665 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1666 visualtype = NULL;
1667 while (d_iter.rem > 0) {
1668 if (d_iter.data->depth == 32) {
1669 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1670 visualtype = vt_iter.data;
1671 break;
1672 }
1673
1674 xcb_depth_next(&d_iter);
1675 }
1676
1677 if (visualtype == NULL) {
1678 weston_log("no 32 bit visualtype\n");
1679 return;
1680 }
1681
1682 wm->visual_id = visualtype->visual_id;
1683 wm->colormap = xcb_generate_id(wm->conn);
1684 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1685 wm->colormap, wm->screen->root, wm->visual_id);
1686}
1687
1688static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001689weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001690{
1691
1692#define F(field) offsetof(struct weston_wm, field)
1693
1694 static const struct { const char *name; int offset; } atoms[] = {
1695 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001696 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001697 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1698 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001699 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001700 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001701 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001702 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001703 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001704 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001705 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1706 { "_NET_WM_STATE", F(atom.net_wm_state) },
1707 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1708 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1709 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1710 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1711
1712 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1713 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1714 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1715 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1716 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1717 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1718 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001719 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1720 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001721 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1722 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1723 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1724 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1725 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1726
1727 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1728 { "_NET_SUPPORTING_WM_CHECK",
1729 F(atom.net_supporting_wm_check) },
1730 { "_NET_SUPPORTED", F(atom.net_supported) },
1731 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1732 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001733 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001734 { "TARGETS", F(atom.targets) },
1735 { "UTF8_STRING", F(atom.utf8_string) },
1736 { "_WL_SELECTION", F(atom.wl_selection) },
1737 { "INCR", F(atom.incr) },
1738 { "TIMESTAMP", F(atom.timestamp) },
1739 { "MULTIPLE", F(atom.multiple) },
1740 { "UTF8_STRING" , F(atom.utf8_string) },
1741 { "COMPOUND_TEXT", F(atom.compound_text) },
1742 { "TEXT", F(atom.text) },
1743 { "STRING", F(atom.string) },
1744 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1745 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001746 { "XdndSelection", F(atom.xdnd_selection) },
1747 { "XdndAware", F(atom.xdnd_aware) },
1748 { "XdndEnter", F(atom.xdnd_enter) },
1749 { "XdndLeave", F(atom.xdnd_leave) },
1750 { "XdndDrop", F(atom.xdnd_drop) },
1751 { "XdndStatus", F(atom.xdnd_status) },
1752 { "XdndFinished", F(atom.xdnd_finished) },
1753 { "XdndTypeList", F(atom.xdnd_type_list) },
1754 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001755 };
1756#undef F
1757
1758 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1759 xcb_xfixes_query_version_reply_t *xfixes_reply;
1760 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1761 xcb_intern_atom_reply_t *reply;
1762 xcb_render_query_pict_formats_reply_t *formats_reply;
1763 xcb_render_query_pict_formats_cookie_t formats_cookie;
1764 xcb_render_pictforminfo_t *formats;
1765 uint32_t i;
1766
1767 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07001768 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001769
1770 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1771
1772 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1773 cookies[i] = xcb_intern_atom (wm->conn, 0,
1774 strlen(atoms[i].name),
1775 atoms[i].name);
1776
1777 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1778 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1779 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1780 free(reply);
1781 }
1782
1783 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1784 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001785 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001786
1787 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1788 XCB_XFIXES_MAJOR_VERSION,
1789 XCB_XFIXES_MINOR_VERSION);
1790 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1791 xfixes_cookie, NULL);
1792
Martin Minarik6d118362012-06-07 18:01:59 +02001793 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001794 xfixes_reply->major_version, xfixes_reply->minor_version);
1795
1796 free(xfixes_reply);
1797
1798 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1799 formats_cookie, 0);
1800 if (formats_reply == NULL)
1801 return;
1802
1803 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001804 for (i = 0; i < formats_reply->num_formats; i++) {
1805 if (formats[i].direct.red_mask != 0xff &&
1806 formats[i].direct.red_shift != 16)
1807 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001808 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1809 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001810 wm->format_rgb = formats[i];
1811 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1812 formats[i].depth == 32 &&
1813 formats[i].direct.alpha_mask == 0xff &&
1814 formats[i].direct.alpha_shift == 24)
1815 wm->format_rgba = formats[i];
1816 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001817
1818 free(formats_reply);
1819}
1820
1821static void
1822weston_wm_create_wm_window(struct weston_wm *wm)
1823{
1824 static const char name[] = "Weston WM";
1825
1826 wm->wm_window = xcb_generate_id(wm->conn);
1827 xcb_create_window(wm->conn,
1828 XCB_COPY_FROM_PARENT,
1829 wm->wm_window,
1830 wm->screen->root,
1831 0, 0,
1832 10, 10,
1833 0,
1834 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1835 wm->screen->root_visual,
1836 0, NULL);
1837
1838 xcb_change_property(wm->conn,
1839 XCB_PROP_MODE_REPLACE,
1840 wm->wm_window,
1841 wm->atom.net_supporting_wm_check,
1842 XCB_ATOM_WINDOW,
1843 32, /* format */
1844 1, &wm->wm_window);
1845
1846 xcb_change_property(wm->conn,
1847 XCB_PROP_MODE_REPLACE,
1848 wm->wm_window,
1849 wm->atom.net_wm_name,
1850 wm->atom.utf8_string,
1851 8, /* format */
1852 strlen(name), name);
1853
1854 xcb_change_property(wm->conn,
1855 XCB_PROP_MODE_REPLACE,
1856 wm->screen->root,
1857 wm->atom.net_supporting_wm_check,
1858 XCB_ATOM_WINDOW,
1859 32, /* format */
1860 1, &wm->wm_window);
1861
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001862 /* Claim the WM_S0 selection even though we don't suport
1863 * the --replace functionality. */
1864 xcb_set_selection_owner(wm->conn,
1865 wm->wm_window,
1866 wm->atom.wm_s0,
1867 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001868
1869 xcb_set_selection_owner(wm->conn,
1870 wm->wm_window,
1871 wm->atom.net_wm_cm_s0,
1872 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001873}
1874
1875struct weston_wm *
1876weston_wm_create(struct weston_xserver *wxs)
1877{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001878 struct weston_wm *wm;
1879 struct wl_event_loop *loop;
1880 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001881 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001882 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001883 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001884
Peter Huttererf3d62272013-08-08 11:57:05 +10001885 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001886 if (wm == NULL)
1887 return NULL;
1888
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001889 wm->server = wxs;
1890 wm->window_hash = hash_table_create();
1891 if (wm->window_hash == NULL) {
1892 free(wm);
1893 return NULL;
1894 }
1895
1896 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001897 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001898 hash_table_destroy(wm->window_hash);
1899 free(wm);
1900 return NULL;
1901 }
1902
1903 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001904 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001905 close(sv[1]);
1906
1907 /* xcb_connect_to_fd takes ownership of the fd. */
1908 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1909 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001910 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001911 close(sv[0]);
1912 hash_table_destroy(wm->window_hash);
1913 free(wm);
1914 return NULL;
1915 }
1916
1917 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1918 wm->screen = s.data;
1919
1920 loop = wl_display_get_event_loop(wxs->wl_display);
1921 wm->source =
1922 wl_event_loop_add_fd(loop, sv[0],
1923 WL_EVENT_READABLE,
1924 weston_wm_handle_event, wm);
1925 wl_event_source_check(wm->source);
1926
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001927 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001928 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001929
1930 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001931 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1932 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1933 XCB_EVENT_MASK_PROPERTY_CHANGE;
1934 xcb_change_window_attributes(wm->conn, wm->screen->root,
1935 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07001936
1937 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
1938 XCB_COMPOSITE_REDIRECT_MANUAL);
1939
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001940 wm->theme = theme_create();
1941
1942 weston_wm_create_wm_window(wm);
1943
1944 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001945 supported[1] = wm->atom.net_wm_state;
1946 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001947 xcb_change_property(wm->conn,
1948 XCB_PROP_MODE_REPLACE,
1949 wm->screen->root,
1950 wm->atom.net_supported,
1951 XCB_ATOM_ATOM,
1952 32, /* format */
1953 ARRAY_LENGTH(supported), supported);
1954
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001955 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001956
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001957 weston_wm_dnd_init(wm);
1958
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001959 xcb_flush(wm->conn);
1960
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001961 wm->activate_listener.notify = weston_wm_window_activate;
1962 wl_signal_add(&wxs->compositor->activate_signal,
1963 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001964 wm->transform_listener.notify = weston_wm_window_transform;
1965 wl_signal_add(&wxs->compositor->transform_signal,
1966 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001967 wm->kill_listener.notify = weston_wm_kill_client;
1968 wl_signal_add(&wxs->compositor->kill_signal,
1969 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001970
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001971 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001972 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001973
Martin Minarik6d118362012-06-07 18:01:59 +02001974 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001975
1976 return wm;
1977}
1978
1979void
1980weston_wm_destroy(struct weston_wm *wm)
1981{
1982 /* FIXME: Free windows in hash. */
1983 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001984 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001985 xcb_disconnect(wm->conn);
1986 wl_event_source_remove(wm->source);
1987 wl_list_remove(&wm->selection_listener.link);
1988 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001989 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001990 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001991
1992 free(wm);
1993}
1994
1995static void
1996surface_destroy(struct wl_listener *listener, void *data)
1997{
1998 struct weston_wm_window *window =
1999 container_of(listener,
2000 struct weston_wm_window, surface_destroy_listener);
2001
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002002 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07002003
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002004 /* This should have been freed by the shell.
2005 Don't try to use it later. */
2006 window->shsurf = NULL;
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07002007 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002008}
2009
2010static struct weston_wm_window *
2011get_wm_window(struct weston_surface *surface)
2012{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002013 struct wl_listener *listener;
2014
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002015 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002016 if (listener)
2017 return container_of(listener, struct weston_wm_window,
2018 surface_destroy_listener);
2019
2020 return NULL;
2021}
2022
2023static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002024weston_wm_window_configure(void *data)
2025{
2026 struct weston_wm_window *window = data;
2027 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002028 uint32_t values[4];
2029 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002030
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002031 weston_wm_window_get_child_position(window, &x, &y);
2032 values[0] = x;
2033 values[1] = y;
2034 values[2] = window->width;
2035 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002036 xcb_configure_window(wm->conn,
2037 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002038 XCB_CONFIG_WINDOW_X |
2039 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002040 XCB_CONFIG_WINDOW_WIDTH |
2041 XCB_CONFIG_WINDOW_HEIGHT,
2042 values);
2043
2044 weston_wm_window_get_frame_size(window, &width, &height);
2045 values[0] = width;
2046 values[1] = height;
2047 xcb_configure_window(wm->conn,
2048 window->frame_id,
2049 XCB_CONFIG_WINDOW_WIDTH |
2050 XCB_CONFIG_WINDOW_HEIGHT,
2051 values);
2052
2053 window->configure_source = NULL;
2054
2055 weston_wm_window_schedule_repaint(window);
2056}
2057
2058static void
2059send_configure(struct weston_surface *surface,
2060 uint32_t edges, int32_t width, int32_t height)
2061{
2062 struct weston_wm_window *window = get_wm_window(surface);
2063 struct weston_wm *wm = window->wm;
2064 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002065 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002066
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002067 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002068 hborder = 0;
2069 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002070 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002071 hborder = 2 * (t->margin + t->width);
2072 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002073 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002074 hborder = 2 * t->margin;
2075 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002076 }
2077
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002078 if (width > hborder)
2079 window->width = width - hborder;
2080 else
2081 window->width = 1;
2082
2083 if (height > vborder)
2084 window->height = height - vborder;
2085 else
2086 window->height = 1;
2087
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002088 if (window->frame)
2089 frame_resize_inside(window->frame, window->width, window->height);
2090
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002091 if (window->configure_source)
2092 return;
2093
2094 window->configure_source =
2095 wl_event_loop_add_idle(wm->server->loop,
2096 weston_wm_window_configure, window);
2097}
2098
2099static const struct weston_shell_client shell_client = {
2100 send_configure
2101};
2102
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002103static int
2104legacy_fullscreen(struct weston_wm *wm,
2105 struct weston_wm_window *window,
2106 struct weston_output **output_ret)
2107{
2108 struct weston_compositor *compositor = wm->server->compositor;
2109 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002110 uint32_t minmax = PMinSize | PMaxSize;
2111 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002112
2113 /* Heuristics for detecting legacy fullscreen windows... */
2114
2115 wl_list_for_each(output, &compositor->output_list, link) {
2116 if (output->x == window->x &&
2117 output->y == window->y &&
2118 output->width == window->width &&
2119 output->height == window->height &&
2120 window->override_redirect) {
2121 *output_ret = output;
2122 return 1;
2123 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002124
2125 matching_size = 0;
2126 if ((window->size_hints.flags & (USSize |PSize)) &&
2127 window->size_hints.width == output->width &&
2128 window->size_hints.height == output->height)
2129 matching_size = 1;
2130 if ((window->size_hints.flags & minmax) == minmax &&
2131 window->size_hints.min_width == output->width &&
2132 window->size_hints.min_height == output->height &&
2133 window->size_hints.max_width == output->width &&
2134 window->size_hints.max_height == output->height)
2135 matching_size = 1;
2136
2137 if (matching_size && !window->decorate &&
2138 (window->size_hints.flags & (USPosition | PPosition)) &&
2139 window->size_hints.x == output->x &&
2140 window->size_hints.y == output->y) {
2141 *output_ret = output;
2142 return 1;
2143 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002144 }
2145
2146 return 0;
2147}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002148static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002149xserver_map_shell_surface(struct weston_wm *wm,
2150 struct weston_wm_window *window)
2151{
2152 struct weston_shell_interface *shell_interface =
2153 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002154 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002155
2156 if (!shell_interface->create_shell_surface)
2157 return;
2158
2159 window->shsurf =
2160 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002161 window->surface,
2162 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002163
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002164 if (window->name)
2165 shell_interface->set_title(window->shsurf, window->name);
2166
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002167 if (window->fullscreen) {
2168 window->saved_width = window->width;
2169 window->saved_height = window->height;
2170 shell_interface->set_fullscreen(window->shsurf,
2171 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2172 0, NULL);
2173 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002174 } else if (legacy_fullscreen(wm, window, &output)) {
2175 window->fullscreen = 1;
2176 shell_interface->set_fullscreen(window->shsurf,
2177 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2178 0, output);
Giulio Camuffoca43f092013-09-11 17:49:13 +02002179 } else if (!window->override_redirect && !window->transient_for) {
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002180 shell_interface->set_toplevel(window->shsurf);
2181 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002182 } else {
2183 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002184 window->x,
2185 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002186 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002187 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002188}
2189
2190static void
2191xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2192 struct wl_resource *surface_resource, uint32_t id)
2193{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002194 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002195 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002196 struct weston_surface *surface =
2197 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002198 struct weston_wm_window *window;
2199
2200 if (client != wxs->client)
2201 return;
2202
2203 window = hash_table_lookup(wm->window_hash, id);
2204 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002205 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002206 return;
2207 }
2208
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002209 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002210
2211 weston_wm_window_read_properties(window);
2212
Giulio Camuffo1cf329b2013-09-20 16:16:06 +02002213 /* A weston_wm_window may have many different surfaces assigned
2214 * throughout its life, so we must make sure to remove the listener
2215 * from the old surface signal list. */
2216 if (window->surface)
2217 wl_list_remove(&window->surface_destroy_listener.link);
2218
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219 window->surface = (struct weston_surface *) surface;
2220 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002221 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002222 &window->surface_destroy_listener);
2223
2224 weston_wm_window_schedule_repaint(window);
2225 xserver_map_shell_surface(wm, window);
2226}
2227
2228const struct xserver_interface xserver_implementation = {
2229 xserver_set_window_id
2230};