blob: c6c7bd58bf22ddff4b92dbd8c0e06352ec484414 [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øgsberg380deee2012-05-21 17:12:41 -0400756weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
757{
758 xcb_map_request_event_t *map_request =
759 (xcb_map_request_event_t *) event;
760 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400761 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400762 int x, y, width, height;
763
764 if (our_resource(wm, map_request->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400765 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
766 map_request->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400767 return;
768 }
769
770 window = hash_table_lookup(wm->window_hash, map_request->window);
771
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400772 if (window->frame_id)
773 return;
774
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400775 weston_wm_window_read_properties(window);
776
777 weston_wm_window_get_frame_size(window, &width, &height);
778 weston_wm_window_get_child_position(window, &x, &y);
779
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400780 values[0] = wm->screen->black_pixel;
781 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400782 XCB_EVENT_MASK_KEY_PRESS |
783 XCB_EVENT_MASK_KEY_RELEASE |
784 XCB_EVENT_MASK_BUTTON_PRESS |
785 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400786 XCB_EVENT_MASK_POINTER_MOTION |
787 XCB_EVENT_MASK_ENTER_WINDOW |
788 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400789 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400790 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400791 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400792
793 window->frame_id = xcb_generate_id(wm->conn);
794 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400795 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400796 window->frame_id,
797 wm->screen->root,
798 0, 0,
799 width, height,
800 0,
801 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400802 wm->visual_id,
803 XCB_CW_BORDER_PIXEL |
804 XCB_CW_EVENT_MASK |
805 XCB_CW_COLORMAP, values);
806
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400807 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
808
809 values[0] = 0;
810 xcb_configure_window(wm->conn, window->id,
811 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
812
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400813 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
814 window->id, window, window->frame_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400815
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500816 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
817 weston_wm_window_set_net_wm_state(window);
818
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400819 xcb_map_window(wm->conn, map_request->window);
820 xcb_map_window(wm->conn, window->frame_id);
821
822 window->cairo_surface =
823 cairo_xcb_surface_create_with_xrender_format(wm->conn,
824 wm->screen,
825 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400826 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400827 width, height);
828
829 hash_table_insert(wm->window_hash, window->frame_id, window);
830}
831
832static void
833weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
834{
835 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
836
837 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400838 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
839 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400840 return;
841 }
842
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400843 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400844}
845
846static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400847weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
848{
849 xcb_unmap_notify_event_t *unmap_notify =
850 (xcb_unmap_notify_event_t *) event;
851 struct weston_wm_window *window;
852
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400853 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
854 unmap_notify->window,
855 unmap_notify->event,
856 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400857
858 if (our_resource(wm, unmap_notify->window))
859 return;
860
MoD31700122013-06-11 19:58:55 -0500861 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400862 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
863 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400864 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400865
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400866 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400867 if (window->repaint_source)
868 wl_event_source_remove(window->repaint_source);
869 if (window->cairo_surface)
870 cairo_surface_destroy(window->cairo_surface);
871
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400872 if (window->frame_id) {
873 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
874 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500875 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400876 hash_table_remove(wm->window_hash, window->frame_id);
877 window->frame_id = XCB_WINDOW_NONE;
878 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400879
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400880 if (wm->focus_window == window)
881 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400882 if (window->surface)
883 wl_list_remove(&window->surface_destroy_listener.link);
884 window->surface = NULL;
885}
886
887static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400888weston_wm_window_draw_decoration(void *data)
889{
890 struct weston_wm_window *window = data;
891 struct weston_wm *wm = window->wm;
892 struct theme *t = wm->theme;
893 cairo_t *cr;
894 int x, y, width, height;
895 const char *title;
896 uint32_t flags = 0;
897
898 weston_wm_window_read_properties(window);
899
900 window->repaint_source = NULL;
901
902 weston_wm_window_get_frame_size(window, &width, &height);
903 weston_wm_window_get_child_position(window, &x, &y);
904
905 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
906 cr = cairo_create(window->cairo_surface);
907
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500908 if (window->fullscreen) {
909 /* nothing */
910 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400911 if (wm->focus_window == window)
912 flags |= THEME_FRAME_ACTIVE;
913
914 if (window->name)
915 title = window->name;
916 else
917 title = "untitled";
918
919 theme_render_frame(t, cr, width, height, title, flags);
920 } else {
921 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
922 cairo_set_source_rgba(cr, 0, 0, 0, 0);
923 cairo_paint(cr);
924
925 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
926 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
927 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
928 }
929
930 cairo_destroy(cr);
931
932 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700933 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500934 if(window->has_alpha) {
935 pixman_region32_init(&window->surface->pending.opaque);
936 } else {
937 /* We leave an extra pixel around the X window area to
938 * make sure we don't sample from the undefined alpha
939 * channel when filtering. */
940 pixman_region32_init_rect(&window->surface->pending.opaque,
941 x - 1, y - 1,
942 window->width + 2,
943 window->height + 2);
944 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200945 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500946 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400947
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500948 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500949 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500950 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400951 t->margin, t->margin,
952 width - 2 * t->margin,
953 height - 2 * t->margin);
954 }
955}
956
957static void
958weston_wm_window_schedule_repaint(struct weston_wm_window *window)
959{
960 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300961 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400962
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400963 if (window->frame_id == XCB_WINDOW_NONE) {
964 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300965 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700966 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500967 if(window->has_alpha) {
968 pixman_region32_init(&window->surface->pending.opaque);
969 } else {
970 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
971 width, height);
972 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200973 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400974 }
975 return;
976 }
977
978 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400979 return;
980
981 window->repaint_source =
982 wl_event_loop_add_idle(wm->server->loop,
983 weston_wm_window_draw_decoration,
984 window);
985}
986
987static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400988weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
989{
990 xcb_property_notify_event_t *property_notify =
991 (xcb_property_notify_event_t *) event;
992 struct weston_wm_window *window;
993
994 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000995 if (!window)
996 return;
997
998 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400999
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001000 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001001 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001002 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001003 else
1004 read_and_dump_property(wm, property_notify->window,
1005 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001006
1007 if (property_notify->atom == wm->atom.net_wm_name ||
1008 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001009 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001010}
1011
1012static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001013weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001014 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001015{
1016 struct weston_wm_window *window;
1017 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001018 xcb_get_geometry_cookie_t geometry_cookie;
1019 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001020
Peter Huttererf3d62272013-08-08 11:57:05 +10001021 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001022 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001023 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001024 return;
1025 }
1026
MoD384a11a2013-06-22 11:04:21 -05001027 geometry_cookie = xcb_get_geometry(wm->conn, id);
1028
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001029 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1030 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1031
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001032 window->wm = wm;
1033 window->id = id;
1034 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001035 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001036 window->width = width;
1037 window->height = height;
1038
MoD384a11a2013-06-22 11:04:21 -05001039 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1040 /* technically we should use XRender and check the visual format's
1041 alpha_mask, but checking depth is simpler and works in all known cases */
1042 if(geometry_reply != NULL)
1043 window->has_alpha = geometry_reply->depth == 32;
1044 free(geometry_reply);
1045
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001046 hash_table_insert(wm->window_hash, id, window);
1047}
1048
1049static void
1050weston_wm_window_destroy(struct weston_wm_window *window)
1051{
1052 hash_table_remove(window->wm->window_hash, window->id);
1053 free(window);
1054}
1055
1056static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001057weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1058{
1059 xcb_create_notify_event_t *create_notify =
1060 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001061
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001062 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1063 create_notify->window,
1064 create_notify->width, create_notify->height,
1065 create_notify->override_redirect ? ", override" : "",
1066 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001067
1068 if (our_resource(wm, create_notify->window))
1069 return;
1070
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001071 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001072 create_notify->width, create_notify->height,
1073 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001074}
1075
1076static void
1077weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1078{
1079 xcb_destroy_notify_event_t *destroy_notify =
1080 (xcb_destroy_notify_event_t *) event;
1081 struct weston_wm_window *window;
1082
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001083 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1084 destroy_notify->window,
1085 destroy_notify->event,
1086 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001087
1088 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001089 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001090
1091 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001092 weston_wm_window_destroy(window);
1093}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001094
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001095static void
1096weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1097{
1098 xcb_reparent_notify_event_t *reparent_notify =
1099 (xcb_reparent_notify_event_t *) event;
1100 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001101
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001102 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1103 reparent_notify->window,
1104 reparent_notify->parent,
1105 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001106
1107 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001108 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1109 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001110 } else if (!our_resource(wm, reparent_notify->parent)) {
1111 window = hash_table_lookup(wm->window_hash,
1112 reparent_notify->window);
1113 weston_wm_window_destroy(window);
1114 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001115}
1116
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001117struct weston_seat *
1118weston_wm_pick_seat(struct weston_wm *wm)
1119{
1120 return container_of(wm->server->compositor->seat_list.next,
1121 struct weston_seat, link);
1122}
1123
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001124static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001125weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1126 xcb_client_message_event_t *client_message)
1127{
1128 static const int map[] = {
1129 THEME_LOCATION_RESIZING_TOP_LEFT,
1130 THEME_LOCATION_RESIZING_TOP,
1131 THEME_LOCATION_RESIZING_TOP_RIGHT,
1132 THEME_LOCATION_RESIZING_RIGHT,
1133 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1134 THEME_LOCATION_RESIZING_BOTTOM,
1135 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1136 THEME_LOCATION_RESIZING_LEFT
1137 };
1138
1139 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001140 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001141 int detail;
1142 struct weston_shell_interface *shell_interface =
1143 &wm->server->compositor->shell_interface;
1144
Kristian Høgsberge3148752013-05-06 23:19:49 -04001145 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001146 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001147 return;
1148
1149 detail = client_message->data.data32[2];
1150 switch (detail) {
1151 case _NET_WM_MOVERESIZE_MOVE:
1152 shell_interface->move(window->shsurf, seat);
1153 break;
1154 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1155 case _NET_WM_MOVERESIZE_SIZE_TOP:
1156 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1157 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1158 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1159 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1160 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1161 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1162 shell_interface->resize(window->shsurf, seat, map[detail]);
1163 break;
1164 case _NET_WM_MOVERESIZE_CANCEL:
1165 break;
1166 }
1167}
1168
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001169#define _NET_WM_STATE_REMOVE 0
1170#define _NET_WM_STATE_ADD 1
1171#define _NET_WM_STATE_TOGGLE 2
1172
1173static int
1174update_state(int action, int *state)
1175{
1176 int new_state, changed;
1177
1178 switch (action) {
1179 case _NET_WM_STATE_REMOVE:
1180 new_state = 0;
1181 break;
1182 case _NET_WM_STATE_ADD:
1183 new_state = 1;
1184 break;
1185 case _NET_WM_STATE_TOGGLE:
1186 new_state = !*state;
1187 break;
1188 default:
1189 return 0;
1190 }
1191
1192 changed = (*state != new_state);
1193 *state = new_state;
1194
1195 return changed;
1196}
1197
1198static void
1199weston_wm_window_configure(void *data);
1200
1201static void
1202weston_wm_window_handle_state(struct weston_wm_window *window,
1203 xcb_client_message_event_t *client_message)
1204{
1205 struct weston_wm *wm = window->wm;
1206 struct weston_shell_interface *shell_interface =
1207 &wm->server->compositor->shell_interface;
1208 uint32_t action, property;
1209
1210 action = client_message->data.data32[0];
1211 property = client_message->data.data32[1];
1212
1213 if (property == wm->atom.net_wm_state_fullscreen &&
1214 update_state(action, &window->fullscreen)) {
1215 weston_wm_window_set_net_wm_state(window);
1216 if (window->fullscreen) {
1217 window->saved_width = window->width;
1218 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001219
1220 if (window->shsurf)
1221 shell_interface->set_fullscreen(window->shsurf,
1222 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1223 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001224 } else {
1225 shell_interface->set_toplevel(window->shsurf);
1226 window->width = window->saved_width;
1227 window->height = window->saved_height;
1228 weston_wm_window_configure(window);
1229 }
1230 }
1231}
1232
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001233static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001234weston_wm_handle_client_message(struct weston_wm *wm,
1235 xcb_generic_event_t *event)
1236{
1237 xcb_client_message_event_t *client_message =
1238 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001239 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001240
1241 window = hash_table_lookup(wm->window_hash, client_message->window);
1242
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001243 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1244 get_atom_name(wm->conn, client_message->type),
1245 client_message->data.data32[0],
1246 client_message->data.data32[1],
1247 client_message->data.data32[2],
1248 client_message->data.data32[3],
1249 client_message->data.data32[4],
1250 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001251
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001252 if (client_message->type == wm->atom.net_wm_moveresize)
1253 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001254 else if (client_message->type == wm->atom.net_wm_state)
1255 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001256}
1257
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001258enum cursor_type {
1259 XWM_CURSOR_TOP,
1260 XWM_CURSOR_BOTTOM,
1261 XWM_CURSOR_LEFT,
1262 XWM_CURSOR_RIGHT,
1263 XWM_CURSOR_TOP_LEFT,
1264 XWM_CURSOR_TOP_RIGHT,
1265 XWM_CURSOR_BOTTOM_LEFT,
1266 XWM_CURSOR_BOTTOM_RIGHT,
1267 XWM_CURSOR_LEFT_PTR,
1268};
1269
1270static const char *cursors[] = {
1271 "top_side",
1272 "bottom_side",
1273 "left_side",
1274 "right_side",
1275 "top_left_corner",
1276 "top_right_corner",
1277 "bottom_left_corner",
1278 "bottom_right_corner",
1279 "left_ptr"
1280};
1281
1282static void
1283weston_wm_create_cursors(struct weston_wm *wm)
1284{
1285 int i, count = ARRAY_LENGTH(cursors);
1286
1287 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1288 for (i = 0; i < count; i++) {
1289 wm->cursors[i] =
1290 xcb_cursor_library_load_cursor(wm, cursors[i]);
1291 }
1292
1293 wm->last_cursor = -1;
1294}
1295
1296static void
1297weston_wm_destroy_cursors(struct weston_wm *wm)
1298{
1299 uint8_t i;
1300
1301 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1302 xcb_free_cursor(wm->conn, wm->cursors[i]);
1303
1304 free(wm->cursors);
1305}
1306
1307static int
1308get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1309{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001310 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001311
1312 switch (location) {
1313 case THEME_LOCATION_RESIZING_TOP:
1314 return XWM_CURSOR_TOP;
1315 case THEME_LOCATION_RESIZING_BOTTOM:
1316 return XWM_CURSOR_BOTTOM;
1317 case THEME_LOCATION_RESIZING_LEFT:
1318 return XWM_CURSOR_LEFT;
1319 case THEME_LOCATION_RESIZING_RIGHT:
1320 return XWM_CURSOR_RIGHT;
1321 case THEME_LOCATION_RESIZING_TOP_LEFT:
1322 return XWM_CURSOR_TOP_LEFT;
1323 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1324 return XWM_CURSOR_TOP_RIGHT;
1325 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1326 return XWM_CURSOR_BOTTOM_LEFT;
1327 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1328 return XWM_CURSOR_BOTTOM_RIGHT;
1329 case THEME_LOCATION_EXTERIOR:
1330 case THEME_LOCATION_TITLEBAR:
1331 default:
1332 return XWM_CURSOR_LEFT_PTR;
1333 }
1334}
1335
1336static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001337weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1338 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001339{
1340 uint32_t cursor_value_list;
1341
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001342 if (wm->last_cursor == cursor)
1343 return;
1344
1345 wm->last_cursor = cursor;
1346
1347 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001348 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001349 XCB_CW_CURSOR, &cursor_value_list);
1350 xcb_flush(wm->conn);
1351}
1352
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001353static void
1354weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1355{
1356 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1357 struct weston_shell_interface *shell_interface =
1358 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001359 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001360 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001361 enum theme_location location;
1362 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001363 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001364
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001365 wm_log("XCB_BUTTON_%s (detail %d)\n",
1366 button->response_type == XCB_BUTTON_PRESS ?
1367 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001368
1369 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001370 weston_wm_window_get_frame_size(window, &width, &height);
1371
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001372 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001373 button->detail == 1) {
1374 location = theme_get_location(t,
1375 button->event_x,
1376 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001377 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001378
1379 switch (location) {
1380 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001381 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001382 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001383 case THEME_LOCATION_RESIZING_TOP:
1384 case THEME_LOCATION_RESIZING_BOTTOM:
1385 case THEME_LOCATION_RESIZING_LEFT:
1386 case THEME_LOCATION_RESIZING_RIGHT:
1387 case THEME_LOCATION_RESIZING_TOP_LEFT:
1388 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1389 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1390 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1391 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001392 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001393 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001394 default:
1395 break;
1396 }
1397 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001398}
1399
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001400static void
1401weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1402{
1403 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1404 struct weston_wm_window *window;
1405 int cursor, width, height;
1406
1407 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001408 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001409 return;
1410
1411 weston_wm_window_get_frame_size(window, &width, &height);
1412 cursor = get_cursor_for_location(wm->theme, width, height,
1413 motion->event_x, motion->event_y);
1414
Tiago Vignattic1903232012-07-16 12:15:37 -04001415 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001416}
1417
1418static void
1419weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1420{
1421 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1422 struct weston_wm_window *window;
1423 int cursor, width, height;
1424
1425 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001426 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001427 return;
1428
1429 weston_wm_window_get_frame_size(window, &width, &height);
1430 cursor = get_cursor_for_location(wm->theme, width, height,
1431 enter->event_x, enter->event_y);
1432
Tiago Vignattic1903232012-07-16 12:15:37 -04001433 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001434}
1435
1436static void
1437weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1438{
1439 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1440 struct weston_wm_window *window;
1441
1442 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001443 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001444 return;
1445
Tiago Vignattic1903232012-07-16 12:15:37 -04001446 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001447}
1448
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001449static int
1450weston_wm_handle_event(int fd, uint32_t mask, void *data)
1451{
1452 struct weston_wm *wm = data;
1453 xcb_generic_event_t *event;
1454 int count = 0;
1455
1456 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1457 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001458 free(event);
1459 count++;
1460 continue;
1461 }
1462
MoD31700122013-06-11 19:58:55 -05001463 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001464 case XCB_BUTTON_PRESS:
1465 case XCB_BUTTON_RELEASE:
1466 weston_wm_handle_button(wm, event);
1467 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001468 case XCB_ENTER_NOTIFY:
1469 weston_wm_handle_enter(wm, event);
1470 break;
1471 case XCB_LEAVE_NOTIFY:
1472 weston_wm_handle_leave(wm, event);
1473 break;
1474 case XCB_MOTION_NOTIFY:
1475 weston_wm_handle_motion(wm, event);
1476 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001477 case XCB_CREATE_NOTIFY:
1478 weston_wm_handle_create_notify(wm, event);
1479 break;
1480 case XCB_MAP_REQUEST:
1481 weston_wm_handle_map_request(wm, event);
1482 break;
1483 case XCB_MAP_NOTIFY:
1484 weston_wm_handle_map_notify(wm, event);
1485 break;
1486 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001487 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001488 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001489 case XCB_REPARENT_NOTIFY:
1490 weston_wm_handle_reparent_notify(wm, event);
1491 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001492 case XCB_CONFIGURE_REQUEST:
1493 weston_wm_handle_configure_request(wm, event);
1494 break;
1495 case XCB_CONFIGURE_NOTIFY:
1496 weston_wm_handle_configure_notify(wm, event);
1497 break;
1498 case XCB_DESTROY_NOTIFY:
1499 weston_wm_handle_destroy_notify(wm, event);
1500 break;
1501 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001502 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001503 break;
1504 case XCB_PROPERTY_NOTIFY:
1505 weston_wm_handle_property_notify(wm, event);
1506 break;
1507 case XCB_CLIENT_MESSAGE:
1508 weston_wm_handle_client_message(wm, event);
1509 break;
1510 }
1511
1512 free(event);
1513 count++;
1514 }
1515
1516 xcb_flush(wm->conn);
1517
1518 return count;
1519}
1520
1521static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001522weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1523{
1524 xcb_depth_iterator_t d_iter;
1525 xcb_visualtype_iterator_t vt_iter;
1526 xcb_visualtype_t *visualtype;
1527
1528 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1529 visualtype = NULL;
1530 while (d_iter.rem > 0) {
1531 if (d_iter.data->depth == 32) {
1532 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1533 visualtype = vt_iter.data;
1534 break;
1535 }
1536
1537 xcb_depth_next(&d_iter);
1538 }
1539
1540 if (visualtype == NULL) {
1541 weston_log("no 32 bit visualtype\n");
1542 return;
1543 }
1544
1545 wm->visual_id = visualtype->visual_id;
1546 wm->colormap = xcb_generate_id(wm->conn);
1547 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1548 wm->colormap, wm->screen->root, wm->visual_id);
1549}
1550
1551static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001552weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001553{
1554
1555#define F(field) offsetof(struct weston_wm, field)
1556
1557 static const struct { const char *name; int offset; } atoms[] = {
1558 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001559 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001560 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1561 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001562 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001563 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001564 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001565 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001566 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001567 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001568 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1569 { "_NET_WM_STATE", F(atom.net_wm_state) },
1570 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1571 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1572 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1573 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1574
1575 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1576 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1577 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1578 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1579 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1580 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1581 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001582 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1583 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001584 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1585 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1586 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1587 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1588 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1589
1590 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1591 { "_NET_SUPPORTING_WM_CHECK",
1592 F(atom.net_supporting_wm_check) },
1593 { "_NET_SUPPORTED", F(atom.net_supported) },
1594 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1595 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001596 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001597 { "TARGETS", F(atom.targets) },
1598 { "UTF8_STRING", F(atom.utf8_string) },
1599 { "_WL_SELECTION", F(atom.wl_selection) },
1600 { "INCR", F(atom.incr) },
1601 { "TIMESTAMP", F(atom.timestamp) },
1602 { "MULTIPLE", F(atom.multiple) },
1603 { "UTF8_STRING" , F(atom.utf8_string) },
1604 { "COMPOUND_TEXT", F(atom.compound_text) },
1605 { "TEXT", F(atom.text) },
1606 { "STRING", F(atom.string) },
1607 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1608 { "text/plain", F(atom.text_plain) },
1609 };
1610#undef F
1611
1612 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1613 xcb_xfixes_query_version_reply_t *xfixes_reply;
1614 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1615 xcb_intern_atom_reply_t *reply;
1616 xcb_render_query_pict_formats_reply_t *formats_reply;
1617 xcb_render_query_pict_formats_cookie_t formats_cookie;
1618 xcb_render_pictforminfo_t *formats;
1619 uint32_t i;
1620
1621 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1622
1623 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1624
1625 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1626 cookies[i] = xcb_intern_atom (wm->conn, 0,
1627 strlen(atoms[i].name),
1628 atoms[i].name);
1629
1630 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1631 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1632 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1633 free(reply);
1634 }
1635
1636 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1637 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001638 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001639
1640 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1641 XCB_XFIXES_MAJOR_VERSION,
1642 XCB_XFIXES_MINOR_VERSION);
1643 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1644 xfixes_cookie, NULL);
1645
Martin Minarik6d118362012-06-07 18:01:59 +02001646 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001647 xfixes_reply->major_version, xfixes_reply->minor_version);
1648
1649 free(xfixes_reply);
1650
1651 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1652 formats_cookie, 0);
1653 if (formats_reply == NULL)
1654 return;
1655
1656 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001657 for (i = 0; i < formats_reply->num_formats; i++) {
1658 if (formats[i].direct.red_mask != 0xff &&
1659 formats[i].direct.red_shift != 16)
1660 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001661 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1662 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001663 wm->format_rgb = formats[i];
1664 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1665 formats[i].depth == 32 &&
1666 formats[i].direct.alpha_mask == 0xff &&
1667 formats[i].direct.alpha_shift == 24)
1668 wm->format_rgba = formats[i];
1669 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001670
1671 free(formats_reply);
1672}
1673
1674static void
1675weston_wm_create_wm_window(struct weston_wm *wm)
1676{
1677 static const char name[] = "Weston WM";
1678
1679 wm->wm_window = xcb_generate_id(wm->conn);
1680 xcb_create_window(wm->conn,
1681 XCB_COPY_FROM_PARENT,
1682 wm->wm_window,
1683 wm->screen->root,
1684 0, 0,
1685 10, 10,
1686 0,
1687 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1688 wm->screen->root_visual,
1689 0, NULL);
1690
1691 xcb_change_property(wm->conn,
1692 XCB_PROP_MODE_REPLACE,
1693 wm->wm_window,
1694 wm->atom.net_supporting_wm_check,
1695 XCB_ATOM_WINDOW,
1696 32, /* format */
1697 1, &wm->wm_window);
1698
1699 xcb_change_property(wm->conn,
1700 XCB_PROP_MODE_REPLACE,
1701 wm->wm_window,
1702 wm->atom.net_wm_name,
1703 wm->atom.utf8_string,
1704 8, /* format */
1705 strlen(name), name);
1706
1707 xcb_change_property(wm->conn,
1708 XCB_PROP_MODE_REPLACE,
1709 wm->screen->root,
1710 wm->atom.net_supporting_wm_check,
1711 XCB_ATOM_WINDOW,
1712 32, /* format */
1713 1, &wm->wm_window);
1714
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001715 /* Claim the WM_S0 selection even though we don't suport
1716 * the --replace functionality. */
1717 xcb_set_selection_owner(wm->conn,
1718 wm->wm_window,
1719 wm->atom.wm_s0,
1720 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001721
1722 xcb_set_selection_owner(wm->conn,
1723 wm->wm_window,
1724 wm->atom.net_wm_cm_s0,
1725 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001726}
1727
1728struct weston_wm *
1729weston_wm_create(struct weston_xserver *wxs)
1730{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001731 struct weston_wm *wm;
1732 struct wl_event_loop *loop;
1733 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001734 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001735 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001736 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001737
Peter Huttererf3d62272013-08-08 11:57:05 +10001738 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001739 if (wm == NULL)
1740 return NULL;
1741
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001742 wm->server = wxs;
1743 wm->window_hash = hash_table_create();
1744 if (wm->window_hash == NULL) {
1745 free(wm);
1746 return NULL;
1747 }
1748
1749 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001750 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001751 hash_table_destroy(wm->window_hash);
1752 free(wm);
1753 return NULL;
1754 }
1755
1756 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001757 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001758 close(sv[1]);
1759
1760 /* xcb_connect_to_fd takes ownership of the fd. */
1761 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1762 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001763 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001764 close(sv[0]);
1765 hash_table_destroy(wm->window_hash);
1766 free(wm);
1767 return NULL;
1768 }
1769
1770 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1771 wm->screen = s.data;
1772
1773 loop = wl_display_get_event_loop(wxs->wl_display);
1774 wm->source =
1775 wl_event_loop_add_fd(loop, sv[0],
1776 WL_EVENT_READABLE,
1777 weston_wm_handle_event, wm);
1778 wl_event_source_check(wm->source);
1779
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001780 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001781 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001782
1783 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001784 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1785 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1786 XCB_EVENT_MASK_PROPERTY_CHANGE;
1787 xcb_change_window_attributes(wm->conn, wm->screen->root,
1788 XCB_CW_EVENT_MASK, values);
1789 wm->theme = theme_create();
1790
1791 weston_wm_create_wm_window(wm);
1792
1793 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001794 supported[1] = wm->atom.net_wm_state;
1795 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001796 xcb_change_property(wm->conn,
1797 XCB_PROP_MODE_REPLACE,
1798 wm->screen->root,
1799 wm->atom.net_supported,
1800 XCB_ATOM_ATOM,
1801 32, /* format */
1802 ARRAY_LENGTH(supported), supported);
1803
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001804 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001805
1806 xcb_flush(wm->conn);
1807
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001808 wm->activate_listener.notify = weston_wm_window_activate;
1809 wl_signal_add(&wxs->compositor->activate_signal,
1810 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001811 wm->transform_listener.notify = weston_wm_window_transform;
1812 wl_signal_add(&wxs->compositor->transform_signal,
1813 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001814 wm->kill_listener.notify = weston_wm_kill_client;
1815 wl_signal_add(&wxs->compositor->kill_signal,
1816 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001817
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001818 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001819 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001820
Martin Minarik6d118362012-06-07 18:01:59 +02001821 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001822
1823 return wm;
1824}
1825
1826void
1827weston_wm_destroy(struct weston_wm *wm)
1828{
1829 /* FIXME: Free windows in hash. */
1830 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001831 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001832 xcb_disconnect(wm->conn);
1833 wl_event_source_remove(wm->source);
1834 wl_list_remove(&wm->selection_listener.link);
1835 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001836 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001837 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001838
1839 free(wm);
1840}
1841
1842static void
1843surface_destroy(struct wl_listener *listener, void *data)
1844{
1845 struct weston_wm_window *window =
1846 container_of(listener,
1847 struct weston_wm_window, surface_destroy_listener);
1848
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001849 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001850
1851 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001852}
1853
1854static struct weston_wm_window *
1855get_wm_window(struct weston_surface *surface)
1856{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001857 struct wl_listener *listener;
1858
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001859 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001860 if (listener)
1861 return container_of(listener, struct weston_wm_window,
1862 surface_destroy_listener);
1863
1864 return NULL;
1865}
1866
1867static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001868weston_wm_window_configure(void *data)
1869{
1870 struct weston_wm_window *window = data;
1871 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001872 uint32_t values[4];
1873 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001874
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001875 weston_wm_window_get_child_position(window, &x, &y);
1876 values[0] = x;
1877 values[1] = y;
1878 values[2] = window->width;
1879 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001880 xcb_configure_window(wm->conn,
1881 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001882 XCB_CONFIG_WINDOW_X |
1883 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001884 XCB_CONFIG_WINDOW_WIDTH |
1885 XCB_CONFIG_WINDOW_HEIGHT,
1886 values);
1887
1888 weston_wm_window_get_frame_size(window, &width, &height);
1889 values[0] = width;
1890 values[1] = height;
1891 xcb_configure_window(wm->conn,
1892 window->frame_id,
1893 XCB_CONFIG_WINDOW_WIDTH |
1894 XCB_CONFIG_WINDOW_HEIGHT,
1895 values);
1896
1897 window->configure_source = NULL;
1898
1899 weston_wm_window_schedule_repaint(window);
1900}
1901
1902static void
1903send_configure(struct weston_surface *surface,
1904 uint32_t edges, int32_t width, int32_t height)
1905{
1906 struct weston_wm_window *window = get_wm_window(surface);
1907 struct weston_wm *wm = window->wm;
1908 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001909 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001910
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001911 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001912 hborder = 0;
1913 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001914 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001915 hborder = 2 * (t->margin + t->width);
1916 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001917 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001918 hborder = 2 * t->margin;
1919 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001920 }
1921
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001922 if (width > hborder)
1923 window->width = width - hborder;
1924 else
1925 window->width = 1;
1926
1927 if (height > vborder)
1928 window->height = height - vborder;
1929 else
1930 window->height = 1;
1931
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001932 if (window->configure_source)
1933 return;
1934
1935 window->configure_source =
1936 wl_event_loop_add_idle(wm->server->loop,
1937 weston_wm_window_configure, window);
1938}
1939
1940static const struct weston_shell_client shell_client = {
1941 send_configure
1942};
1943
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001944static int
1945legacy_fullscreen(struct weston_wm *wm,
1946 struct weston_wm_window *window,
1947 struct weston_output **output_ret)
1948{
1949 struct weston_compositor *compositor = wm->server->compositor;
1950 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001951 uint32_t minmax = PMinSize | PMaxSize;
1952 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001953
1954 /* Heuristics for detecting legacy fullscreen windows... */
1955
1956 wl_list_for_each(output, &compositor->output_list, link) {
1957 if (output->x == window->x &&
1958 output->y == window->y &&
1959 output->width == window->width &&
1960 output->height == window->height &&
1961 window->override_redirect) {
1962 *output_ret = output;
1963 return 1;
1964 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001965
1966 matching_size = 0;
1967 if ((window->size_hints.flags & (USSize |PSize)) &&
1968 window->size_hints.width == output->width &&
1969 window->size_hints.height == output->height)
1970 matching_size = 1;
1971 if ((window->size_hints.flags & minmax) == minmax &&
1972 window->size_hints.min_width == output->width &&
1973 window->size_hints.min_height == output->height &&
1974 window->size_hints.max_width == output->width &&
1975 window->size_hints.max_height == output->height)
1976 matching_size = 1;
1977
1978 if (matching_size && !window->decorate &&
1979 (window->size_hints.flags & (USPosition | PPosition)) &&
1980 window->size_hints.x == output->x &&
1981 window->size_hints.y == output->y) {
1982 *output_ret = output;
1983 return 1;
1984 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001985 }
1986
1987 return 0;
1988}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001989static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001990xserver_map_shell_surface(struct weston_wm *wm,
1991 struct weston_wm_window *window)
1992{
1993 struct weston_shell_interface *shell_interface =
1994 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001995 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001996
1997 if (!shell_interface->create_shell_surface)
1998 return;
1999
2000 window->shsurf =
2001 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002002 window->surface,
2003 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002004
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002005 if (window->fullscreen) {
2006 window->saved_width = window->width;
2007 window->saved_height = window->height;
2008 shell_interface->set_fullscreen(window->shsurf,
2009 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2010 0, NULL);
2011 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002012 } else if (legacy_fullscreen(wm, window, &output)) {
2013 window->fullscreen = 1;
2014 shell_interface->set_fullscreen(window->shsurf,
2015 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2016 0, output);
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002017 } else if (!window->override_redirect) {
2018 shell_interface->set_toplevel(window->shsurf);
2019 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002020 } else {
2021 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002022 window->x,
2023 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002024 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002025 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002026}
2027
2028static void
2029xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2030 struct wl_resource *surface_resource, uint32_t id)
2031{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002032 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002033 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002034 struct weston_surface *surface =
2035 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002036 struct weston_wm_window *window;
2037
2038 if (client != wxs->client)
2039 return;
2040
2041 window = hash_table_lookup(wm->window_hash, id);
2042 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002043 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002044 return;
2045 }
2046
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002047 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002048
2049 weston_wm_window_read_properties(window);
2050
2051 window->surface = (struct weston_surface *) surface;
2052 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002053 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002054 &window->surface_destroy_listener);
2055
2056 weston_wm_window_schedule_repaint(window);
2057 xserver_map_shell_surface(wm, window);
2058}
2059
2060const struct xserver_interface xserver_implementation = {
2061 xserver_set_window_id
2062};