blob: 3b88dac50cdc2ed60ace001f04525140ceb739ad [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
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400118struct weston_wm_window {
119 struct weston_wm *wm;
120 xcb_window_t id;
121 xcb_window_t frame_id;
122 cairo_surface_t *cairo_surface;
123 struct weston_surface *surface;
124 struct shell_surface *shsurf;
125 struct wl_listener surface_destroy_listener;
126 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400127 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400128 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300129 int pid;
130 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 char *class;
132 char *name;
133 struct weston_wm_window *transient_for;
134 uint32_t protocols;
135 xcb_atom_t type;
136 int width, height;
137 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500138 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400139 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300140 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500141 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500142 int has_alpha;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700143 struct wm_size_hints size_hints;
144 struct motif_wm_hints motif_hints;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400145};
146
147static struct weston_wm_window *
148get_wm_window(struct weston_surface *surface);
149
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400150static void
151weston_wm_window_schedule_repaint(struct weston_wm_window *window);
152
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400153static int __attribute__ ((format (printf, 1, 2)))
154wm_log(const char *fmt, ...)
155{
156#ifdef WM_DEBUG
157 int l;
158 va_list argp;
159
160 va_start(argp, fmt);
161 l = weston_vlog(fmt, argp);
162 va_end(argp);
163
164 return l;
165#else
166 return 0;
167#endif
168}
169
170static int __attribute__ ((format (printf, 1, 2)))
171wm_log_continue(const char *fmt, ...)
172{
173#ifdef WM_DEBUG
174 int l;
175 va_list argp;
176
177 va_start(argp, fmt);
178 l = weston_vlog_continue(fmt, argp);
179 va_end(argp);
180
181 return l;
182#else
183 return 0;
184#endif
185}
186
187
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400188const char *
189get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
190{
191 xcb_get_atom_name_cookie_t cookie;
192 xcb_get_atom_name_reply_t *reply;
193 xcb_generic_error_t *e;
194 static char buffer[64];
195
196 if (atom == XCB_ATOM_NONE)
197 return "None";
198
199 cookie = xcb_get_atom_name (c, atom);
200 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500201
202 if(reply) {
203 snprintf(buffer, sizeof buffer, "%.*s",
204 xcb_get_atom_name_name_length (reply),
205 xcb_get_atom_name_name (reply));
206 } else {
207 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
208 }
209
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400210 free(reply);
211
212 return buffer;
213}
214
Tiago Vignatti90fada42012-07-16 12:02:08 -0400215static xcb_cursor_t
216xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
217{
218 xcb_connection_t *c = wm->conn;
219 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
220 xcb_screen_t *screen = s.data;
221 xcb_gcontext_t gc;
222 xcb_pixmap_t pix;
223 xcb_render_picture_t pic;
224 xcb_cursor_t cursor;
225 int stride = img->width * 4;
226
227 pix = xcb_generate_id(c);
228 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
229
230 pic = xcb_generate_id(c);
231 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
232
233 gc = xcb_generate_id(c);
234 xcb_create_gc(c, gc, pix, 0, 0);
235
236 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
237 img->width, img->height, 0, 0, 0, 32,
238 stride * img->height, (uint8_t *) img->pixels);
239 xcb_free_gc(c, gc);
240
241 cursor = xcb_generate_id(c);
242 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
243
244 xcb_render_free_picture(c, pic);
245 xcb_free_pixmap(c, pix);
246
247 return cursor;
248}
249
250static xcb_cursor_t
251xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
252{
253 /* TODO: treat animated cursors as well */
254 if (images->nimage != 1)
255 return -1;
256
257 return xcb_cursor_image_load_cursor(wm, images->images[0]);
258}
259
260static xcb_cursor_t
261xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
262{
263 xcb_cursor_t cursor;
264 XcursorImages *images;
265 char *v = NULL;
266 int size = 0;
267
268 if (!file)
269 return 0;
270
271 v = getenv ("XCURSOR_SIZE");
272 if (v)
273 size = atoi(v);
274
275 if (!size)
276 size = 32;
277
278 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300279 if (!images)
280 return -1;
281
Tiago Vignatti90fada42012-07-16 12:02:08 -0400282 cursor = xcb_cursor_images_load_cursor (wm, images);
283 XcursorImagesDestroy (images);
284
285 return cursor;
286}
287
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400288void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400289dump_property(struct weston_wm *wm,
290 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400291{
292 int32_t *incr_value;
293 const char *text_value, *name;
294 xcb_atom_t *atom_value;
295 int width, len;
296 uint32_t i;
297
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400298 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400299 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400300 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400301 return;
302 }
303
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400304 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
305 get_atom_name(wm->conn, reply->type),
306 reply->format,
307 xcb_get_property_value_length(reply),
308 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400309
310 if (reply->type == wm->atom.incr) {
311 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400312 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400313 } else if (reply->type == wm->atom.utf8_string ||
314 reply->type == wm->atom.string) {
315 text_value = xcb_get_property_value(reply);
316 if (reply->value_len > 40)
317 len = 40;
318 else
319 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400320 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400321 } else if (reply->type == XCB_ATOM_ATOM) {
322 atom_value = xcb_get_property_value(reply);
323 for (i = 0; i < reply->value_len; i++) {
324 name = get_atom_name(wm->conn, atom_value[i]);
325 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400326 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400327 width = 4;
328 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400329 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400330 }
331
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400332 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400333 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400334 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400335 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400336 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400337 }
338}
339
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200340static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400341read_and_dump_property(struct weston_wm *wm,
342 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400343{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400344 xcb_get_property_reply_t *reply;
345 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400347 cookie = xcb_get_property(wm->conn, 0, window,
348 property, XCB_ATOM_ANY, 0, 2048);
349 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400350
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400351 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400352
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400353 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354}
355
356/* We reuse some predefined, but otherwise useles atoms */
357#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
358#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500359#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700360#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361
362static void
363weston_wm_window_read_properties(struct weston_wm_window *window)
364{
365 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200366 struct weston_shell_interface *shell_interface =
367 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400368
369#define F(field) offsetof(struct weston_wm_window, field)
370 const struct {
371 xcb_atom_t atom;
372 xcb_atom_t type;
373 int offset;
374 } props[] = {
375 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
376 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
377 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
378 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700379 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500380 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
382 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300383 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400384 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300385 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400386 };
387#undef F
388
389 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
390 xcb_get_property_reply_t *reply;
391 void *p;
392 uint32_t *xid;
393 xcb_atom_t *atom;
394 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395
396 if (!window->properties_dirty)
397 return;
398 window->properties_dirty = 0;
399
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400400 for (i = 0; i < ARRAY_LENGTH(props); i++)
401 cookie[i] = xcb_get_property(wm->conn,
402 0, /* delete */
403 window->id,
404 props[i].atom,
405 XCB_ATOM_ANY, 0, 2048);
406
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300407 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700408 window->size_hints.flags = 0;
409 window->motif_hints.flags = 0;
410
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 for (i = 0; i < ARRAY_LENGTH(props); i++) {
412 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
413 if (!reply)
414 /* Bad window, typically */
415 continue;
416 if (reply->type == XCB_ATOM_NONE) {
417 /* No such property */
418 free(reply);
419 continue;
420 }
421
422 p = ((char *) window + props[i].offset);
423
424 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300425 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400426 case XCB_ATOM_STRING:
427 /* FIXME: We're using this for both string and
428 utf8_string */
429 if (*(char **) p)
430 free(*(char **) p);
431
432 *(char **) p =
433 strndup(xcb_get_property_value(reply),
434 xcb_get_property_value_length(reply));
435 break;
436 case XCB_ATOM_WINDOW:
437 xid = xcb_get_property_value(reply);
438 *(struct weston_wm_window **) p =
439 hash_table_lookup(wm->window_hash, *xid);
440 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300441 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400442 case XCB_ATOM_ATOM:
443 atom = xcb_get_property_value(reply);
444 *(xcb_atom_t *) p = *atom;
445 break;
446 case TYPE_WM_PROTOCOLS:
447 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700448 case TYPE_WM_NORMAL_HINTS:
449 memcpy(&window->size_hints,
450 xcb_get_property_value(reply),
451 sizeof window->size_hints);
452 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500453 case TYPE_NET_WM_STATE:
454 window->fullscreen = 0;
455 atom = xcb_get_property_value(reply);
456 for (i = 0; i < reply->value_len; i++)
457 if (atom[i] == wm->atom.net_wm_state_fullscreen)
458 window->fullscreen = 1;
459 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400460 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700461 memcpy(&window->motif_hints,
462 xcb_get_property_value(reply),
463 sizeof window->motif_hints);
464 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
465 window->decorate =
466 window->motif_hints.decorations > 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400467 break;
468 default:
469 break;
470 }
471 free(reply);
472 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200473
474 if (window->shsurf && window->name)
475 shell_interface->set_title(window->shsurf, window->name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400476}
477
478static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400479weston_wm_window_get_frame_size(struct weston_wm_window *window,
480 int *width, int *height)
481{
482 struct theme *t = window->wm->theme;
483
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500484 if (window->fullscreen) {
485 *width = window->width;
486 *height = window->height;
487 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400488 *width = window->width + (t->margin + t->width) * 2;
489 *height = window->height +
490 t->margin * 2 + t->width + t->titlebar_height;
491 } else {
492 *width = window->width + t->margin * 2;
493 *height = window->height + t->margin * 2;
494 }
495}
496
497static void
498weston_wm_window_get_child_position(struct weston_wm_window *window,
499 int *x, int *y)
500{
501 struct theme *t = window->wm->theme;
502
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500503 if (window->fullscreen) {
504 *x = 0;
505 *y = 0;
506 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400507 *x = t->margin + t->width;
508 *y = t->margin + t->titlebar_height;
509 } else {
510 *x = t->margin;
511 *y = t->margin;
512 }
513}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400514
515static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500516weston_wm_window_send_configure_notify(struct weston_wm_window *window)
517{
518 xcb_configure_notify_event_t configure_notify;
519 struct weston_wm *wm = window->wm;
520 int x, y;
521
522 weston_wm_window_get_child_position(window, &x, &y);
523 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
524 configure_notify.pad0 = 0;
525 configure_notify.event = window->id;
526 configure_notify.window = window->id;
527 configure_notify.above_sibling = XCB_WINDOW_NONE;
528 configure_notify.x = x;
529 configure_notify.y = y;
530 configure_notify.width = window->width;
531 configure_notify.height = window->height;
532 configure_notify.border_width = 0;
533 configure_notify.override_redirect = 0;
534 configure_notify.pad1 = 0;
535
536 xcb_send_event(wm->conn, 0, window->id,
537 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
538 (char *) &configure_notify);
539}
540
541static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400542weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
543{
544 xcb_configure_request_event_t *configure_request =
545 (xcb_configure_request_event_t *) event;
546 struct weston_wm_window *window;
547 uint32_t mask, values[16];
548 int x, y, width, height, i = 0;
549
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400550 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
551 configure_request->window,
552 configure_request->x, configure_request->y,
553 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400554
555 window = hash_table_lookup(wm->window_hash, configure_request->window);
556
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500557 if (window->fullscreen) {
558 weston_wm_window_send_configure_notify(window);
559 return;
560 }
561
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400562 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
563 window->width = configure_request->width;
564 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
565 window->height = configure_request->height;
566
567 weston_wm_window_get_child_position(window, &x, &y);
568 values[i++] = x;
569 values[i++] = y;
570 values[i++] = window->width;
571 values[i++] = window->height;
572 values[i++] = 0;
573 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
574 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
575 XCB_CONFIG_WINDOW_BORDER_WIDTH;
576 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
577 values[i++] = configure_request->sibling;
578 mask |= XCB_CONFIG_WINDOW_SIBLING;
579 }
580 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
581 values[i++] = configure_request->stack_mode;
582 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
583 }
584
585 xcb_configure_window(wm->conn, window->id, mask, values);
586
587 weston_wm_window_get_frame_size(window, &width, &height);
588 values[0] = width;
589 values[1] = height;
590 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
591 xcb_configure_window(wm->conn, window->frame_id, mask, values);
592
593 weston_wm_window_schedule_repaint(window);
594}
595
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400596static int
597our_resource(struct weston_wm *wm, uint32_t id)
598{
599 const xcb_setup_t *setup;
600
601 setup = xcb_get_setup(wm->conn);
602
603 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
604}
605
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400606static void
607weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
608{
609 xcb_configure_notify_event_t *configure_notify =
610 (xcb_configure_notify_event_t *) event;
611 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300612 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400613
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400614 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400615 configure_notify->window,
616 configure_notify->x, configure_notify->y,
617 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300618
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400619 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300620 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700621 window->x = configure_notify->x;
622 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700623 if (window->override_redirect) {
624 window->width = configure_notify->width;
625 window->height = configure_notify->height;
626 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400627}
628
629static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300630weston_wm_kill_client(struct wl_listener *listener, void *data)
631{
632 struct weston_surface *surface = data;
633 struct weston_wm_window *window = get_wm_window(surface);
634 char name[1024];
635
636 if (!window)
637 return;
638
639 gethostname(name, 1024);
640
641 /* this is only one heuristic to guess the PID of a client is valid,
642 * assuming it's compliant with icccm and ewmh. Non-compliants and
643 * remote applications of course fail. */
644 if (!strcmp(window->machine, name) && window->pid != 0)
645 kill(window->pid, SIGKILL);
646}
647
648static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400649weston_wm_window_activate(struct wl_listener *listener, void *data)
650{
651 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200652 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600653 struct weston_wm *wm =
654 container_of(listener, struct weston_wm, activate_listener);
655 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400656
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200657 if (surface) {
658 window = get_wm_window(surface);
659 }
660
Scott Moreau85ecac02012-05-21 15:49:14 -0600661 if (window) {
662 client_message.response_type = XCB_CLIENT_MESSAGE;
663 client_message.format = 32;
664 client_message.window = window->id;
665 client_message.type = wm->atom.wm_protocols;
666 client_message.data.data32[0] = wm->atom.wm_take_focus;
667 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
668
669 xcb_send_event(wm->conn, 0, window->id,
670 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
671 (char *) &client_message);
672
673 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
674 window->id, XCB_TIME_CURRENT_TIME);
675 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400676 xcb_set_input_focus (wm->conn,
677 XCB_INPUT_FOCUS_POINTER_ROOT,
678 XCB_NONE,
679 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600680 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400681
682 if (wm->focus_window)
683 weston_wm_window_schedule_repaint(wm->focus_window);
684 wm->focus_window = window;
685 if (wm->focus_window)
686 weston_wm_window_schedule_repaint(wm->focus_window);
687}
688
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300689static void
690weston_wm_window_transform(struct wl_listener *listener, void *data)
691{
692 struct weston_surface *surface = data;
693 struct weston_wm_window *window = get_wm_window(surface);
694 struct weston_wm *wm =
695 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300696 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300697
698 if (!window || !wm)
699 return;
700
701 if (!weston_surface_is_mapped(surface))
702 return;
703
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700704 if (window->x != surface->geometry.x ||
705 window->y != surface->geometry.y) {
706 values[0] = surface->geometry.x;
707 values[1] = surface->geometry.y;
708 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300709
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700710 xcb_configure_window(wm->conn, window->frame_id, mask, values);
711 xcb_flush(wm->conn);
712 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300713}
714
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400715#define ICCCM_WITHDRAWN_STATE 0
716#define ICCCM_NORMAL_STATE 1
717#define ICCCM_ICONIC_STATE 3
718
719static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500720weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400721{
722 struct weston_wm *wm = window->wm;
723 uint32_t property[2];
724
725 property[0] = state;
726 property[1] = XCB_WINDOW_NONE;
727
728 xcb_change_property(wm->conn,
729 XCB_PROP_MODE_REPLACE,
730 window->id,
731 wm->atom.wm_state,
732 wm->atom.wm_state,
733 32, /* format */
734 2, property);
735}
736
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500738weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
739{
740 struct weston_wm *wm = window->wm;
741 uint32_t property[1];
742 int i;
743
744 i = 0;
745 if (window->fullscreen)
746 property[i++] = wm->atom.net_wm_state_fullscreen;
747
748 xcb_change_property(wm->conn,
749 XCB_PROP_MODE_REPLACE,
750 window->id,
751 wm->atom.net_wm_state,
752 XCB_ATOM_ATOM,
753 32, /* format */
754 i, property);
755}
756
757static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700758weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400759{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700760 struct weston_wm *wm = window->wm;
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
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764 weston_wm_window_get_frame_size(window, &width, &height);
765 weston_wm_window_get_child_position(window, &x, &y);
766
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400767 values[0] = wm->screen->black_pixel;
768 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400769 XCB_EVENT_MASK_KEY_PRESS |
770 XCB_EVENT_MASK_KEY_RELEASE |
771 XCB_EVENT_MASK_BUTTON_PRESS |
772 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400773 XCB_EVENT_MASK_POINTER_MOTION |
774 XCB_EVENT_MASK_ENTER_WINDOW |
775 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400776 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400777 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400778 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400779
780 window->frame_id = xcb_generate_id(wm->conn);
781 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400782 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400783 window->frame_id,
784 wm->screen->root,
785 0, 0,
786 width, height,
787 0,
788 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400789 wm->visual_id,
790 XCB_CW_BORDER_PIXEL |
791 XCB_CW_EVENT_MASK |
792 XCB_CW_COLORMAP, values);
793
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400794 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
795
796 values[0] = 0;
797 xcb_configure_window(wm->conn, window->id,
798 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
799
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400800 window->cairo_surface =
801 cairo_xcb_surface_create_with_xrender_format(wm->conn,
802 wm->screen,
803 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400804 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400805 width, height);
806
807 hash_table_insert(wm->window_hash, window->frame_id, window);
808}
809
810static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700811weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
812{
813 xcb_map_request_event_t *map_request =
814 (xcb_map_request_event_t *) event;
815 struct weston_wm_window *window;
816
817 if (our_resource(wm, map_request->window)) {
818 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
819 map_request->window);
820 return;
821 }
822
823 window = hash_table_lookup(wm->window_hash, map_request->window);
824
825 weston_wm_window_read_properties(window);
826
827 if (window->frame_id == XCB_WINDOW_NONE)
828 weston_wm_window_create_frame(window);
829
830 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
831 window->id, window, window->frame_id);
832
833 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
834 weston_wm_window_set_net_wm_state(window);
835
836 xcb_map_window(wm->conn, map_request->window);
837 xcb_map_window(wm->conn, window->frame_id);
838}
839
840static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400841weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
842{
843 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
844
845 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400846 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
847 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400848 return;
849 }
850
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400851 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400852}
853
854static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400855weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
856{
857 xcb_unmap_notify_event_t *unmap_notify =
858 (xcb_unmap_notify_event_t *) event;
859 struct weston_wm_window *window;
860
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400861 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
862 unmap_notify->window,
863 unmap_notify->event,
864 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400865
866 if (our_resource(wm, unmap_notify->window))
867 return;
868
MoD31700122013-06-11 19:58:55 -0500869 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400870 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
871 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400872 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400873
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400874 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400875 if (wm->focus_window == window)
876 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400877 if (window->surface)
878 wl_list_remove(&window->surface_destroy_listener.link);
879 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200880 window->shsurf = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700881 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400882}
883
884static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400885weston_wm_window_draw_decoration(void *data)
886{
887 struct weston_wm_window *window = data;
888 struct weston_wm *wm = window->wm;
889 struct theme *t = wm->theme;
890 cairo_t *cr;
891 int x, y, width, height;
892 const char *title;
893 uint32_t flags = 0;
894
895 weston_wm_window_read_properties(window);
896
897 window->repaint_source = NULL;
898
899 weston_wm_window_get_frame_size(window, &width, &height);
900 weston_wm_window_get_child_position(window, &x, &y);
901
902 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
903 cr = cairo_create(window->cairo_surface);
904
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500905 if (window->fullscreen) {
906 /* nothing */
907 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400908 if (wm->focus_window == window)
909 flags |= THEME_FRAME_ACTIVE;
910
911 if (window->name)
912 title = window->name;
913 else
914 title = "untitled";
915
916 theme_render_frame(t, cr, width, height, title, flags);
917 } else {
918 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
919 cairo_set_source_rgba(cr, 0, 0, 0, 0);
920 cairo_paint(cr);
921
922 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
923 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
924 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
925 }
926
927 cairo_destroy(cr);
928
929 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700930 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500931 if(window->has_alpha) {
932 pixman_region32_init(&window->surface->pending.opaque);
933 } else {
934 /* We leave an extra pixel around the X window area to
935 * make sure we don't sample from the undefined alpha
936 * channel when filtering. */
937 pixman_region32_init_rect(&window->surface->pending.opaque,
938 x - 1, y - 1,
939 window->width + 2,
940 window->height + 2);
941 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200942 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500943 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400944
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500945 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500946 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500947 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948 t->margin, t->margin,
949 width - 2 * t->margin,
950 height - 2 * t->margin);
951 }
952}
953
954static void
955weston_wm_window_schedule_repaint(struct weston_wm_window *window)
956{
957 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300958 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400959
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400960 if (window->frame_id == XCB_WINDOW_NONE) {
961 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300962 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700963 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500964 if(window->has_alpha) {
965 pixman_region32_init(&window->surface->pending.opaque);
966 } else {
967 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
968 width, height);
969 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200970 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400971 }
972 return;
973 }
974
975 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400976 return;
977
978 window->repaint_source =
979 wl_event_loop_add_idle(wm->server->loop,
980 weston_wm_window_draw_decoration,
981 window);
982}
983
984static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400985weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
986{
987 xcb_property_notify_event_t *property_notify =
988 (xcb_property_notify_event_t *) event;
989 struct weston_wm_window *window;
990
991 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000992 if (!window)
993 return;
994
995 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400996
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400997 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400998 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400999 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001000 else
1001 read_and_dump_property(wm, property_notify->window,
1002 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001003
1004 if (property_notify->atom == wm->atom.net_wm_name ||
1005 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001006 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001007}
1008
1009static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001010weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001011 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001012{
1013 struct weston_wm_window *window;
1014 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001015 xcb_get_geometry_cookie_t geometry_cookie;
1016 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001017
Peter Huttererf3d62272013-08-08 11:57:05 +10001018 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001019 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001020 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001021 return;
1022 }
1023
MoD384a11a2013-06-22 11:04:21 -05001024 geometry_cookie = xcb_get_geometry(wm->conn, id);
1025
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001026 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1027 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1028
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001029 window->wm = wm;
1030 window->id = id;
1031 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001032 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001033 window->width = width;
1034 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001035 window->x = x;
1036 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001037
MoD384a11a2013-06-22 11:04:21 -05001038 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1039 /* technically we should use XRender and check the visual format's
1040 alpha_mask, but checking depth is simpler and works in all known cases */
1041 if(geometry_reply != NULL)
1042 window->has_alpha = geometry_reply->depth == 32;
1043 free(geometry_reply);
1044
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001045 hash_table_insert(wm->window_hash, id, window);
1046}
1047
1048static void
1049weston_wm_window_destroy(struct weston_wm_window *window)
1050{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001051 struct weston_wm *wm = window->wm;
1052
1053 if (window->repaint_source)
1054 wl_event_source_remove(window->repaint_source);
1055 if (window->cairo_surface)
1056 cairo_surface_destroy(window->cairo_surface);
1057
1058 if (window->frame_id) {
1059 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1060 xcb_destroy_window(wm->conn, window->frame_id);
1061 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1062 hash_table_remove(wm->window_hash, window->frame_id);
1063 window->frame_id = XCB_WINDOW_NONE;
1064 }
1065
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001066 hash_table_remove(window->wm->window_hash, window->id);
1067 free(window);
1068}
1069
1070static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001071weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1072{
1073 xcb_create_notify_event_t *create_notify =
1074 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001075
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001076 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1077 create_notify->window,
1078 create_notify->width, create_notify->height,
1079 create_notify->override_redirect ? ", override" : "",
1080 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001081
1082 if (our_resource(wm, create_notify->window))
1083 return;
1084
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001085 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001086 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001087 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001088 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001089}
1090
1091static void
1092weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1093{
1094 xcb_destroy_notify_event_t *destroy_notify =
1095 (xcb_destroy_notify_event_t *) event;
1096 struct weston_wm_window *window;
1097
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001098 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1099 destroy_notify->window,
1100 destroy_notify->event,
1101 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001102
1103 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001104 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001105
1106 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001107 weston_wm_window_destroy(window);
1108}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001109
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001110static void
1111weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1112{
1113 xcb_reparent_notify_event_t *reparent_notify =
1114 (xcb_reparent_notify_event_t *) event;
1115 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001116
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001117 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1118 reparent_notify->window,
1119 reparent_notify->parent,
1120 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001121
1122 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001123 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001124 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001125 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001126 } else if (!our_resource(wm, reparent_notify->parent)) {
1127 window = hash_table_lookup(wm->window_hash,
1128 reparent_notify->window);
1129 weston_wm_window_destroy(window);
1130 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001131}
1132
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001133struct weston_seat *
1134weston_wm_pick_seat(struct weston_wm *wm)
1135{
1136 return container_of(wm->server->compositor->seat_list.next,
1137 struct weston_seat, link);
1138}
1139
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001140static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001141weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1142 xcb_client_message_event_t *client_message)
1143{
1144 static const int map[] = {
1145 THEME_LOCATION_RESIZING_TOP_LEFT,
1146 THEME_LOCATION_RESIZING_TOP,
1147 THEME_LOCATION_RESIZING_TOP_RIGHT,
1148 THEME_LOCATION_RESIZING_RIGHT,
1149 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1150 THEME_LOCATION_RESIZING_BOTTOM,
1151 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1152 THEME_LOCATION_RESIZING_LEFT
1153 };
1154
1155 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001156 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001157 int detail;
1158 struct weston_shell_interface *shell_interface =
1159 &wm->server->compositor->shell_interface;
1160
Kristian Høgsberge3148752013-05-06 23:19:49 -04001161 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001162 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001163 return;
1164
1165 detail = client_message->data.data32[2];
1166 switch (detail) {
1167 case _NET_WM_MOVERESIZE_MOVE:
1168 shell_interface->move(window->shsurf, seat);
1169 break;
1170 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1171 case _NET_WM_MOVERESIZE_SIZE_TOP:
1172 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1173 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1174 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1175 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1176 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1177 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1178 shell_interface->resize(window->shsurf, seat, map[detail]);
1179 break;
1180 case _NET_WM_MOVERESIZE_CANCEL:
1181 break;
1182 }
1183}
1184
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001185#define _NET_WM_STATE_REMOVE 0
1186#define _NET_WM_STATE_ADD 1
1187#define _NET_WM_STATE_TOGGLE 2
1188
1189static int
1190update_state(int action, int *state)
1191{
1192 int new_state, changed;
1193
1194 switch (action) {
1195 case _NET_WM_STATE_REMOVE:
1196 new_state = 0;
1197 break;
1198 case _NET_WM_STATE_ADD:
1199 new_state = 1;
1200 break;
1201 case _NET_WM_STATE_TOGGLE:
1202 new_state = !*state;
1203 break;
1204 default:
1205 return 0;
1206 }
1207
1208 changed = (*state != new_state);
1209 *state = new_state;
1210
1211 return changed;
1212}
1213
1214static void
1215weston_wm_window_configure(void *data);
1216
1217static void
1218weston_wm_window_handle_state(struct weston_wm_window *window,
1219 xcb_client_message_event_t *client_message)
1220{
1221 struct weston_wm *wm = window->wm;
1222 struct weston_shell_interface *shell_interface =
1223 &wm->server->compositor->shell_interface;
1224 uint32_t action, property;
1225
1226 action = client_message->data.data32[0];
1227 property = client_message->data.data32[1];
1228
1229 if (property == wm->atom.net_wm_state_fullscreen &&
1230 update_state(action, &window->fullscreen)) {
1231 weston_wm_window_set_net_wm_state(window);
1232 if (window->fullscreen) {
1233 window->saved_width = window->width;
1234 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001235
1236 if (window->shsurf)
1237 shell_interface->set_fullscreen(window->shsurf,
1238 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1239 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001240 } else {
1241 shell_interface->set_toplevel(window->shsurf);
1242 window->width = window->saved_width;
1243 window->height = window->saved_height;
1244 weston_wm_window_configure(window);
1245 }
1246 }
1247}
1248
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001249static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001250weston_wm_handle_client_message(struct weston_wm *wm,
1251 xcb_generic_event_t *event)
1252{
1253 xcb_client_message_event_t *client_message =
1254 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001255 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001256
1257 window = hash_table_lookup(wm->window_hash, client_message->window);
1258
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001259 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1260 get_atom_name(wm->conn, client_message->type),
1261 client_message->data.data32[0],
1262 client_message->data.data32[1],
1263 client_message->data.data32[2],
1264 client_message->data.data32[3],
1265 client_message->data.data32[4],
1266 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001267
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001268 if (client_message->type == wm->atom.net_wm_moveresize)
1269 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001270 else if (client_message->type == wm->atom.net_wm_state)
1271 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001272}
1273
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001274enum cursor_type {
1275 XWM_CURSOR_TOP,
1276 XWM_CURSOR_BOTTOM,
1277 XWM_CURSOR_LEFT,
1278 XWM_CURSOR_RIGHT,
1279 XWM_CURSOR_TOP_LEFT,
1280 XWM_CURSOR_TOP_RIGHT,
1281 XWM_CURSOR_BOTTOM_LEFT,
1282 XWM_CURSOR_BOTTOM_RIGHT,
1283 XWM_CURSOR_LEFT_PTR,
1284};
1285
1286static const char *cursors[] = {
1287 "top_side",
1288 "bottom_side",
1289 "left_side",
1290 "right_side",
1291 "top_left_corner",
1292 "top_right_corner",
1293 "bottom_left_corner",
1294 "bottom_right_corner",
1295 "left_ptr"
1296};
1297
1298static void
1299weston_wm_create_cursors(struct weston_wm *wm)
1300{
1301 int i, count = ARRAY_LENGTH(cursors);
1302
1303 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1304 for (i = 0; i < count; i++) {
1305 wm->cursors[i] =
1306 xcb_cursor_library_load_cursor(wm, cursors[i]);
1307 }
1308
1309 wm->last_cursor = -1;
1310}
1311
1312static void
1313weston_wm_destroy_cursors(struct weston_wm *wm)
1314{
1315 uint8_t i;
1316
1317 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1318 xcb_free_cursor(wm->conn, wm->cursors[i]);
1319
1320 free(wm->cursors);
1321}
1322
1323static int
1324get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1325{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001326 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001327
1328 switch (location) {
1329 case THEME_LOCATION_RESIZING_TOP:
1330 return XWM_CURSOR_TOP;
1331 case THEME_LOCATION_RESIZING_BOTTOM:
1332 return XWM_CURSOR_BOTTOM;
1333 case THEME_LOCATION_RESIZING_LEFT:
1334 return XWM_CURSOR_LEFT;
1335 case THEME_LOCATION_RESIZING_RIGHT:
1336 return XWM_CURSOR_RIGHT;
1337 case THEME_LOCATION_RESIZING_TOP_LEFT:
1338 return XWM_CURSOR_TOP_LEFT;
1339 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1340 return XWM_CURSOR_TOP_RIGHT;
1341 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1342 return XWM_CURSOR_BOTTOM_LEFT;
1343 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1344 return XWM_CURSOR_BOTTOM_RIGHT;
1345 case THEME_LOCATION_EXTERIOR:
1346 case THEME_LOCATION_TITLEBAR:
1347 default:
1348 return XWM_CURSOR_LEFT_PTR;
1349 }
1350}
1351
1352static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001353weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1354 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001355{
1356 uint32_t cursor_value_list;
1357
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001358 if (wm->last_cursor == cursor)
1359 return;
1360
1361 wm->last_cursor = cursor;
1362
1363 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001364 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001365 XCB_CW_CURSOR, &cursor_value_list);
1366 xcb_flush(wm->conn);
1367}
1368
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001369static void
1370weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1371{
1372 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1373 struct weston_shell_interface *shell_interface =
1374 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001375 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001376 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001377 enum theme_location location;
1378 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001379 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001380
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001381 wm_log("XCB_BUTTON_%s (detail %d)\n",
1382 button->response_type == XCB_BUTTON_PRESS ?
1383 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001384
1385 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001386 weston_wm_window_get_frame_size(window, &width, &height);
1387
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001388 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001389 button->detail == 1) {
1390 location = theme_get_location(t,
1391 button->event_x,
1392 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001393 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001394
1395 switch (location) {
1396 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001397 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001398 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001399 case THEME_LOCATION_RESIZING_TOP:
1400 case THEME_LOCATION_RESIZING_BOTTOM:
1401 case THEME_LOCATION_RESIZING_LEFT:
1402 case THEME_LOCATION_RESIZING_RIGHT:
1403 case THEME_LOCATION_RESIZING_TOP_LEFT:
1404 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1405 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1406 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1407 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001408 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001409 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001410 default:
1411 break;
1412 }
1413 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001414}
1415
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001416static void
1417weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1418{
1419 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1420 struct weston_wm_window *window;
1421 int cursor, width, height;
1422
1423 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001424 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001425 return;
1426
1427 weston_wm_window_get_frame_size(window, &width, &height);
1428 cursor = get_cursor_for_location(wm->theme, width, height,
1429 motion->event_x, motion->event_y);
1430
Tiago Vignattic1903232012-07-16 12:15:37 -04001431 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001432}
1433
1434static void
1435weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1436{
1437 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1438 struct weston_wm_window *window;
1439 int cursor, width, height;
1440
1441 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001442 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001443 return;
1444
1445 weston_wm_window_get_frame_size(window, &width, &height);
1446 cursor = get_cursor_for_location(wm->theme, width, height,
1447 enter->event_x, enter->event_y);
1448
Tiago Vignattic1903232012-07-16 12:15:37 -04001449 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001450}
1451
1452static void
1453weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1454{
1455 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1456 struct weston_wm_window *window;
1457
1458 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001459 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001460 return;
1461
Tiago Vignattic1903232012-07-16 12:15:37 -04001462 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001463}
1464
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001465static int
1466weston_wm_handle_event(int fd, uint32_t mask, void *data)
1467{
1468 struct weston_wm *wm = data;
1469 xcb_generic_event_t *event;
1470 int count = 0;
1471
1472 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1473 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001474 free(event);
1475 count++;
1476 continue;
1477 }
1478
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001479 if (weston_wm_handle_dnd_event(wm, event)) {
1480 free(event);
1481 count++;
1482 continue;
1483 }
1484
MoD31700122013-06-11 19:58:55 -05001485 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001486 case XCB_BUTTON_PRESS:
1487 case XCB_BUTTON_RELEASE:
1488 weston_wm_handle_button(wm, event);
1489 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001490 case XCB_ENTER_NOTIFY:
1491 weston_wm_handle_enter(wm, event);
1492 break;
1493 case XCB_LEAVE_NOTIFY:
1494 weston_wm_handle_leave(wm, event);
1495 break;
1496 case XCB_MOTION_NOTIFY:
1497 weston_wm_handle_motion(wm, event);
1498 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001499 case XCB_CREATE_NOTIFY:
1500 weston_wm_handle_create_notify(wm, event);
1501 break;
1502 case XCB_MAP_REQUEST:
1503 weston_wm_handle_map_request(wm, event);
1504 break;
1505 case XCB_MAP_NOTIFY:
1506 weston_wm_handle_map_notify(wm, event);
1507 break;
1508 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001509 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001510 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001511 case XCB_REPARENT_NOTIFY:
1512 weston_wm_handle_reparent_notify(wm, event);
1513 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001514 case XCB_CONFIGURE_REQUEST:
1515 weston_wm_handle_configure_request(wm, event);
1516 break;
1517 case XCB_CONFIGURE_NOTIFY:
1518 weston_wm_handle_configure_notify(wm, event);
1519 break;
1520 case XCB_DESTROY_NOTIFY:
1521 weston_wm_handle_destroy_notify(wm, event);
1522 break;
1523 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001524 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001525 break;
1526 case XCB_PROPERTY_NOTIFY:
1527 weston_wm_handle_property_notify(wm, event);
1528 break;
1529 case XCB_CLIENT_MESSAGE:
1530 weston_wm_handle_client_message(wm, event);
1531 break;
1532 }
1533
1534 free(event);
1535 count++;
1536 }
1537
1538 xcb_flush(wm->conn);
1539
1540 return count;
1541}
1542
1543static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001544weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1545{
1546 xcb_depth_iterator_t d_iter;
1547 xcb_visualtype_iterator_t vt_iter;
1548 xcb_visualtype_t *visualtype;
1549
1550 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1551 visualtype = NULL;
1552 while (d_iter.rem > 0) {
1553 if (d_iter.data->depth == 32) {
1554 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1555 visualtype = vt_iter.data;
1556 break;
1557 }
1558
1559 xcb_depth_next(&d_iter);
1560 }
1561
1562 if (visualtype == NULL) {
1563 weston_log("no 32 bit visualtype\n");
1564 return;
1565 }
1566
1567 wm->visual_id = visualtype->visual_id;
1568 wm->colormap = xcb_generate_id(wm->conn);
1569 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1570 wm->colormap, wm->screen->root, wm->visual_id);
1571}
1572
1573static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001574weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001575{
1576
1577#define F(field) offsetof(struct weston_wm, field)
1578
1579 static const struct { const char *name; int offset; } atoms[] = {
1580 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001581 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001582 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1583 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001584 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001585 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001586 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001587 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001588 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001589 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001590 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1591 { "_NET_WM_STATE", F(atom.net_wm_state) },
1592 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1593 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1594 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1595 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1596
1597 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1598 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1599 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1600 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1601 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1602 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1603 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001604 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1605 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001606 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1607 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1608 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1609 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1610 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1611
1612 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1613 { "_NET_SUPPORTING_WM_CHECK",
1614 F(atom.net_supporting_wm_check) },
1615 { "_NET_SUPPORTED", F(atom.net_supported) },
1616 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1617 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001618 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001619 { "TARGETS", F(atom.targets) },
1620 { "UTF8_STRING", F(atom.utf8_string) },
1621 { "_WL_SELECTION", F(atom.wl_selection) },
1622 { "INCR", F(atom.incr) },
1623 { "TIMESTAMP", F(atom.timestamp) },
1624 { "MULTIPLE", F(atom.multiple) },
1625 { "UTF8_STRING" , F(atom.utf8_string) },
1626 { "COMPOUND_TEXT", F(atom.compound_text) },
1627 { "TEXT", F(atom.text) },
1628 { "STRING", F(atom.string) },
1629 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1630 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001631 { "XdndSelection", F(atom.xdnd_selection) },
1632 { "XdndAware", F(atom.xdnd_aware) },
1633 { "XdndEnter", F(atom.xdnd_enter) },
1634 { "XdndLeave", F(atom.xdnd_leave) },
1635 { "XdndDrop", F(atom.xdnd_drop) },
1636 { "XdndStatus", F(atom.xdnd_status) },
1637 { "XdndFinished", F(atom.xdnd_finished) },
1638 { "XdndTypeList", F(atom.xdnd_type_list) },
1639 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001640 };
1641#undef F
1642
1643 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1644 xcb_xfixes_query_version_reply_t *xfixes_reply;
1645 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1646 xcb_intern_atom_reply_t *reply;
1647 xcb_render_query_pict_formats_reply_t *formats_reply;
1648 xcb_render_query_pict_formats_cookie_t formats_cookie;
1649 xcb_render_pictforminfo_t *formats;
1650 uint32_t i;
1651
1652 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1653
1654 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1655
1656 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1657 cookies[i] = xcb_intern_atom (wm->conn, 0,
1658 strlen(atoms[i].name),
1659 atoms[i].name);
1660
1661 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1662 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1663 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1664 free(reply);
1665 }
1666
1667 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1668 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001669 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001670
1671 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1672 XCB_XFIXES_MAJOR_VERSION,
1673 XCB_XFIXES_MINOR_VERSION);
1674 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1675 xfixes_cookie, NULL);
1676
Martin Minarik6d118362012-06-07 18:01:59 +02001677 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001678 xfixes_reply->major_version, xfixes_reply->minor_version);
1679
1680 free(xfixes_reply);
1681
1682 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1683 formats_cookie, 0);
1684 if (formats_reply == NULL)
1685 return;
1686
1687 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001688 for (i = 0; i < formats_reply->num_formats; i++) {
1689 if (formats[i].direct.red_mask != 0xff &&
1690 formats[i].direct.red_shift != 16)
1691 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001692 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1693 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001694 wm->format_rgb = formats[i];
1695 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1696 formats[i].depth == 32 &&
1697 formats[i].direct.alpha_mask == 0xff &&
1698 formats[i].direct.alpha_shift == 24)
1699 wm->format_rgba = formats[i];
1700 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001701
1702 free(formats_reply);
1703}
1704
1705static void
1706weston_wm_create_wm_window(struct weston_wm *wm)
1707{
1708 static const char name[] = "Weston WM";
1709
1710 wm->wm_window = xcb_generate_id(wm->conn);
1711 xcb_create_window(wm->conn,
1712 XCB_COPY_FROM_PARENT,
1713 wm->wm_window,
1714 wm->screen->root,
1715 0, 0,
1716 10, 10,
1717 0,
1718 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1719 wm->screen->root_visual,
1720 0, NULL);
1721
1722 xcb_change_property(wm->conn,
1723 XCB_PROP_MODE_REPLACE,
1724 wm->wm_window,
1725 wm->atom.net_supporting_wm_check,
1726 XCB_ATOM_WINDOW,
1727 32, /* format */
1728 1, &wm->wm_window);
1729
1730 xcb_change_property(wm->conn,
1731 XCB_PROP_MODE_REPLACE,
1732 wm->wm_window,
1733 wm->atom.net_wm_name,
1734 wm->atom.utf8_string,
1735 8, /* format */
1736 strlen(name), name);
1737
1738 xcb_change_property(wm->conn,
1739 XCB_PROP_MODE_REPLACE,
1740 wm->screen->root,
1741 wm->atom.net_supporting_wm_check,
1742 XCB_ATOM_WINDOW,
1743 32, /* format */
1744 1, &wm->wm_window);
1745
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001746 /* Claim the WM_S0 selection even though we don't suport
1747 * the --replace functionality. */
1748 xcb_set_selection_owner(wm->conn,
1749 wm->wm_window,
1750 wm->atom.wm_s0,
1751 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001752
1753 xcb_set_selection_owner(wm->conn,
1754 wm->wm_window,
1755 wm->atom.net_wm_cm_s0,
1756 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001757}
1758
1759struct weston_wm *
1760weston_wm_create(struct weston_xserver *wxs)
1761{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001762 struct weston_wm *wm;
1763 struct wl_event_loop *loop;
1764 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001765 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001766 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001767 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001768
Peter Huttererf3d62272013-08-08 11:57:05 +10001769 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001770 if (wm == NULL)
1771 return NULL;
1772
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001773 wm->server = wxs;
1774 wm->window_hash = hash_table_create();
1775 if (wm->window_hash == NULL) {
1776 free(wm);
1777 return NULL;
1778 }
1779
1780 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001781 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001782 hash_table_destroy(wm->window_hash);
1783 free(wm);
1784 return NULL;
1785 }
1786
1787 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001788 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001789 close(sv[1]);
1790
1791 /* xcb_connect_to_fd takes ownership of the fd. */
1792 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1793 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001794 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001795 close(sv[0]);
1796 hash_table_destroy(wm->window_hash);
1797 free(wm);
1798 return NULL;
1799 }
1800
1801 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1802 wm->screen = s.data;
1803
1804 loop = wl_display_get_event_loop(wxs->wl_display);
1805 wm->source =
1806 wl_event_loop_add_fd(loop, sv[0],
1807 WL_EVENT_READABLE,
1808 weston_wm_handle_event, wm);
1809 wl_event_source_check(wm->source);
1810
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001811 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001812 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001813
1814 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001815 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1816 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1817 XCB_EVENT_MASK_PROPERTY_CHANGE;
1818 xcb_change_window_attributes(wm->conn, wm->screen->root,
1819 XCB_CW_EVENT_MASK, values);
1820 wm->theme = theme_create();
1821
1822 weston_wm_create_wm_window(wm);
1823
1824 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001825 supported[1] = wm->atom.net_wm_state;
1826 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001827 xcb_change_property(wm->conn,
1828 XCB_PROP_MODE_REPLACE,
1829 wm->screen->root,
1830 wm->atom.net_supported,
1831 XCB_ATOM_ATOM,
1832 32, /* format */
1833 ARRAY_LENGTH(supported), supported);
1834
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001835 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001836
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001837 weston_wm_dnd_init(wm);
1838
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001839 xcb_flush(wm->conn);
1840
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001841 wm->activate_listener.notify = weston_wm_window_activate;
1842 wl_signal_add(&wxs->compositor->activate_signal,
1843 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001844 wm->transform_listener.notify = weston_wm_window_transform;
1845 wl_signal_add(&wxs->compositor->transform_signal,
1846 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001847 wm->kill_listener.notify = weston_wm_kill_client;
1848 wl_signal_add(&wxs->compositor->kill_signal,
1849 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001850
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001851 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001852 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001853
Martin Minarik6d118362012-06-07 18:01:59 +02001854 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001855
1856 return wm;
1857}
1858
1859void
1860weston_wm_destroy(struct weston_wm *wm)
1861{
1862 /* FIXME: Free windows in hash. */
1863 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001864 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001865 xcb_disconnect(wm->conn);
1866 wl_event_source_remove(wm->source);
1867 wl_list_remove(&wm->selection_listener.link);
1868 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001869 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001870 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001871
1872 free(wm);
1873}
1874
1875static void
1876surface_destroy(struct wl_listener *listener, void *data)
1877{
1878 struct weston_wm_window *window =
1879 container_of(listener,
1880 struct weston_wm_window, surface_destroy_listener);
1881
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001882 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001883
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001884 /* This should have been freed by the shell.
1885 Don't try to use it later. */
1886 window->shsurf = NULL;
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001887 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001888}
1889
1890static struct weston_wm_window *
1891get_wm_window(struct weston_surface *surface)
1892{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001893 struct wl_listener *listener;
1894
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001895 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001896 if (listener)
1897 return container_of(listener, struct weston_wm_window,
1898 surface_destroy_listener);
1899
1900 return NULL;
1901}
1902
1903static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001904weston_wm_window_configure(void *data)
1905{
1906 struct weston_wm_window *window = data;
1907 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001908 uint32_t values[4];
1909 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001910
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001911 weston_wm_window_get_child_position(window, &x, &y);
1912 values[0] = x;
1913 values[1] = y;
1914 values[2] = window->width;
1915 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001916 xcb_configure_window(wm->conn,
1917 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001918 XCB_CONFIG_WINDOW_X |
1919 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001920 XCB_CONFIG_WINDOW_WIDTH |
1921 XCB_CONFIG_WINDOW_HEIGHT,
1922 values);
1923
1924 weston_wm_window_get_frame_size(window, &width, &height);
1925 values[0] = width;
1926 values[1] = height;
1927 xcb_configure_window(wm->conn,
1928 window->frame_id,
1929 XCB_CONFIG_WINDOW_WIDTH |
1930 XCB_CONFIG_WINDOW_HEIGHT,
1931 values);
1932
1933 window->configure_source = NULL;
1934
1935 weston_wm_window_schedule_repaint(window);
1936}
1937
1938static void
1939send_configure(struct weston_surface *surface,
1940 uint32_t edges, int32_t width, int32_t height)
1941{
1942 struct weston_wm_window *window = get_wm_window(surface);
1943 struct weston_wm *wm = window->wm;
1944 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001945 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001946
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001947 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001948 hborder = 0;
1949 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001950 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001951 hborder = 2 * (t->margin + t->width);
1952 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001953 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001954 hborder = 2 * t->margin;
1955 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001956 }
1957
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001958 if (width > hborder)
1959 window->width = width - hborder;
1960 else
1961 window->width = 1;
1962
1963 if (height > vborder)
1964 window->height = height - vborder;
1965 else
1966 window->height = 1;
1967
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001968 if (window->configure_source)
1969 return;
1970
1971 window->configure_source =
1972 wl_event_loop_add_idle(wm->server->loop,
1973 weston_wm_window_configure, window);
1974}
1975
1976static const struct weston_shell_client shell_client = {
1977 send_configure
1978};
1979
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001980static int
1981legacy_fullscreen(struct weston_wm *wm,
1982 struct weston_wm_window *window,
1983 struct weston_output **output_ret)
1984{
1985 struct weston_compositor *compositor = wm->server->compositor;
1986 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001987 uint32_t minmax = PMinSize | PMaxSize;
1988 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001989
1990 /* Heuristics for detecting legacy fullscreen windows... */
1991
1992 wl_list_for_each(output, &compositor->output_list, link) {
1993 if (output->x == window->x &&
1994 output->y == window->y &&
1995 output->width == window->width &&
1996 output->height == window->height &&
1997 window->override_redirect) {
1998 *output_ret = output;
1999 return 1;
2000 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002001
2002 matching_size = 0;
2003 if ((window->size_hints.flags & (USSize |PSize)) &&
2004 window->size_hints.width == output->width &&
2005 window->size_hints.height == output->height)
2006 matching_size = 1;
2007 if ((window->size_hints.flags & minmax) == minmax &&
2008 window->size_hints.min_width == output->width &&
2009 window->size_hints.min_height == output->height &&
2010 window->size_hints.max_width == output->width &&
2011 window->size_hints.max_height == output->height)
2012 matching_size = 1;
2013
2014 if (matching_size && !window->decorate &&
2015 (window->size_hints.flags & (USPosition | PPosition)) &&
2016 window->size_hints.x == output->x &&
2017 window->size_hints.y == output->y) {
2018 *output_ret = output;
2019 return 1;
2020 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002021 }
2022
2023 return 0;
2024}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002025static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002026xserver_map_shell_surface(struct weston_wm *wm,
2027 struct weston_wm_window *window)
2028{
2029 struct weston_shell_interface *shell_interface =
2030 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002031 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002032
2033 if (!shell_interface->create_shell_surface)
2034 return;
2035
2036 window->shsurf =
2037 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002038 window->surface,
2039 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002040
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002041 if (window->name)
2042 shell_interface->set_title(window->shsurf, window->name);
2043
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002044 if (window->fullscreen) {
2045 window->saved_width = window->width;
2046 window->saved_height = window->height;
2047 shell_interface->set_fullscreen(window->shsurf,
2048 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2049 0, NULL);
2050 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002051 } else if (legacy_fullscreen(wm, window, &output)) {
2052 window->fullscreen = 1;
2053 shell_interface->set_fullscreen(window->shsurf,
2054 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2055 0, output);
Giulio Camuffoca43f092013-09-11 17:49:13 +02002056 } else if (!window->override_redirect && !window->transient_for) {
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002057 shell_interface->set_toplevel(window->shsurf);
2058 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002059 } else {
2060 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002061 window->x,
2062 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002063 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002064 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002065}
2066
2067static void
2068xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2069 struct wl_resource *surface_resource, uint32_t id)
2070{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002071 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002072 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002073 struct weston_surface *surface =
2074 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002075 struct weston_wm_window *window;
2076
2077 if (client != wxs->client)
2078 return;
2079
2080 window = hash_table_lookup(wm->window_hash, id);
2081 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002082 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002083 return;
2084 }
2085
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002086 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002087
2088 weston_wm_window_read_properties(window);
2089
2090 window->surface = (struct weston_surface *) surface;
2091 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002092 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002093 &window->surface_destroy_listener);
2094
2095 weston_wm_window_schedule_repaint(window);
2096 xserver_map_shell_surface(wm, window);
2097}
2098
2099const struct xserver_interface xserver_implementation = {
2100 xserver_set_window_id
2101};