blob: 9d01d3616e8742c7ae7b62561fd6b7b4867df64f [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>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040035
36#include "xwayland.h"
37
38#include "../../shared/cairo-util.h"
39#include "../compositor.h"
40#include "xserver-server-protocol.h"
41#include "hash.h"
42
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070043struct wm_size_hints {
44 uint32_t flags;
45 int32_t x, y;
46 int32_t width, height; /* should set so old wm's don't mess up */
47 int32_t min_width, min_height;
48 int32_t max_width, max_height;
49 int32_t width_inc, height_inc;
50 struct {
51 int32_t x;
52 int32_t y;
53 } min_aspect, max_aspect;
54 int32_t base_width, base_height;
55 int32_t win_gravity;
56};
57
58#define USPosition (1L << 0)
59#define USSize (1L << 1)
60#define PPosition (1L << 2)
61#define PSize (1L << 3)
62#define PMinSize (1L << 4)
63#define PMaxSize (1L << 5)
64#define PResizeInc (1L << 6)
65#define PAspect (1L << 7)
66#define PBaseSize (1L << 8)
67#define PWinGravity (1L << 9)
68
Kristian Høgsberg380deee2012-05-21 17:12:41 -040069struct motif_wm_hints {
70 uint32_t flags;
71 uint32_t functions;
72 uint32_t decorations;
73 int32_t input_mode;
74 uint32_t status;
75};
76
77#define MWM_HINTS_FUNCTIONS (1L << 0)
78#define MWM_HINTS_DECORATIONS (1L << 1)
79#define MWM_HINTS_INPUT_MODE (1L << 2)
80#define MWM_HINTS_STATUS (1L << 3)
81
82#define MWM_FUNC_ALL (1L << 0)
83#define MWM_FUNC_RESIZE (1L << 1)
84#define MWM_FUNC_MOVE (1L << 2)
85#define MWM_FUNC_MINIMIZE (1L << 3)
86#define MWM_FUNC_MAXIMIZE (1L << 4)
87#define MWM_FUNC_CLOSE (1L << 5)
88
89#define MWM_DECOR_ALL (1L << 0)
90#define MWM_DECOR_BORDER (1L << 1)
91#define MWM_DECOR_RESIZEH (1L << 2)
92#define MWM_DECOR_TITLE (1L << 3)
93#define MWM_DECOR_MENU (1L << 4)
94#define MWM_DECOR_MINIMIZE (1L << 5)
95#define MWM_DECOR_MAXIMIZE (1L << 6)
96
97#define MWM_INPUT_MODELESS 0
98#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
99#define MWM_INPUT_SYSTEM_MODAL 2
100#define MWM_INPUT_FULL_APPLICATION_MODAL 3
101#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
102
103#define MWM_TEAROFF_WINDOW (1L<<0)
104
105#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
106#define _NET_WM_MOVERESIZE_SIZE_TOP 1
107#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
108#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
109#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
110#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
111#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
112#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
113#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
114#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
115#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
116#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
117
MoD31700122013-06-11 19:58:55 -0500118#define SEND_EVENT_MASK (0x80)
119#define EVENT_TYPE(event) ((event)->response_type & ~SEND_EVENT_MASK)
120
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400121struct weston_wm_window {
122 struct weston_wm *wm;
123 xcb_window_t id;
124 xcb_window_t frame_id;
125 cairo_surface_t *cairo_surface;
126 struct weston_surface *surface;
127 struct shell_surface *shsurf;
128 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øgsberg1a7a57f2013-08-31 00:12:25 -0700146 struct wm_size_hints size_hints;
147 struct motif_wm_hints motif_hints;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400148};
149
150static struct weston_wm_window *
151get_wm_window(struct weston_surface *surface);
152
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400153static void
154weston_wm_window_schedule_repaint(struct weston_wm_window *window);
155
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400156static int __attribute__ ((format (printf, 1, 2)))
157wm_log(const char *fmt, ...)
158{
159#ifdef WM_DEBUG
160 int l;
161 va_list argp;
162
163 va_start(argp, fmt);
164 l = weston_vlog(fmt, argp);
165 va_end(argp);
166
167 return l;
168#else
169 return 0;
170#endif
171}
172
173static int __attribute__ ((format (printf, 1, 2)))
174wm_log_continue(const char *fmt, ...)
175{
176#ifdef WM_DEBUG
177 int l;
178 va_list argp;
179
180 va_start(argp, fmt);
181 l = weston_vlog_continue(fmt, argp);
182 va_end(argp);
183
184 return l;
185#else
186 return 0;
187#endif
188}
189
190
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400191const char *
192get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
193{
194 xcb_get_atom_name_cookie_t cookie;
195 xcb_get_atom_name_reply_t *reply;
196 xcb_generic_error_t *e;
197 static char buffer[64];
198
199 if (atom == XCB_ATOM_NONE)
200 return "None";
201
202 cookie = xcb_get_atom_name (c, atom);
203 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500204
205 if(reply) {
206 snprintf(buffer, sizeof buffer, "%.*s",
207 xcb_get_atom_name_name_length (reply),
208 xcb_get_atom_name_name (reply));
209 } else {
210 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
211 }
212
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400213 free(reply);
214
215 return buffer;
216}
217
Tiago Vignatti90fada42012-07-16 12:02:08 -0400218static xcb_cursor_t
219xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
220{
221 xcb_connection_t *c = wm->conn;
222 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
223 xcb_screen_t *screen = s.data;
224 xcb_gcontext_t gc;
225 xcb_pixmap_t pix;
226 xcb_render_picture_t pic;
227 xcb_cursor_t cursor;
228 int stride = img->width * 4;
229
230 pix = xcb_generate_id(c);
231 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
232
233 pic = xcb_generate_id(c);
234 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
235
236 gc = xcb_generate_id(c);
237 xcb_create_gc(c, gc, pix, 0, 0);
238
239 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
240 img->width, img->height, 0, 0, 0, 32,
241 stride * img->height, (uint8_t *) img->pixels);
242 xcb_free_gc(c, gc);
243
244 cursor = xcb_generate_id(c);
245 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
246
247 xcb_render_free_picture(c, pic);
248 xcb_free_pixmap(c, pix);
249
250 return cursor;
251}
252
253static xcb_cursor_t
254xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
255{
256 /* TODO: treat animated cursors as well */
257 if (images->nimage != 1)
258 return -1;
259
260 return xcb_cursor_image_load_cursor(wm, images->images[0]);
261}
262
263static xcb_cursor_t
264xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
265{
266 xcb_cursor_t cursor;
267 XcursorImages *images;
268 char *v = NULL;
269 int size = 0;
270
271 if (!file)
272 return 0;
273
274 v = getenv ("XCURSOR_SIZE");
275 if (v)
276 size = atoi(v);
277
278 if (!size)
279 size = 32;
280
281 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300282 if (!images)
283 return -1;
284
Tiago Vignatti90fada42012-07-16 12:02:08 -0400285 cursor = xcb_cursor_images_load_cursor (wm, images);
286 XcursorImagesDestroy (images);
287
288 return cursor;
289}
290
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400291void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400292dump_property(struct weston_wm *wm,
293 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400294{
295 int32_t *incr_value;
296 const char *text_value, *name;
297 xcb_atom_t *atom_value;
298 int width, len;
299 uint32_t i;
300
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400301 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400302 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400303 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400304 return;
305 }
306
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400307 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
308 get_atom_name(wm->conn, reply->type),
309 reply->format,
310 xcb_get_property_value_length(reply),
311 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400312
313 if (reply->type == wm->atom.incr) {
314 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400315 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400316 } else if (reply->type == wm->atom.utf8_string ||
317 reply->type == wm->atom.string) {
318 text_value = xcb_get_property_value(reply);
319 if (reply->value_len > 40)
320 len = 40;
321 else
322 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400323 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400324 } else if (reply->type == XCB_ATOM_ATOM) {
325 atom_value = xcb_get_property_value(reply);
326 for (i = 0; i < reply->value_len; i++) {
327 name = get_atom_name(wm->conn, atom_value[i]);
328 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400329 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400330 width = 4;
331 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400332 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400333 }
334
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400335 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400336 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400337 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400338 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400339 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400340 }
341}
342
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200343static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400344read_and_dump_property(struct weston_wm *wm,
345 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400347 xcb_get_property_reply_t *reply;
348 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400349
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400350 cookie = xcb_get_property(wm->conn, 0, window,
351 property, XCB_ATOM_ANY, 0, 2048);
352 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400353
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400354 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400355
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400356 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357}
358
359/* We reuse some predefined, but otherwise useles atoms */
360#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
361#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500362#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700363#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400364
365static void
366weston_wm_window_read_properties(struct weston_wm_window *window)
367{
368 struct weston_wm *wm = window->wm;
369
370#define F(field) offsetof(struct weston_wm_window, field)
371 const struct {
372 xcb_atom_t atom;
373 xcb_atom_t type;
374 int offset;
375 } props[] = {
376 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
377 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
378 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
379 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700380 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500381 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400382 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
383 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300384 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300386 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400387 };
388#undef F
389
390 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
391 xcb_get_property_reply_t *reply;
392 void *p;
393 uint32_t *xid;
394 xcb_atom_t *atom;
395 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400396
397 if (!window->properties_dirty)
398 return;
399 window->properties_dirty = 0;
400
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400401 for (i = 0; i < ARRAY_LENGTH(props); i++)
402 cookie[i] = xcb_get_property(wm->conn,
403 0, /* delete */
404 window->id,
405 props[i].atom,
406 XCB_ATOM_ANY, 0, 2048);
407
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300408 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700409 window->size_hints.flags = 0;
410 window->motif_hints.flags = 0;
411
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400412 for (i = 0; i < ARRAY_LENGTH(props); i++) {
413 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
414 if (!reply)
415 /* Bad window, typically */
416 continue;
417 if (reply->type == XCB_ATOM_NONE) {
418 /* No such property */
419 free(reply);
420 continue;
421 }
422
423 p = ((char *) window + props[i].offset);
424
425 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300426 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400427 case XCB_ATOM_STRING:
428 /* FIXME: We're using this for both string and
429 utf8_string */
430 if (*(char **) p)
431 free(*(char **) p);
432
433 *(char **) p =
434 strndup(xcb_get_property_value(reply),
435 xcb_get_property_value_length(reply));
436 break;
437 case XCB_ATOM_WINDOW:
438 xid = xcb_get_property_value(reply);
439 *(struct weston_wm_window **) p =
440 hash_table_lookup(wm->window_hash, *xid);
441 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300442 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400443 case XCB_ATOM_ATOM:
444 atom = xcb_get_property_value(reply);
445 *(xcb_atom_t *) p = *atom;
446 break;
447 case TYPE_WM_PROTOCOLS:
448 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700449 case TYPE_WM_NORMAL_HINTS:
450 memcpy(&window->size_hints,
451 xcb_get_property_value(reply),
452 sizeof window->size_hints);
453 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500454 case TYPE_NET_WM_STATE:
455 window->fullscreen = 0;
456 atom = xcb_get_property_value(reply);
457 for (i = 0; i < reply->value_len; i++)
458 if (atom[i] == wm->atom.net_wm_state_fullscreen)
459 window->fullscreen = 1;
460 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400461 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700462 memcpy(&window->motif_hints,
463 xcb_get_property_value(reply),
464 sizeof window->motif_hints);
465 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
466 window->decorate =
467 window->motif_hints.decorations > 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400468 break;
469 default:
470 break;
471 }
472 free(reply);
473 }
474}
475
476static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400477weston_wm_window_get_frame_size(struct weston_wm_window *window,
478 int *width, int *height)
479{
480 struct theme *t = window->wm->theme;
481
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500482 if (window->fullscreen) {
483 *width = window->width;
484 *height = window->height;
485 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400486 *width = window->width + (t->margin + t->width) * 2;
487 *height = window->height +
488 t->margin * 2 + t->width + t->titlebar_height;
489 } else {
490 *width = window->width + t->margin * 2;
491 *height = window->height + t->margin * 2;
492 }
493}
494
495static void
496weston_wm_window_get_child_position(struct weston_wm_window *window,
497 int *x, int *y)
498{
499 struct theme *t = window->wm->theme;
500
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500501 if (window->fullscreen) {
502 *x = 0;
503 *y = 0;
504 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400505 *x = t->margin + t->width;
506 *y = t->margin + t->titlebar_height;
507 } else {
508 *x = t->margin;
509 *y = t->margin;
510 }
511}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400512
513static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500514weston_wm_window_send_configure_notify(struct weston_wm_window *window)
515{
516 xcb_configure_notify_event_t configure_notify;
517 struct weston_wm *wm = window->wm;
518 int x, y;
519
520 weston_wm_window_get_child_position(window, &x, &y);
521 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
522 configure_notify.pad0 = 0;
523 configure_notify.event = window->id;
524 configure_notify.window = window->id;
525 configure_notify.above_sibling = XCB_WINDOW_NONE;
526 configure_notify.x = x;
527 configure_notify.y = y;
528 configure_notify.width = window->width;
529 configure_notify.height = window->height;
530 configure_notify.border_width = 0;
531 configure_notify.override_redirect = 0;
532 configure_notify.pad1 = 0;
533
534 xcb_send_event(wm->conn, 0, window->id,
535 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
536 (char *) &configure_notify);
537}
538
539static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400540weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
541{
542 xcb_configure_request_event_t *configure_request =
543 (xcb_configure_request_event_t *) event;
544 struct weston_wm_window *window;
545 uint32_t mask, values[16];
546 int x, y, width, height, i = 0;
547
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400548 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
549 configure_request->window,
550 configure_request->x, configure_request->y,
551 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400552
553 window = hash_table_lookup(wm->window_hash, configure_request->window);
554
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500555 if (window->fullscreen) {
556 weston_wm_window_send_configure_notify(window);
557 return;
558 }
559
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400560 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
561 window->width = configure_request->width;
562 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
563 window->height = configure_request->height;
564
565 weston_wm_window_get_child_position(window, &x, &y);
566 values[i++] = x;
567 values[i++] = y;
568 values[i++] = window->width;
569 values[i++] = window->height;
570 values[i++] = 0;
571 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
572 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
573 XCB_CONFIG_WINDOW_BORDER_WIDTH;
574 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
575 values[i++] = configure_request->sibling;
576 mask |= XCB_CONFIG_WINDOW_SIBLING;
577 }
578 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
579 values[i++] = configure_request->stack_mode;
580 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
581 }
582
583 xcb_configure_window(wm->conn, window->id, mask, values);
584
585 weston_wm_window_get_frame_size(window, &width, &height);
586 values[0] = width;
587 values[1] = height;
588 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
589 xcb_configure_window(wm->conn, window->frame_id, mask, values);
590
591 weston_wm_window_schedule_repaint(window);
592}
593
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400594static int
595our_resource(struct weston_wm *wm, uint32_t id)
596{
597 const xcb_setup_t *setup;
598
599 setup = xcb_get_setup(wm->conn);
600
601 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
602}
603
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400604static void
605weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
606{
607 xcb_configure_notify_event_t *configure_notify =
608 (xcb_configure_notify_event_t *) event;
609 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300610 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400611
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400612 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400613 configure_notify->window,
614 configure_notify->x, configure_notify->y,
615 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300616
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400617 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300618 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700619 window->x = configure_notify->x;
620 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700621 if (window->override_redirect) {
622 window->width = configure_notify->width;
623 window->height = configure_notify->height;
624 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400625}
626
627static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300628weston_wm_kill_client(struct wl_listener *listener, void *data)
629{
630 struct weston_surface *surface = data;
631 struct weston_wm_window *window = get_wm_window(surface);
632 char name[1024];
633
634 if (!window)
635 return;
636
637 gethostname(name, 1024);
638
639 /* this is only one heuristic to guess the PID of a client is valid,
640 * assuming it's compliant with icccm and ewmh. Non-compliants and
641 * remote applications of course fail. */
642 if (!strcmp(window->machine, name) && window->pid != 0)
643 kill(window->pid, SIGKILL);
644}
645
646static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400647weston_wm_window_activate(struct wl_listener *listener, void *data)
648{
649 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200650 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600651 struct weston_wm *wm =
652 container_of(listener, struct weston_wm, activate_listener);
653 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400654
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200655 if (surface) {
656 window = get_wm_window(surface);
657 }
658
Scott Moreau85ecac02012-05-21 15:49:14 -0600659 if (window) {
660 client_message.response_type = XCB_CLIENT_MESSAGE;
661 client_message.format = 32;
662 client_message.window = window->id;
663 client_message.type = wm->atom.wm_protocols;
664 client_message.data.data32[0] = wm->atom.wm_take_focus;
665 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
666
667 xcb_send_event(wm->conn, 0, window->id,
668 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
669 (char *) &client_message);
670
671 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
672 window->id, XCB_TIME_CURRENT_TIME);
673 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400674 xcb_set_input_focus (wm->conn,
675 XCB_INPUT_FOCUS_POINTER_ROOT,
676 XCB_NONE,
677 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600678 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400679
680 if (wm->focus_window)
681 weston_wm_window_schedule_repaint(wm->focus_window);
682 wm->focus_window = window;
683 if (wm->focus_window)
684 weston_wm_window_schedule_repaint(wm->focus_window);
685}
686
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300687static void
688weston_wm_window_transform(struct wl_listener *listener, void *data)
689{
690 struct weston_surface *surface = data;
691 struct weston_wm_window *window = get_wm_window(surface);
692 struct weston_wm *wm =
693 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300694 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300695
696 if (!window || !wm)
697 return;
698
699 if (!weston_surface_is_mapped(surface))
700 return;
701
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700702 if (window->x != surface->geometry.x ||
703 window->y != surface->geometry.y) {
704 values[0] = surface->geometry.x;
705 values[1] = surface->geometry.y;
706 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300707
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700708 xcb_configure_window(wm->conn, window->frame_id, mask, values);
709 xcb_flush(wm->conn);
710 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300711}
712
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400713#define ICCCM_WITHDRAWN_STATE 0
714#define ICCCM_NORMAL_STATE 1
715#define ICCCM_ICONIC_STATE 3
716
717static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500718weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400719{
720 struct weston_wm *wm = window->wm;
721 uint32_t property[2];
722
723 property[0] = state;
724 property[1] = XCB_WINDOW_NONE;
725
726 xcb_change_property(wm->conn,
727 XCB_PROP_MODE_REPLACE,
728 window->id,
729 wm->atom.wm_state,
730 wm->atom.wm_state,
731 32, /* format */
732 2, property);
733}
734
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400735static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500736weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
737{
738 struct weston_wm *wm = window->wm;
739 uint32_t property[1];
740 int i;
741
742 i = 0;
743 if (window->fullscreen)
744 property[i++] = wm->atom.net_wm_state_fullscreen;
745
746 xcb_change_property(wm->conn,
747 XCB_PROP_MODE_REPLACE,
748 window->id,
749 wm->atom.net_wm_state,
750 XCB_ATOM_ATOM,
751 32, /* format */
752 i, property);
753}
754
755static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700756weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400757{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700758 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400759 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400760 int x, y, width, height;
761
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400762 weston_wm_window_get_frame_size(window, &width, &height);
763 weston_wm_window_get_child_position(window, &x, &y);
764
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400765 values[0] = wm->screen->black_pixel;
766 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400767 XCB_EVENT_MASK_KEY_PRESS |
768 XCB_EVENT_MASK_KEY_RELEASE |
769 XCB_EVENT_MASK_BUTTON_PRESS |
770 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400771 XCB_EVENT_MASK_POINTER_MOTION |
772 XCB_EVENT_MASK_ENTER_WINDOW |
773 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400774 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400775 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400776 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400777
778 window->frame_id = xcb_generate_id(wm->conn);
779 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400780 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400781 window->frame_id,
782 wm->screen->root,
783 0, 0,
784 width, height,
785 0,
786 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400787 wm->visual_id,
788 XCB_CW_BORDER_PIXEL |
789 XCB_CW_EVENT_MASK |
790 XCB_CW_COLORMAP, values);
791
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400792 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
793
794 values[0] = 0;
795 xcb_configure_window(wm->conn, window->id,
796 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
797
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400798 window->cairo_surface =
799 cairo_xcb_surface_create_with_xrender_format(wm->conn,
800 wm->screen,
801 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400802 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400803 width, height);
804
805 hash_table_insert(wm->window_hash, window->frame_id, window);
806}
807
808static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700809weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
810{
811 xcb_map_request_event_t *map_request =
812 (xcb_map_request_event_t *) event;
813 struct weston_wm_window *window;
814
815 if (our_resource(wm, map_request->window)) {
816 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
817 map_request->window);
818 return;
819 }
820
821 window = hash_table_lookup(wm->window_hash, map_request->window);
822
823 weston_wm_window_read_properties(window);
824
825 if (window->frame_id == XCB_WINDOW_NONE)
826 weston_wm_window_create_frame(window);
827
828 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
829 window->id, window, window->frame_id);
830
831 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
832 weston_wm_window_set_net_wm_state(window);
833
834 xcb_map_window(wm->conn, map_request->window);
835 xcb_map_window(wm->conn, window->frame_id);
836}
837
838static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
840{
841 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
842
843 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400844 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
845 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400846 return;
847 }
848
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400849 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400850}
851
852static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400853weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
854{
855 xcb_unmap_notify_event_t *unmap_notify =
856 (xcb_unmap_notify_event_t *) event;
857 struct weston_wm_window *window;
858
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400859 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
860 unmap_notify->window,
861 unmap_notify->event,
862 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400863
864 if (our_resource(wm, unmap_notify->window))
865 return;
866
MoD31700122013-06-11 19:58:55 -0500867 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400868 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
869 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400870 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400871
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400872 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400873 if (wm->focus_window == window)
874 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400875 if (window->surface)
876 wl_list_remove(&window->surface_destroy_listener.link);
877 window->surface = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700878 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400879}
880
881static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400882weston_wm_window_draw_decoration(void *data)
883{
884 struct weston_wm_window *window = data;
885 struct weston_wm *wm = window->wm;
886 struct theme *t = wm->theme;
887 cairo_t *cr;
888 int x, y, width, height;
889 const char *title;
890 uint32_t flags = 0;
891
892 weston_wm_window_read_properties(window);
893
894 window->repaint_source = NULL;
895
896 weston_wm_window_get_frame_size(window, &width, &height);
897 weston_wm_window_get_child_position(window, &x, &y);
898
899 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
900 cr = cairo_create(window->cairo_surface);
901
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500902 if (window->fullscreen) {
903 /* nothing */
904 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400905 if (wm->focus_window == window)
906 flags |= THEME_FRAME_ACTIVE;
907
908 if (window->name)
909 title = window->name;
910 else
911 title = "untitled";
912
913 theme_render_frame(t, cr, width, height, title, flags);
914 } else {
915 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
916 cairo_set_source_rgba(cr, 0, 0, 0, 0);
917 cairo_paint(cr);
918
919 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
920 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
921 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
922 }
923
924 cairo_destroy(cr);
925
926 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700927 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500928 if(window->has_alpha) {
929 pixman_region32_init(&window->surface->pending.opaque);
930 } else {
931 /* We leave an extra pixel around the X window area to
932 * make sure we don't sample from the undefined alpha
933 * channel when filtering. */
934 pixman_region32_init_rect(&window->surface->pending.opaque,
935 x - 1, y - 1,
936 window->width + 2,
937 window->height + 2);
938 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200939 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500940 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400941
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500942 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500943 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500944 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400945 t->margin, t->margin,
946 width - 2 * t->margin,
947 height - 2 * t->margin);
948 }
949}
950
951static void
952weston_wm_window_schedule_repaint(struct weston_wm_window *window)
953{
954 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300955 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400956
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400957 if (window->frame_id == XCB_WINDOW_NONE) {
958 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300959 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700960 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500961 if(window->has_alpha) {
962 pixman_region32_init(&window->surface->pending.opaque);
963 } else {
964 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
965 width, height);
966 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200967 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400968 }
969 return;
970 }
971
972 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400973 return;
974
975 window->repaint_source =
976 wl_event_loop_add_idle(wm->server->loop,
977 weston_wm_window_draw_decoration,
978 window);
979}
980
981static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400982weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
983{
984 xcb_property_notify_event_t *property_notify =
985 (xcb_property_notify_event_t *) event;
986 struct weston_wm_window *window;
987
988 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000989 if (!window)
990 return;
991
992 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400993
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400994 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400995 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400996 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400997 else
998 read_and_dump_property(wm, property_notify->window,
999 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001000
1001 if (property_notify->atom == wm->atom.net_wm_name ||
1002 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001003 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001004}
1005
1006static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001007weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001008 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001009{
1010 struct weston_wm_window *window;
1011 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001012 xcb_get_geometry_cookie_t geometry_cookie;
1013 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001014
Peter Huttererf3d62272013-08-08 11:57:05 +10001015 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001016 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001017 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001018 return;
1019 }
1020
MoD384a11a2013-06-22 11:04:21 -05001021 geometry_cookie = xcb_get_geometry(wm->conn, id);
1022
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001023 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1024 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1025
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001026 window->wm = wm;
1027 window->id = id;
1028 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001029 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001030 window->width = width;
1031 window->height = height;
1032
MoD384a11a2013-06-22 11:04:21 -05001033 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1034 /* technically we should use XRender and check the visual format's
1035 alpha_mask, but checking depth is simpler and works in all known cases */
1036 if(geometry_reply != NULL)
1037 window->has_alpha = geometry_reply->depth == 32;
1038 free(geometry_reply);
1039
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001040 hash_table_insert(wm->window_hash, id, window);
1041}
1042
1043static void
1044weston_wm_window_destroy(struct weston_wm_window *window)
1045{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001046 struct weston_wm *wm = window->wm;
1047
1048 if (window->repaint_source)
1049 wl_event_source_remove(window->repaint_source);
1050 if (window->cairo_surface)
1051 cairo_surface_destroy(window->cairo_surface);
1052
1053 if (window->frame_id) {
1054 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1055 xcb_destroy_window(wm->conn, window->frame_id);
1056 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1057 hash_table_remove(wm->window_hash, window->frame_id);
1058 window->frame_id = XCB_WINDOW_NONE;
1059 }
1060
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001061 hash_table_remove(window->wm->window_hash, window->id);
1062 free(window);
1063}
1064
1065static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001066weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1067{
1068 xcb_create_notify_event_t *create_notify =
1069 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001070
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001071 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1072 create_notify->window,
1073 create_notify->width, create_notify->height,
1074 create_notify->override_redirect ? ", override" : "",
1075 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001076
1077 if (our_resource(wm, create_notify->window))
1078 return;
1079
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001080 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001081 create_notify->width, create_notify->height,
1082 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001083}
1084
1085static void
1086weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1087{
1088 xcb_destroy_notify_event_t *destroy_notify =
1089 (xcb_destroy_notify_event_t *) event;
1090 struct weston_wm_window *window;
1091
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001092 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1093 destroy_notify->window,
1094 destroy_notify->event,
1095 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001096
1097 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001098 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001099
1100 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001101 weston_wm_window_destroy(window);
1102}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001103
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001104static void
1105weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1106{
1107 xcb_reparent_notify_event_t *reparent_notify =
1108 (xcb_reparent_notify_event_t *) event;
1109 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001110
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001111 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1112 reparent_notify->window,
1113 reparent_notify->parent,
1114 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001115
1116 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001117 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1118 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001119 } else if (!our_resource(wm, reparent_notify->parent)) {
1120 window = hash_table_lookup(wm->window_hash,
1121 reparent_notify->window);
1122 weston_wm_window_destroy(window);
1123 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001124}
1125
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001126struct weston_seat *
1127weston_wm_pick_seat(struct weston_wm *wm)
1128{
1129 return container_of(wm->server->compositor->seat_list.next,
1130 struct weston_seat, link);
1131}
1132
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001133static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001134weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1135 xcb_client_message_event_t *client_message)
1136{
1137 static const int map[] = {
1138 THEME_LOCATION_RESIZING_TOP_LEFT,
1139 THEME_LOCATION_RESIZING_TOP,
1140 THEME_LOCATION_RESIZING_TOP_RIGHT,
1141 THEME_LOCATION_RESIZING_RIGHT,
1142 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1143 THEME_LOCATION_RESIZING_BOTTOM,
1144 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1145 THEME_LOCATION_RESIZING_LEFT
1146 };
1147
1148 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001149 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001150 int detail;
1151 struct weston_shell_interface *shell_interface =
1152 &wm->server->compositor->shell_interface;
1153
Kristian Høgsberge3148752013-05-06 23:19:49 -04001154 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001155 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001156 return;
1157
1158 detail = client_message->data.data32[2];
1159 switch (detail) {
1160 case _NET_WM_MOVERESIZE_MOVE:
1161 shell_interface->move(window->shsurf, seat);
1162 break;
1163 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1164 case _NET_WM_MOVERESIZE_SIZE_TOP:
1165 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1166 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1167 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1168 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1169 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1170 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1171 shell_interface->resize(window->shsurf, seat, map[detail]);
1172 break;
1173 case _NET_WM_MOVERESIZE_CANCEL:
1174 break;
1175 }
1176}
1177
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001178#define _NET_WM_STATE_REMOVE 0
1179#define _NET_WM_STATE_ADD 1
1180#define _NET_WM_STATE_TOGGLE 2
1181
1182static int
1183update_state(int action, int *state)
1184{
1185 int new_state, changed;
1186
1187 switch (action) {
1188 case _NET_WM_STATE_REMOVE:
1189 new_state = 0;
1190 break;
1191 case _NET_WM_STATE_ADD:
1192 new_state = 1;
1193 break;
1194 case _NET_WM_STATE_TOGGLE:
1195 new_state = !*state;
1196 break;
1197 default:
1198 return 0;
1199 }
1200
1201 changed = (*state != new_state);
1202 *state = new_state;
1203
1204 return changed;
1205}
1206
1207static void
1208weston_wm_window_configure(void *data);
1209
1210static void
1211weston_wm_window_handle_state(struct weston_wm_window *window,
1212 xcb_client_message_event_t *client_message)
1213{
1214 struct weston_wm *wm = window->wm;
1215 struct weston_shell_interface *shell_interface =
1216 &wm->server->compositor->shell_interface;
1217 uint32_t action, property;
1218
1219 action = client_message->data.data32[0];
1220 property = client_message->data.data32[1];
1221
1222 if (property == wm->atom.net_wm_state_fullscreen &&
1223 update_state(action, &window->fullscreen)) {
1224 weston_wm_window_set_net_wm_state(window);
1225 if (window->fullscreen) {
1226 window->saved_width = window->width;
1227 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001228
1229 if (window->shsurf)
1230 shell_interface->set_fullscreen(window->shsurf,
1231 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1232 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001233 } else {
1234 shell_interface->set_toplevel(window->shsurf);
1235 window->width = window->saved_width;
1236 window->height = window->saved_height;
1237 weston_wm_window_configure(window);
1238 }
1239 }
1240}
1241
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001242static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001243weston_wm_handle_client_message(struct weston_wm *wm,
1244 xcb_generic_event_t *event)
1245{
1246 xcb_client_message_event_t *client_message =
1247 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001248 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001249
1250 window = hash_table_lookup(wm->window_hash, client_message->window);
1251
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001252 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1253 get_atom_name(wm->conn, client_message->type),
1254 client_message->data.data32[0],
1255 client_message->data.data32[1],
1256 client_message->data.data32[2],
1257 client_message->data.data32[3],
1258 client_message->data.data32[4],
1259 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001260
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001261 if (client_message->type == wm->atom.net_wm_moveresize)
1262 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001263 else if (client_message->type == wm->atom.net_wm_state)
1264 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001265}
1266
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001267enum cursor_type {
1268 XWM_CURSOR_TOP,
1269 XWM_CURSOR_BOTTOM,
1270 XWM_CURSOR_LEFT,
1271 XWM_CURSOR_RIGHT,
1272 XWM_CURSOR_TOP_LEFT,
1273 XWM_CURSOR_TOP_RIGHT,
1274 XWM_CURSOR_BOTTOM_LEFT,
1275 XWM_CURSOR_BOTTOM_RIGHT,
1276 XWM_CURSOR_LEFT_PTR,
1277};
1278
1279static const char *cursors[] = {
1280 "top_side",
1281 "bottom_side",
1282 "left_side",
1283 "right_side",
1284 "top_left_corner",
1285 "top_right_corner",
1286 "bottom_left_corner",
1287 "bottom_right_corner",
1288 "left_ptr"
1289};
1290
1291static void
1292weston_wm_create_cursors(struct weston_wm *wm)
1293{
1294 int i, count = ARRAY_LENGTH(cursors);
1295
1296 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1297 for (i = 0; i < count; i++) {
1298 wm->cursors[i] =
1299 xcb_cursor_library_load_cursor(wm, cursors[i]);
1300 }
1301
1302 wm->last_cursor = -1;
1303}
1304
1305static void
1306weston_wm_destroy_cursors(struct weston_wm *wm)
1307{
1308 uint8_t i;
1309
1310 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1311 xcb_free_cursor(wm->conn, wm->cursors[i]);
1312
1313 free(wm->cursors);
1314}
1315
1316static int
1317get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1318{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001319 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001320
1321 switch (location) {
1322 case THEME_LOCATION_RESIZING_TOP:
1323 return XWM_CURSOR_TOP;
1324 case THEME_LOCATION_RESIZING_BOTTOM:
1325 return XWM_CURSOR_BOTTOM;
1326 case THEME_LOCATION_RESIZING_LEFT:
1327 return XWM_CURSOR_LEFT;
1328 case THEME_LOCATION_RESIZING_RIGHT:
1329 return XWM_CURSOR_RIGHT;
1330 case THEME_LOCATION_RESIZING_TOP_LEFT:
1331 return XWM_CURSOR_TOP_LEFT;
1332 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1333 return XWM_CURSOR_TOP_RIGHT;
1334 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1335 return XWM_CURSOR_BOTTOM_LEFT;
1336 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1337 return XWM_CURSOR_BOTTOM_RIGHT;
1338 case THEME_LOCATION_EXTERIOR:
1339 case THEME_LOCATION_TITLEBAR:
1340 default:
1341 return XWM_CURSOR_LEFT_PTR;
1342 }
1343}
1344
1345static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001346weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1347 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001348{
1349 uint32_t cursor_value_list;
1350
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001351 if (wm->last_cursor == cursor)
1352 return;
1353
1354 wm->last_cursor = cursor;
1355
1356 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001357 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001358 XCB_CW_CURSOR, &cursor_value_list);
1359 xcb_flush(wm->conn);
1360}
1361
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001362static void
1363weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1364{
1365 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1366 struct weston_shell_interface *shell_interface =
1367 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001368 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001369 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001370 enum theme_location location;
1371 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001372 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001373
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001374 wm_log("XCB_BUTTON_%s (detail %d)\n",
1375 button->response_type == XCB_BUTTON_PRESS ?
1376 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001377
1378 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001379 weston_wm_window_get_frame_size(window, &width, &height);
1380
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001381 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001382 button->detail == 1) {
1383 location = theme_get_location(t,
1384 button->event_x,
1385 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001386 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001387
1388 switch (location) {
1389 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001390 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001391 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001392 case THEME_LOCATION_RESIZING_TOP:
1393 case THEME_LOCATION_RESIZING_BOTTOM:
1394 case THEME_LOCATION_RESIZING_LEFT:
1395 case THEME_LOCATION_RESIZING_RIGHT:
1396 case THEME_LOCATION_RESIZING_TOP_LEFT:
1397 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1398 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1399 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1400 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001401 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001402 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001403 default:
1404 break;
1405 }
1406 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001407}
1408
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001409static void
1410weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1411{
1412 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1413 struct weston_wm_window *window;
1414 int cursor, width, height;
1415
1416 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001417 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001418 return;
1419
1420 weston_wm_window_get_frame_size(window, &width, &height);
1421 cursor = get_cursor_for_location(wm->theme, width, height,
1422 motion->event_x, motion->event_y);
1423
Tiago Vignattic1903232012-07-16 12:15:37 -04001424 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001425}
1426
1427static void
1428weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1429{
1430 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1431 struct weston_wm_window *window;
1432 int cursor, width, height;
1433
1434 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001435 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001436 return;
1437
1438 weston_wm_window_get_frame_size(window, &width, &height);
1439 cursor = get_cursor_for_location(wm->theme, width, height,
1440 enter->event_x, enter->event_y);
1441
Tiago Vignattic1903232012-07-16 12:15:37 -04001442 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001443}
1444
1445static void
1446weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1447{
1448 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1449 struct weston_wm_window *window;
1450
1451 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001452 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001453 return;
1454
Tiago Vignattic1903232012-07-16 12:15:37 -04001455 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001456}
1457
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001458static int
1459weston_wm_handle_event(int fd, uint32_t mask, void *data)
1460{
1461 struct weston_wm *wm = data;
1462 xcb_generic_event_t *event;
1463 int count = 0;
1464
1465 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1466 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001467 free(event);
1468 count++;
1469 continue;
1470 }
1471
MoD31700122013-06-11 19:58:55 -05001472 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001473 case XCB_BUTTON_PRESS:
1474 case XCB_BUTTON_RELEASE:
1475 weston_wm_handle_button(wm, event);
1476 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001477 case XCB_ENTER_NOTIFY:
1478 weston_wm_handle_enter(wm, event);
1479 break;
1480 case XCB_LEAVE_NOTIFY:
1481 weston_wm_handle_leave(wm, event);
1482 break;
1483 case XCB_MOTION_NOTIFY:
1484 weston_wm_handle_motion(wm, event);
1485 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001486 case XCB_CREATE_NOTIFY:
1487 weston_wm_handle_create_notify(wm, event);
1488 break;
1489 case XCB_MAP_REQUEST:
1490 weston_wm_handle_map_request(wm, event);
1491 break;
1492 case XCB_MAP_NOTIFY:
1493 weston_wm_handle_map_notify(wm, event);
1494 break;
1495 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001496 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001497 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001498 case XCB_REPARENT_NOTIFY:
1499 weston_wm_handle_reparent_notify(wm, event);
1500 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001501 case XCB_CONFIGURE_REQUEST:
1502 weston_wm_handle_configure_request(wm, event);
1503 break;
1504 case XCB_CONFIGURE_NOTIFY:
1505 weston_wm_handle_configure_notify(wm, event);
1506 break;
1507 case XCB_DESTROY_NOTIFY:
1508 weston_wm_handle_destroy_notify(wm, event);
1509 break;
1510 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001511 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001512 break;
1513 case XCB_PROPERTY_NOTIFY:
1514 weston_wm_handle_property_notify(wm, event);
1515 break;
1516 case XCB_CLIENT_MESSAGE:
1517 weston_wm_handle_client_message(wm, event);
1518 break;
1519 }
1520
1521 free(event);
1522 count++;
1523 }
1524
1525 xcb_flush(wm->conn);
1526
1527 return count;
1528}
1529
1530static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001531weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1532{
1533 xcb_depth_iterator_t d_iter;
1534 xcb_visualtype_iterator_t vt_iter;
1535 xcb_visualtype_t *visualtype;
1536
1537 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1538 visualtype = NULL;
1539 while (d_iter.rem > 0) {
1540 if (d_iter.data->depth == 32) {
1541 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1542 visualtype = vt_iter.data;
1543 break;
1544 }
1545
1546 xcb_depth_next(&d_iter);
1547 }
1548
1549 if (visualtype == NULL) {
1550 weston_log("no 32 bit visualtype\n");
1551 return;
1552 }
1553
1554 wm->visual_id = visualtype->visual_id;
1555 wm->colormap = xcb_generate_id(wm->conn);
1556 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1557 wm->colormap, wm->screen->root, wm->visual_id);
1558}
1559
1560static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001561weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001562{
1563
1564#define F(field) offsetof(struct weston_wm, field)
1565
1566 static const struct { const char *name; int offset; } atoms[] = {
1567 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001568 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001569 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1570 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001571 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001572 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001573 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001574 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001575 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001576 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001577 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1578 { "_NET_WM_STATE", F(atom.net_wm_state) },
1579 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1580 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1581 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1582 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1583
1584 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1585 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1586 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1587 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1588 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1589 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1590 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001591 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1592 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001593 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1594 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1595 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1596 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1597 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1598
1599 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1600 { "_NET_SUPPORTING_WM_CHECK",
1601 F(atom.net_supporting_wm_check) },
1602 { "_NET_SUPPORTED", F(atom.net_supported) },
1603 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1604 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001605 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001606 { "TARGETS", F(atom.targets) },
1607 { "UTF8_STRING", F(atom.utf8_string) },
1608 { "_WL_SELECTION", F(atom.wl_selection) },
1609 { "INCR", F(atom.incr) },
1610 { "TIMESTAMP", F(atom.timestamp) },
1611 { "MULTIPLE", F(atom.multiple) },
1612 { "UTF8_STRING" , F(atom.utf8_string) },
1613 { "COMPOUND_TEXT", F(atom.compound_text) },
1614 { "TEXT", F(atom.text) },
1615 { "STRING", F(atom.string) },
1616 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1617 { "text/plain", F(atom.text_plain) },
1618 };
1619#undef F
1620
1621 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1622 xcb_xfixes_query_version_reply_t *xfixes_reply;
1623 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1624 xcb_intern_atom_reply_t *reply;
1625 xcb_render_query_pict_formats_reply_t *formats_reply;
1626 xcb_render_query_pict_formats_cookie_t formats_cookie;
1627 xcb_render_pictforminfo_t *formats;
1628 uint32_t i;
1629
1630 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1631
1632 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1633
1634 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1635 cookies[i] = xcb_intern_atom (wm->conn, 0,
1636 strlen(atoms[i].name),
1637 atoms[i].name);
1638
1639 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1640 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1641 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1642 free(reply);
1643 }
1644
1645 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1646 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001647 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001648
1649 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1650 XCB_XFIXES_MAJOR_VERSION,
1651 XCB_XFIXES_MINOR_VERSION);
1652 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1653 xfixes_cookie, NULL);
1654
Martin Minarik6d118362012-06-07 18:01:59 +02001655 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001656 xfixes_reply->major_version, xfixes_reply->minor_version);
1657
1658 free(xfixes_reply);
1659
1660 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1661 formats_cookie, 0);
1662 if (formats_reply == NULL)
1663 return;
1664
1665 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001666 for (i = 0; i < formats_reply->num_formats; i++) {
1667 if (formats[i].direct.red_mask != 0xff &&
1668 formats[i].direct.red_shift != 16)
1669 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001670 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1671 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001672 wm->format_rgb = formats[i];
1673 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1674 formats[i].depth == 32 &&
1675 formats[i].direct.alpha_mask == 0xff &&
1676 formats[i].direct.alpha_shift == 24)
1677 wm->format_rgba = formats[i];
1678 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001679
1680 free(formats_reply);
1681}
1682
1683static void
1684weston_wm_create_wm_window(struct weston_wm *wm)
1685{
1686 static const char name[] = "Weston WM";
1687
1688 wm->wm_window = xcb_generate_id(wm->conn);
1689 xcb_create_window(wm->conn,
1690 XCB_COPY_FROM_PARENT,
1691 wm->wm_window,
1692 wm->screen->root,
1693 0, 0,
1694 10, 10,
1695 0,
1696 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1697 wm->screen->root_visual,
1698 0, NULL);
1699
1700 xcb_change_property(wm->conn,
1701 XCB_PROP_MODE_REPLACE,
1702 wm->wm_window,
1703 wm->atom.net_supporting_wm_check,
1704 XCB_ATOM_WINDOW,
1705 32, /* format */
1706 1, &wm->wm_window);
1707
1708 xcb_change_property(wm->conn,
1709 XCB_PROP_MODE_REPLACE,
1710 wm->wm_window,
1711 wm->atom.net_wm_name,
1712 wm->atom.utf8_string,
1713 8, /* format */
1714 strlen(name), name);
1715
1716 xcb_change_property(wm->conn,
1717 XCB_PROP_MODE_REPLACE,
1718 wm->screen->root,
1719 wm->atom.net_supporting_wm_check,
1720 XCB_ATOM_WINDOW,
1721 32, /* format */
1722 1, &wm->wm_window);
1723
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001724 /* Claim the WM_S0 selection even though we don't suport
1725 * the --replace functionality. */
1726 xcb_set_selection_owner(wm->conn,
1727 wm->wm_window,
1728 wm->atom.wm_s0,
1729 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001730
1731 xcb_set_selection_owner(wm->conn,
1732 wm->wm_window,
1733 wm->atom.net_wm_cm_s0,
1734 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001735}
1736
1737struct weston_wm *
1738weston_wm_create(struct weston_xserver *wxs)
1739{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001740 struct weston_wm *wm;
1741 struct wl_event_loop *loop;
1742 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001743 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001744 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001745 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001746
Peter Huttererf3d62272013-08-08 11:57:05 +10001747 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001748 if (wm == NULL)
1749 return NULL;
1750
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001751 wm->server = wxs;
1752 wm->window_hash = hash_table_create();
1753 if (wm->window_hash == NULL) {
1754 free(wm);
1755 return NULL;
1756 }
1757
1758 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001759 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760 hash_table_destroy(wm->window_hash);
1761 free(wm);
1762 return NULL;
1763 }
1764
1765 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001766 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001767 close(sv[1]);
1768
1769 /* xcb_connect_to_fd takes ownership of the fd. */
1770 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1771 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001772 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001773 close(sv[0]);
1774 hash_table_destroy(wm->window_hash);
1775 free(wm);
1776 return NULL;
1777 }
1778
1779 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1780 wm->screen = s.data;
1781
1782 loop = wl_display_get_event_loop(wxs->wl_display);
1783 wm->source =
1784 wl_event_loop_add_fd(loop, sv[0],
1785 WL_EVENT_READABLE,
1786 weston_wm_handle_event, wm);
1787 wl_event_source_check(wm->source);
1788
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001789 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001790 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001791
1792 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001793 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1794 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1795 XCB_EVENT_MASK_PROPERTY_CHANGE;
1796 xcb_change_window_attributes(wm->conn, wm->screen->root,
1797 XCB_CW_EVENT_MASK, values);
1798 wm->theme = theme_create();
1799
1800 weston_wm_create_wm_window(wm);
1801
1802 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001803 supported[1] = wm->atom.net_wm_state;
1804 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001805 xcb_change_property(wm->conn,
1806 XCB_PROP_MODE_REPLACE,
1807 wm->screen->root,
1808 wm->atom.net_supported,
1809 XCB_ATOM_ATOM,
1810 32, /* format */
1811 ARRAY_LENGTH(supported), supported);
1812
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001813 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001814
1815 xcb_flush(wm->conn);
1816
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001817 wm->activate_listener.notify = weston_wm_window_activate;
1818 wl_signal_add(&wxs->compositor->activate_signal,
1819 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001820 wm->transform_listener.notify = weston_wm_window_transform;
1821 wl_signal_add(&wxs->compositor->transform_signal,
1822 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001823 wm->kill_listener.notify = weston_wm_kill_client;
1824 wl_signal_add(&wxs->compositor->kill_signal,
1825 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001826
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001827 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001828 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001829
Martin Minarik6d118362012-06-07 18:01:59 +02001830 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001831
1832 return wm;
1833}
1834
1835void
1836weston_wm_destroy(struct weston_wm *wm)
1837{
1838 /* FIXME: Free windows in hash. */
1839 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001840 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001841 xcb_disconnect(wm->conn);
1842 wl_event_source_remove(wm->source);
1843 wl_list_remove(&wm->selection_listener.link);
1844 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001845 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001846 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001847
1848 free(wm);
1849}
1850
1851static void
1852surface_destroy(struct wl_listener *listener, void *data)
1853{
1854 struct weston_wm_window *window =
1855 container_of(listener,
1856 struct weston_wm_window, surface_destroy_listener);
1857
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001858 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001859
1860 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001861}
1862
1863static struct weston_wm_window *
1864get_wm_window(struct weston_surface *surface)
1865{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001866 struct wl_listener *listener;
1867
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001868 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001869 if (listener)
1870 return container_of(listener, struct weston_wm_window,
1871 surface_destroy_listener);
1872
1873 return NULL;
1874}
1875
1876static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001877weston_wm_window_configure(void *data)
1878{
1879 struct weston_wm_window *window = data;
1880 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001881 uint32_t values[4];
1882 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001883
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001884 weston_wm_window_get_child_position(window, &x, &y);
1885 values[0] = x;
1886 values[1] = y;
1887 values[2] = window->width;
1888 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001889 xcb_configure_window(wm->conn,
1890 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001891 XCB_CONFIG_WINDOW_X |
1892 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001893 XCB_CONFIG_WINDOW_WIDTH |
1894 XCB_CONFIG_WINDOW_HEIGHT,
1895 values);
1896
1897 weston_wm_window_get_frame_size(window, &width, &height);
1898 values[0] = width;
1899 values[1] = height;
1900 xcb_configure_window(wm->conn,
1901 window->frame_id,
1902 XCB_CONFIG_WINDOW_WIDTH |
1903 XCB_CONFIG_WINDOW_HEIGHT,
1904 values);
1905
1906 window->configure_source = NULL;
1907
1908 weston_wm_window_schedule_repaint(window);
1909}
1910
1911static void
1912send_configure(struct weston_surface *surface,
1913 uint32_t edges, int32_t width, int32_t height)
1914{
1915 struct weston_wm_window *window = get_wm_window(surface);
1916 struct weston_wm *wm = window->wm;
1917 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001918 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001919
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001920 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001921 hborder = 0;
1922 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001923 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001924 hborder = 2 * (t->margin + t->width);
1925 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001926 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001927 hborder = 2 * t->margin;
1928 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001929 }
1930
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001931 if (width > hborder)
1932 window->width = width - hborder;
1933 else
1934 window->width = 1;
1935
1936 if (height > vborder)
1937 window->height = height - vborder;
1938 else
1939 window->height = 1;
1940
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001941 if (window->configure_source)
1942 return;
1943
1944 window->configure_source =
1945 wl_event_loop_add_idle(wm->server->loop,
1946 weston_wm_window_configure, window);
1947}
1948
1949static const struct weston_shell_client shell_client = {
1950 send_configure
1951};
1952
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001953static int
1954legacy_fullscreen(struct weston_wm *wm,
1955 struct weston_wm_window *window,
1956 struct weston_output **output_ret)
1957{
1958 struct weston_compositor *compositor = wm->server->compositor;
1959 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001960 uint32_t minmax = PMinSize | PMaxSize;
1961 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001962
1963 /* Heuristics for detecting legacy fullscreen windows... */
1964
1965 wl_list_for_each(output, &compositor->output_list, link) {
1966 if (output->x == window->x &&
1967 output->y == window->y &&
1968 output->width == window->width &&
1969 output->height == window->height &&
1970 window->override_redirect) {
1971 *output_ret = output;
1972 return 1;
1973 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001974
1975 matching_size = 0;
1976 if ((window->size_hints.flags & (USSize |PSize)) &&
1977 window->size_hints.width == output->width &&
1978 window->size_hints.height == output->height)
1979 matching_size = 1;
1980 if ((window->size_hints.flags & minmax) == minmax &&
1981 window->size_hints.min_width == output->width &&
1982 window->size_hints.min_height == output->height &&
1983 window->size_hints.max_width == output->width &&
1984 window->size_hints.max_height == output->height)
1985 matching_size = 1;
1986
1987 if (matching_size && !window->decorate &&
1988 (window->size_hints.flags & (USPosition | PPosition)) &&
1989 window->size_hints.x == output->x &&
1990 window->size_hints.y == output->y) {
1991 *output_ret = output;
1992 return 1;
1993 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001994 }
1995
1996 return 0;
1997}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001998static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001999xserver_map_shell_surface(struct weston_wm *wm,
2000 struct weston_wm_window *window)
2001{
2002 struct weston_shell_interface *shell_interface =
2003 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002004 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002005
2006 if (!shell_interface->create_shell_surface)
2007 return;
2008
2009 window->shsurf =
2010 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002011 window->surface,
2012 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002013
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002014 if (window->fullscreen) {
2015 window->saved_width = window->width;
2016 window->saved_height = window->height;
2017 shell_interface->set_fullscreen(window->shsurf,
2018 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2019 0, NULL);
2020 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002021 } else if (legacy_fullscreen(wm, window, &output)) {
2022 window->fullscreen = 1;
2023 shell_interface->set_fullscreen(window->shsurf,
2024 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2025 0, output);
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002026 } else if (!window->override_redirect) {
2027 shell_interface->set_toplevel(window->shsurf);
2028 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002029 } else {
2030 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002031 window->x,
2032 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002033 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002034 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002035}
2036
2037static void
2038xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2039 struct wl_resource *surface_resource, uint32_t id)
2040{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002041 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002042 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002043 struct weston_surface *surface =
2044 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002045 struct weston_wm_window *window;
2046
2047 if (client != wxs->client)
2048 return;
2049
2050 window = hash_table_lookup(wm->window_hash, id);
2051 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002052 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002053 return;
2054 }
2055
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002056 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002057
2058 weston_wm_window_read_properties(window);
2059
2060 window->surface = (struct weston_surface *) surface;
2061 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002062 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002063 &window->surface_destroy_listener);
2064
2065 weston_wm_window_schedule_repaint(window);
2066 xserver_map_shell_surface(wm, window);
2067}
2068
2069const struct xserver_interface xserver_implementation = {
2070 xserver_set_window_id
2071};