blob: cc6b2614f14ada566b862d3ab8cedd7e8ac36411 [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 (window->repaint_source)
874 wl_event_source_remove(window->repaint_source);
875 if (window->cairo_surface)
876 cairo_surface_destroy(window->cairo_surface);
877
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400878 if (window->frame_id) {
879 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
880 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500881 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400882 hash_table_remove(wm->window_hash, window->frame_id);
883 window->frame_id = XCB_WINDOW_NONE;
884 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400885
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400886 if (wm->focus_window == window)
887 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400888 if (window->surface)
889 wl_list_remove(&window->surface_destroy_listener.link);
890 window->surface = NULL;
891}
892
893static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400894weston_wm_window_draw_decoration(void *data)
895{
896 struct weston_wm_window *window = data;
897 struct weston_wm *wm = window->wm;
898 struct theme *t = wm->theme;
899 cairo_t *cr;
900 int x, y, width, height;
901 const char *title;
902 uint32_t flags = 0;
903
904 weston_wm_window_read_properties(window);
905
906 window->repaint_source = NULL;
907
908 weston_wm_window_get_frame_size(window, &width, &height);
909 weston_wm_window_get_child_position(window, &x, &y);
910
911 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
912 cr = cairo_create(window->cairo_surface);
913
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500914 if (window->fullscreen) {
915 /* nothing */
916 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400917 if (wm->focus_window == window)
918 flags |= THEME_FRAME_ACTIVE;
919
920 if (window->name)
921 title = window->name;
922 else
923 title = "untitled";
924
925 theme_render_frame(t, cr, width, height, title, flags);
926 } else {
927 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
928 cairo_set_source_rgba(cr, 0, 0, 0, 0);
929 cairo_paint(cr);
930
931 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
932 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
933 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
934 }
935
936 cairo_destroy(cr);
937
938 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700939 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500940 if(window->has_alpha) {
941 pixman_region32_init(&window->surface->pending.opaque);
942 } else {
943 /* We leave an extra pixel around the X window area to
944 * make sure we don't sample from the undefined alpha
945 * channel when filtering. */
946 pixman_region32_init_rect(&window->surface->pending.opaque,
947 x - 1, y - 1,
948 window->width + 2,
949 window->height + 2);
950 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200951 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500952 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400953
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500954 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500955 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500956 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400957 t->margin, t->margin,
958 width - 2 * t->margin,
959 height - 2 * t->margin);
960 }
961}
962
963static void
964weston_wm_window_schedule_repaint(struct weston_wm_window *window)
965{
966 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300967 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400968
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400969 if (window->frame_id == XCB_WINDOW_NONE) {
970 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300971 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700972 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500973 if(window->has_alpha) {
974 pixman_region32_init(&window->surface->pending.opaque);
975 } else {
976 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
977 width, height);
978 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200979 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400980 }
981 return;
982 }
983
984 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400985 return;
986
987 window->repaint_source =
988 wl_event_loop_add_idle(wm->server->loop,
989 weston_wm_window_draw_decoration,
990 window);
991}
992
993static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400994weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
995{
996 xcb_property_notify_event_t *property_notify =
997 (xcb_property_notify_event_t *) event;
998 struct weston_wm_window *window;
999
1000 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001001 if (!window)
1002 return;
1003
1004 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001005
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001006 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001007 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001008 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001009 else
1010 read_and_dump_property(wm, property_notify->window,
1011 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001012
1013 if (property_notify->atom == wm->atom.net_wm_name ||
1014 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001015 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001016}
1017
1018static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001019weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001020 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001021{
1022 struct weston_wm_window *window;
1023 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001024 xcb_get_geometry_cookie_t geometry_cookie;
1025 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001026
Peter Huttererf3d62272013-08-08 11:57:05 +10001027 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001028 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001029 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001030 return;
1031 }
1032
MoD384a11a2013-06-22 11:04:21 -05001033 geometry_cookie = xcb_get_geometry(wm->conn, id);
1034
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001035 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1036 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1037
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001038 window->wm = wm;
1039 window->id = id;
1040 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001041 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001042 window->width = width;
1043 window->height = height;
1044
MoD384a11a2013-06-22 11:04:21 -05001045 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1046 /* technically we should use XRender and check the visual format's
1047 alpha_mask, but checking depth is simpler and works in all known cases */
1048 if(geometry_reply != NULL)
1049 window->has_alpha = geometry_reply->depth == 32;
1050 free(geometry_reply);
1051
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001052 hash_table_insert(wm->window_hash, id, window);
1053}
1054
1055static void
1056weston_wm_window_destroy(struct weston_wm_window *window)
1057{
1058 hash_table_remove(window->wm->window_hash, window->id);
1059 free(window);
1060}
1061
1062static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001063weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1064{
1065 xcb_create_notify_event_t *create_notify =
1066 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001067
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001068 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1069 create_notify->window,
1070 create_notify->width, create_notify->height,
1071 create_notify->override_redirect ? ", override" : "",
1072 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001073
1074 if (our_resource(wm, create_notify->window))
1075 return;
1076
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001077 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001078 create_notify->width, create_notify->height,
1079 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001080}
1081
1082static void
1083weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1084{
1085 xcb_destroy_notify_event_t *destroy_notify =
1086 (xcb_destroy_notify_event_t *) event;
1087 struct weston_wm_window *window;
1088
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001089 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1090 destroy_notify->window,
1091 destroy_notify->event,
1092 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001093
1094 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001095 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001096
1097 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001098 weston_wm_window_destroy(window);
1099}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001100
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001101static void
1102weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1103{
1104 xcb_reparent_notify_event_t *reparent_notify =
1105 (xcb_reparent_notify_event_t *) event;
1106 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001107
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001108 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1109 reparent_notify->window,
1110 reparent_notify->parent,
1111 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001112
1113 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001114 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1115 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001116 } else if (!our_resource(wm, reparent_notify->parent)) {
1117 window = hash_table_lookup(wm->window_hash,
1118 reparent_notify->window);
1119 weston_wm_window_destroy(window);
1120 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001121}
1122
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001123struct weston_seat *
1124weston_wm_pick_seat(struct weston_wm *wm)
1125{
1126 return container_of(wm->server->compositor->seat_list.next,
1127 struct weston_seat, link);
1128}
1129
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001130static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001131weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1132 xcb_client_message_event_t *client_message)
1133{
1134 static const int map[] = {
1135 THEME_LOCATION_RESIZING_TOP_LEFT,
1136 THEME_LOCATION_RESIZING_TOP,
1137 THEME_LOCATION_RESIZING_TOP_RIGHT,
1138 THEME_LOCATION_RESIZING_RIGHT,
1139 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1140 THEME_LOCATION_RESIZING_BOTTOM,
1141 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1142 THEME_LOCATION_RESIZING_LEFT
1143 };
1144
1145 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001146 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001147 int detail;
1148 struct weston_shell_interface *shell_interface =
1149 &wm->server->compositor->shell_interface;
1150
Kristian Høgsberge3148752013-05-06 23:19:49 -04001151 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001152 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001153 return;
1154
1155 detail = client_message->data.data32[2];
1156 switch (detail) {
1157 case _NET_WM_MOVERESIZE_MOVE:
1158 shell_interface->move(window->shsurf, seat);
1159 break;
1160 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1161 case _NET_WM_MOVERESIZE_SIZE_TOP:
1162 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1163 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1164 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1165 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1166 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1167 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1168 shell_interface->resize(window->shsurf, seat, map[detail]);
1169 break;
1170 case _NET_WM_MOVERESIZE_CANCEL:
1171 break;
1172 }
1173}
1174
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001175#define _NET_WM_STATE_REMOVE 0
1176#define _NET_WM_STATE_ADD 1
1177#define _NET_WM_STATE_TOGGLE 2
1178
1179static int
1180update_state(int action, int *state)
1181{
1182 int new_state, changed;
1183
1184 switch (action) {
1185 case _NET_WM_STATE_REMOVE:
1186 new_state = 0;
1187 break;
1188 case _NET_WM_STATE_ADD:
1189 new_state = 1;
1190 break;
1191 case _NET_WM_STATE_TOGGLE:
1192 new_state = !*state;
1193 break;
1194 default:
1195 return 0;
1196 }
1197
1198 changed = (*state != new_state);
1199 *state = new_state;
1200
1201 return changed;
1202}
1203
1204static void
1205weston_wm_window_configure(void *data);
1206
1207static void
1208weston_wm_window_handle_state(struct weston_wm_window *window,
1209 xcb_client_message_event_t *client_message)
1210{
1211 struct weston_wm *wm = window->wm;
1212 struct weston_shell_interface *shell_interface =
1213 &wm->server->compositor->shell_interface;
1214 uint32_t action, property;
1215
1216 action = client_message->data.data32[0];
1217 property = client_message->data.data32[1];
1218
1219 if (property == wm->atom.net_wm_state_fullscreen &&
1220 update_state(action, &window->fullscreen)) {
1221 weston_wm_window_set_net_wm_state(window);
1222 if (window->fullscreen) {
1223 window->saved_width = window->width;
1224 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001225
1226 if (window->shsurf)
1227 shell_interface->set_fullscreen(window->shsurf,
1228 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1229 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001230 } else {
1231 shell_interface->set_toplevel(window->shsurf);
1232 window->width = window->saved_width;
1233 window->height = window->saved_height;
1234 weston_wm_window_configure(window);
1235 }
1236 }
1237}
1238
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001239static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001240weston_wm_handle_client_message(struct weston_wm *wm,
1241 xcb_generic_event_t *event)
1242{
1243 xcb_client_message_event_t *client_message =
1244 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001245 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001246
1247 window = hash_table_lookup(wm->window_hash, client_message->window);
1248
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001249 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1250 get_atom_name(wm->conn, client_message->type),
1251 client_message->data.data32[0],
1252 client_message->data.data32[1],
1253 client_message->data.data32[2],
1254 client_message->data.data32[3],
1255 client_message->data.data32[4],
1256 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001257
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001258 if (client_message->type == wm->atom.net_wm_moveresize)
1259 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001260 else if (client_message->type == wm->atom.net_wm_state)
1261 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001262}
1263
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001264enum cursor_type {
1265 XWM_CURSOR_TOP,
1266 XWM_CURSOR_BOTTOM,
1267 XWM_CURSOR_LEFT,
1268 XWM_CURSOR_RIGHT,
1269 XWM_CURSOR_TOP_LEFT,
1270 XWM_CURSOR_TOP_RIGHT,
1271 XWM_CURSOR_BOTTOM_LEFT,
1272 XWM_CURSOR_BOTTOM_RIGHT,
1273 XWM_CURSOR_LEFT_PTR,
1274};
1275
1276static const char *cursors[] = {
1277 "top_side",
1278 "bottom_side",
1279 "left_side",
1280 "right_side",
1281 "top_left_corner",
1282 "top_right_corner",
1283 "bottom_left_corner",
1284 "bottom_right_corner",
1285 "left_ptr"
1286};
1287
1288static void
1289weston_wm_create_cursors(struct weston_wm *wm)
1290{
1291 int i, count = ARRAY_LENGTH(cursors);
1292
1293 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1294 for (i = 0; i < count; i++) {
1295 wm->cursors[i] =
1296 xcb_cursor_library_load_cursor(wm, cursors[i]);
1297 }
1298
1299 wm->last_cursor = -1;
1300}
1301
1302static void
1303weston_wm_destroy_cursors(struct weston_wm *wm)
1304{
1305 uint8_t i;
1306
1307 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1308 xcb_free_cursor(wm->conn, wm->cursors[i]);
1309
1310 free(wm->cursors);
1311}
1312
1313static int
1314get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1315{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001316 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001317
1318 switch (location) {
1319 case THEME_LOCATION_RESIZING_TOP:
1320 return XWM_CURSOR_TOP;
1321 case THEME_LOCATION_RESIZING_BOTTOM:
1322 return XWM_CURSOR_BOTTOM;
1323 case THEME_LOCATION_RESIZING_LEFT:
1324 return XWM_CURSOR_LEFT;
1325 case THEME_LOCATION_RESIZING_RIGHT:
1326 return XWM_CURSOR_RIGHT;
1327 case THEME_LOCATION_RESIZING_TOP_LEFT:
1328 return XWM_CURSOR_TOP_LEFT;
1329 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1330 return XWM_CURSOR_TOP_RIGHT;
1331 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1332 return XWM_CURSOR_BOTTOM_LEFT;
1333 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1334 return XWM_CURSOR_BOTTOM_RIGHT;
1335 case THEME_LOCATION_EXTERIOR:
1336 case THEME_LOCATION_TITLEBAR:
1337 default:
1338 return XWM_CURSOR_LEFT_PTR;
1339 }
1340}
1341
1342static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001343weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1344 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001345{
1346 uint32_t cursor_value_list;
1347
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001348 if (wm->last_cursor == cursor)
1349 return;
1350
1351 wm->last_cursor = cursor;
1352
1353 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001354 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001355 XCB_CW_CURSOR, &cursor_value_list);
1356 xcb_flush(wm->conn);
1357}
1358
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001359static void
1360weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1361{
1362 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1363 struct weston_shell_interface *shell_interface =
1364 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001365 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001366 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001367 enum theme_location location;
1368 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001369 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001370
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001371 wm_log("XCB_BUTTON_%s (detail %d)\n",
1372 button->response_type == XCB_BUTTON_PRESS ?
1373 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001374
1375 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001376 weston_wm_window_get_frame_size(window, &width, &height);
1377
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001378 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001379 button->detail == 1) {
1380 location = theme_get_location(t,
1381 button->event_x,
1382 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001383 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001384
1385 switch (location) {
1386 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001387 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001388 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001389 case THEME_LOCATION_RESIZING_TOP:
1390 case THEME_LOCATION_RESIZING_BOTTOM:
1391 case THEME_LOCATION_RESIZING_LEFT:
1392 case THEME_LOCATION_RESIZING_RIGHT:
1393 case THEME_LOCATION_RESIZING_TOP_LEFT:
1394 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1395 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1396 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1397 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001398 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001399 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001400 default:
1401 break;
1402 }
1403 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001404}
1405
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001406static void
1407weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1408{
1409 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1410 struct weston_wm_window *window;
1411 int cursor, width, height;
1412
1413 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001414 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001415 return;
1416
1417 weston_wm_window_get_frame_size(window, &width, &height);
1418 cursor = get_cursor_for_location(wm->theme, width, height,
1419 motion->event_x, motion->event_y);
1420
Tiago Vignattic1903232012-07-16 12:15:37 -04001421 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001422}
1423
1424static void
1425weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1426{
1427 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1428 struct weston_wm_window *window;
1429 int cursor, width, height;
1430
1431 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001432 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001433 return;
1434
1435 weston_wm_window_get_frame_size(window, &width, &height);
1436 cursor = get_cursor_for_location(wm->theme, width, height,
1437 enter->event_x, enter->event_y);
1438
Tiago Vignattic1903232012-07-16 12:15:37 -04001439 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001440}
1441
1442static void
1443weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1444{
1445 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1446 struct weston_wm_window *window;
1447
1448 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001449 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001450 return;
1451
Tiago Vignattic1903232012-07-16 12:15:37 -04001452 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001453}
1454
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001455static int
1456weston_wm_handle_event(int fd, uint32_t mask, void *data)
1457{
1458 struct weston_wm *wm = data;
1459 xcb_generic_event_t *event;
1460 int count = 0;
1461
1462 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1463 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001464 free(event);
1465 count++;
1466 continue;
1467 }
1468
MoD31700122013-06-11 19:58:55 -05001469 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001470 case XCB_BUTTON_PRESS:
1471 case XCB_BUTTON_RELEASE:
1472 weston_wm_handle_button(wm, event);
1473 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001474 case XCB_ENTER_NOTIFY:
1475 weston_wm_handle_enter(wm, event);
1476 break;
1477 case XCB_LEAVE_NOTIFY:
1478 weston_wm_handle_leave(wm, event);
1479 break;
1480 case XCB_MOTION_NOTIFY:
1481 weston_wm_handle_motion(wm, event);
1482 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001483 case XCB_CREATE_NOTIFY:
1484 weston_wm_handle_create_notify(wm, event);
1485 break;
1486 case XCB_MAP_REQUEST:
1487 weston_wm_handle_map_request(wm, event);
1488 break;
1489 case XCB_MAP_NOTIFY:
1490 weston_wm_handle_map_notify(wm, event);
1491 break;
1492 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001493 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001494 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001495 case XCB_REPARENT_NOTIFY:
1496 weston_wm_handle_reparent_notify(wm, event);
1497 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001498 case XCB_CONFIGURE_REQUEST:
1499 weston_wm_handle_configure_request(wm, event);
1500 break;
1501 case XCB_CONFIGURE_NOTIFY:
1502 weston_wm_handle_configure_notify(wm, event);
1503 break;
1504 case XCB_DESTROY_NOTIFY:
1505 weston_wm_handle_destroy_notify(wm, event);
1506 break;
1507 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001508 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001509 break;
1510 case XCB_PROPERTY_NOTIFY:
1511 weston_wm_handle_property_notify(wm, event);
1512 break;
1513 case XCB_CLIENT_MESSAGE:
1514 weston_wm_handle_client_message(wm, event);
1515 break;
1516 }
1517
1518 free(event);
1519 count++;
1520 }
1521
1522 xcb_flush(wm->conn);
1523
1524 return count;
1525}
1526
1527static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001528weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1529{
1530 xcb_depth_iterator_t d_iter;
1531 xcb_visualtype_iterator_t vt_iter;
1532 xcb_visualtype_t *visualtype;
1533
1534 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1535 visualtype = NULL;
1536 while (d_iter.rem > 0) {
1537 if (d_iter.data->depth == 32) {
1538 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1539 visualtype = vt_iter.data;
1540 break;
1541 }
1542
1543 xcb_depth_next(&d_iter);
1544 }
1545
1546 if (visualtype == NULL) {
1547 weston_log("no 32 bit visualtype\n");
1548 return;
1549 }
1550
1551 wm->visual_id = visualtype->visual_id;
1552 wm->colormap = xcb_generate_id(wm->conn);
1553 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1554 wm->colormap, wm->screen->root, wm->visual_id);
1555}
1556
1557static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001558weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001559{
1560
1561#define F(field) offsetof(struct weston_wm, field)
1562
1563 static const struct { const char *name; int offset; } atoms[] = {
1564 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001565 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001566 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1567 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001568 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001569 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001570 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001571 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001572 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001573 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001574 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1575 { "_NET_WM_STATE", F(atom.net_wm_state) },
1576 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1577 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1578 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1579 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1580
1581 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1582 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1583 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1584 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1585 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1586 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1587 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001588 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1589 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001590 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1591 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1592 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1593 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1594 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1595
1596 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1597 { "_NET_SUPPORTING_WM_CHECK",
1598 F(atom.net_supporting_wm_check) },
1599 { "_NET_SUPPORTED", F(atom.net_supported) },
1600 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1601 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001602 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001603 { "TARGETS", F(atom.targets) },
1604 { "UTF8_STRING", F(atom.utf8_string) },
1605 { "_WL_SELECTION", F(atom.wl_selection) },
1606 { "INCR", F(atom.incr) },
1607 { "TIMESTAMP", F(atom.timestamp) },
1608 { "MULTIPLE", F(atom.multiple) },
1609 { "UTF8_STRING" , F(atom.utf8_string) },
1610 { "COMPOUND_TEXT", F(atom.compound_text) },
1611 { "TEXT", F(atom.text) },
1612 { "STRING", F(atom.string) },
1613 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1614 { "text/plain", F(atom.text_plain) },
1615 };
1616#undef F
1617
1618 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1619 xcb_xfixes_query_version_reply_t *xfixes_reply;
1620 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1621 xcb_intern_atom_reply_t *reply;
1622 xcb_render_query_pict_formats_reply_t *formats_reply;
1623 xcb_render_query_pict_formats_cookie_t formats_cookie;
1624 xcb_render_pictforminfo_t *formats;
1625 uint32_t i;
1626
1627 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1628
1629 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1630
1631 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1632 cookies[i] = xcb_intern_atom (wm->conn, 0,
1633 strlen(atoms[i].name),
1634 atoms[i].name);
1635
1636 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1637 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1638 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1639 free(reply);
1640 }
1641
1642 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1643 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001644 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001645
1646 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1647 XCB_XFIXES_MAJOR_VERSION,
1648 XCB_XFIXES_MINOR_VERSION);
1649 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1650 xfixes_cookie, NULL);
1651
Martin Minarik6d118362012-06-07 18:01:59 +02001652 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001653 xfixes_reply->major_version, xfixes_reply->minor_version);
1654
1655 free(xfixes_reply);
1656
1657 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1658 formats_cookie, 0);
1659 if (formats_reply == NULL)
1660 return;
1661
1662 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001663 for (i = 0; i < formats_reply->num_formats; i++) {
1664 if (formats[i].direct.red_mask != 0xff &&
1665 formats[i].direct.red_shift != 16)
1666 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001667 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1668 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001669 wm->format_rgb = formats[i];
1670 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1671 formats[i].depth == 32 &&
1672 formats[i].direct.alpha_mask == 0xff &&
1673 formats[i].direct.alpha_shift == 24)
1674 wm->format_rgba = formats[i];
1675 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001676
1677 free(formats_reply);
1678}
1679
1680static void
1681weston_wm_create_wm_window(struct weston_wm *wm)
1682{
1683 static const char name[] = "Weston WM";
1684
1685 wm->wm_window = xcb_generate_id(wm->conn);
1686 xcb_create_window(wm->conn,
1687 XCB_COPY_FROM_PARENT,
1688 wm->wm_window,
1689 wm->screen->root,
1690 0, 0,
1691 10, 10,
1692 0,
1693 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1694 wm->screen->root_visual,
1695 0, NULL);
1696
1697 xcb_change_property(wm->conn,
1698 XCB_PROP_MODE_REPLACE,
1699 wm->wm_window,
1700 wm->atom.net_supporting_wm_check,
1701 XCB_ATOM_WINDOW,
1702 32, /* format */
1703 1, &wm->wm_window);
1704
1705 xcb_change_property(wm->conn,
1706 XCB_PROP_MODE_REPLACE,
1707 wm->wm_window,
1708 wm->atom.net_wm_name,
1709 wm->atom.utf8_string,
1710 8, /* format */
1711 strlen(name), name);
1712
1713 xcb_change_property(wm->conn,
1714 XCB_PROP_MODE_REPLACE,
1715 wm->screen->root,
1716 wm->atom.net_supporting_wm_check,
1717 XCB_ATOM_WINDOW,
1718 32, /* format */
1719 1, &wm->wm_window);
1720
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001721 /* Claim the WM_S0 selection even though we don't suport
1722 * the --replace functionality. */
1723 xcb_set_selection_owner(wm->conn,
1724 wm->wm_window,
1725 wm->atom.wm_s0,
1726 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001727
1728 xcb_set_selection_owner(wm->conn,
1729 wm->wm_window,
1730 wm->atom.net_wm_cm_s0,
1731 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001732}
1733
1734struct weston_wm *
1735weston_wm_create(struct weston_xserver *wxs)
1736{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001737 struct weston_wm *wm;
1738 struct wl_event_loop *loop;
1739 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001740 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001741 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001742 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001743
Peter Huttererf3d62272013-08-08 11:57:05 +10001744 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001745 if (wm == NULL)
1746 return NULL;
1747
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001748 wm->server = wxs;
1749 wm->window_hash = hash_table_create();
1750 if (wm->window_hash == NULL) {
1751 free(wm);
1752 return NULL;
1753 }
1754
1755 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001756 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001757 hash_table_destroy(wm->window_hash);
1758 free(wm);
1759 return NULL;
1760 }
1761
1762 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001763 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001764 close(sv[1]);
1765
1766 /* xcb_connect_to_fd takes ownership of the fd. */
1767 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1768 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001769 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001770 close(sv[0]);
1771 hash_table_destroy(wm->window_hash);
1772 free(wm);
1773 return NULL;
1774 }
1775
1776 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1777 wm->screen = s.data;
1778
1779 loop = wl_display_get_event_loop(wxs->wl_display);
1780 wm->source =
1781 wl_event_loop_add_fd(loop, sv[0],
1782 WL_EVENT_READABLE,
1783 weston_wm_handle_event, wm);
1784 wl_event_source_check(wm->source);
1785
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001786 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001787 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001788
1789 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001790 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1791 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1792 XCB_EVENT_MASK_PROPERTY_CHANGE;
1793 xcb_change_window_attributes(wm->conn, wm->screen->root,
1794 XCB_CW_EVENT_MASK, values);
1795 wm->theme = theme_create();
1796
1797 weston_wm_create_wm_window(wm);
1798
1799 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001800 supported[1] = wm->atom.net_wm_state;
1801 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001802 xcb_change_property(wm->conn,
1803 XCB_PROP_MODE_REPLACE,
1804 wm->screen->root,
1805 wm->atom.net_supported,
1806 XCB_ATOM_ATOM,
1807 32, /* format */
1808 ARRAY_LENGTH(supported), supported);
1809
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001810 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001811
1812 xcb_flush(wm->conn);
1813
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001814 wm->activate_listener.notify = weston_wm_window_activate;
1815 wl_signal_add(&wxs->compositor->activate_signal,
1816 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001817 wm->transform_listener.notify = weston_wm_window_transform;
1818 wl_signal_add(&wxs->compositor->transform_signal,
1819 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001820 wm->kill_listener.notify = weston_wm_kill_client;
1821 wl_signal_add(&wxs->compositor->kill_signal,
1822 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001823
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001824 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001825 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001826
Martin Minarik6d118362012-06-07 18:01:59 +02001827 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001828
1829 return wm;
1830}
1831
1832void
1833weston_wm_destroy(struct weston_wm *wm)
1834{
1835 /* FIXME: Free windows in hash. */
1836 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001837 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001838 xcb_disconnect(wm->conn);
1839 wl_event_source_remove(wm->source);
1840 wl_list_remove(&wm->selection_listener.link);
1841 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001842 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001843 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001844
1845 free(wm);
1846}
1847
1848static void
1849surface_destroy(struct wl_listener *listener, void *data)
1850{
1851 struct weston_wm_window *window =
1852 container_of(listener,
1853 struct weston_wm_window, surface_destroy_listener);
1854
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001855 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001856
1857 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001858}
1859
1860static struct weston_wm_window *
1861get_wm_window(struct weston_surface *surface)
1862{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001863 struct wl_listener *listener;
1864
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001865 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001866 if (listener)
1867 return container_of(listener, struct weston_wm_window,
1868 surface_destroy_listener);
1869
1870 return NULL;
1871}
1872
1873static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001874weston_wm_window_configure(void *data)
1875{
1876 struct weston_wm_window *window = data;
1877 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001878 uint32_t values[4];
1879 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001880
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001881 weston_wm_window_get_child_position(window, &x, &y);
1882 values[0] = x;
1883 values[1] = y;
1884 values[2] = window->width;
1885 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001886 xcb_configure_window(wm->conn,
1887 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001888 XCB_CONFIG_WINDOW_X |
1889 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001890 XCB_CONFIG_WINDOW_WIDTH |
1891 XCB_CONFIG_WINDOW_HEIGHT,
1892 values);
1893
1894 weston_wm_window_get_frame_size(window, &width, &height);
1895 values[0] = width;
1896 values[1] = height;
1897 xcb_configure_window(wm->conn,
1898 window->frame_id,
1899 XCB_CONFIG_WINDOW_WIDTH |
1900 XCB_CONFIG_WINDOW_HEIGHT,
1901 values);
1902
1903 window->configure_source = NULL;
1904
1905 weston_wm_window_schedule_repaint(window);
1906}
1907
1908static void
1909send_configure(struct weston_surface *surface,
1910 uint32_t edges, int32_t width, int32_t height)
1911{
1912 struct weston_wm_window *window = get_wm_window(surface);
1913 struct weston_wm *wm = window->wm;
1914 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001915 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001916
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001917 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001918 hborder = 0;
1919 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001920 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001921 hborder = 2 * (t->margin + t->width);
1922 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001923 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001924 hborder = 2 * t->margin;
1925 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001926 }
1927
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001928 if (width > hborder)
1929 window->width = width - hborder;
1930 else
1931 window->width = 1;
1932
1933 if (height > vborder)
1934 window->height = height - vborder;
1935 else
1936 window->height = 1;
1937
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001938 if (window->configure_source)
1939 return;
1940
1941 window->configure_source =
1942 wl_event_loop_add_idle(wm->server->loop,
1943 weston_wm_window_configure, window);
1944}
1945
1946static const struct weston_shell_client shell_client = {
1947 send_configure
1948};
1949
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001950static int
1951legacy_fullscreen(struct weston_wm *wm,
1952 struct weston_wm_window *window,
1953 struct weston_output **output_ret)
1954{
1955 struct weston_compositor *compositor = wm->server->compositor;
1956 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001957 uint32_t minmax = PMinSize | PMaxSize;
1958 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001959
1960 /* Heuristics for detecting legacy fullscreen windows... */
1961
1962 wl_list_for_each(output, &compositor->output_list, link) {
1963 if (output->x == window->x &&
1964 output->y == window->y &&
1965 output->width == window->width &&
1966 output->height == window->height &&
1967 window->override_redirect) {
1968 *output_ret = output;
1969 return 1;
1970 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001971
1972 matching_size = 0;
1973 if ((window->size_hints.flags & (USSize |PSize)) &&
1974 window->size_hints.width == output->width &&
1975 window->size_hints.height == output->height)
1976 matching_size = 1;
1977 if ((window->size_hints.flags & minmax) == minmax &&
1978 window->size_hints.min_width == output->width &&
1979 window->size_hints.min_height == output->height &&
1980 window->size_hints.max_width == output->width &&
1981 window->size_hints.max_height == output->height)
1982 matching_size = 1;
1983
1984 if (matching_size && !window->decorate &&
1985 (window->size_hints.flags & (USPosition | PPosition)) &&
1986 window->size_hints.x == output->x &&
1987 window->size_hints.y == output->y) {
1988 *output_ret = output;
1989 return 1;
1990 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001991 }
1992
1993 return 0;
1994}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001995static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001996xserver_map_shell_surface(struct weston_wm *wm,
1997 struct weston_wm_window *window)
1998{
1999 struct weston_shell_interface *shell_interface =
2000 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002001 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002002
2003 if (!shell_interface->create_shell_surface)
2004 return;
2005
2006 window->shsurf =
2007 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002008 window->surface,
2009 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002010
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002011 if (window->fullscreen) {
2012 window->saved_width = window->width;
2013 window->saved_height = window->height;
2014 shell_interface->set_fullscreen(window->shsurf,
2015 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2016 0, NULL);
2017 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002018 } else if (legacy_fullscreen(wm, window, &output)) {
2019 window->fullscreen = 1;
2020 shell_interface->set_fullscreen(window->shsurf,
2021 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2022 0, output);
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002023 } else if (!window->override_redirect) {
2024 shell_interface->set_toplevel(window->shsurf);
2025 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002026 } else {
2027 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002028 window->x,
2029 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002030 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002031 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002032}
2033
2034static void
2035xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2036 struct wl_resource *surface_resource, uint32_t id)
2037{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002038 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002039 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002040 struct weston_surface *surface =
2041 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002042 struct weston_wm_window *window;
2043
2044 if (client != wxs->client)
2045 return;
2046
2047 window = hash_table_lookup(wm->window_hash, id);
2048 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002049 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002050 return;
2051 }
2052
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002053 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002054
2055 weston_wm_window_read_properties(window);
2056
2057 window->surface = (struct weston_surface *) surface;
2058 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002059 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002060 &window->surface_destroy_listener);
2061
2062 weston_wm_window_schedule_repaint(window);
2063 xserver_map_shell_surface(wm, window);
2064}
2065
2066const struct xserver_interface xserver_implementation = {
2067 xserver_set_window_id
2068};