blob: ef59e6d226dc3fb3fab5a0f41d6a190cac0ff3dd [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;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400612
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400613 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400614 configure_notify->window,
615 configure_notify->x, configure_notify->y,
616 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300617
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400618 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700619 window->x = configure_notify->x;
620 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700621 if (window->override_redirect) {
622 window->width = configure_notify->width;
623 window->height = configure_notify->height;
624 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400625}
626
627static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300628weston_wm_kill_client(struct wl_listener *listener, void *data)
629{
630 struct weston_surface *surface = data;
631 struct weston_wm_window *window = get_wm_window(surface);
632 char name[1024];
633
634 if (!window)
635 return;
636
637 gethostname(name, 1024);
638
639 /* this is only one heuristic to guess the PID of a client is valid,
640 * assuming it's compliant with icccm and ewmh. Non-compliants and
641 * remote applications of course fail. */
642 if (!strcmp(window->machine, name) && window->pid != 0)
643 kill(window->pid, SIGKILL);
644}
645
646static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400647weston_wm_window_activate(struct wl_listener *listener, void *data)
648{
649 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200650 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600651 struct weston_wm *wm =
652 container_of(listener, struct weston_wm, activate_listener);
653 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400654
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200655 if (surface) {
656 window = get_wm_window(surface);
657 }
658
Scott Moreau85ecac02012-05-21 15:49:14 -0600659 if (window) {
660 client_message.response_type = XCB_CLIENT_MESSAGE;
661 client_message.format = 32;
662 client_message.window = window->id;
663 client_message.type = wm->atom.wm_protocols;
664 client_message.data.data32[0] = wm->atom.wm_take_focus;
665 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
666
667 xcb_send_event(wm->conn, 0, window->id,
668 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
669 (char *) &client_message);
670
671 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
672 window->id, XCB_TIME_CURRENT_TIME);
673 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400674 xcb_set_input_focus (wm->conn,
675 XCB_INPUT_FOCUS_POINTER_ROOT,
676 XCB_NONE,
677 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600678 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400679
680 if (wm->focus_window)
681 weston_wm_window_schedule_repaint(wm->focus_window);
682 wm->focus_window = window;
683 if (wm->focus_window)
684 weston_wm_window_schedule_repaint(wm->focus_window);
685}
686
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300687static void
688weston_wm_window_transform(struct wl_listener *listener, void *data)
689{
690 struct weston_surface *surface = data;
691 struct weston_wm_window *window = get_wm_window(surface);
692 struct weston_wm *wm =
693 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300694 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300695
696 if (!window || !wm)
697 return;
698
699 if (!weston_surface_is_mapped(surface))
700 return;
701
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700702 if (window->x != surface->geometry.x ||
703 window->y != surface->geometry.y) {
704 values[0] = surface->geometry.x;
705 values[1] = surface->geometry.y;
706 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300707
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700708 xcb_configure_window(wm->conn, window->frame_id, mask, values);
709 xcb_flush(wm->conn);
710 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300711}
712
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400713#define ICCCM_WITHDRAWN_STATE 0
714#define ICCCM_NORMAL_STATE 1
715#define ICCCM_ICONIC_STATE 3
716
717static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500718weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400719{
720 struct weston_wm *wm = window->wm;
721 uint32_t property[2];
722
723 property[0] = state;
724 property[1] = XCB_WINDOW_NONE;
725
726 xcb_change_property(wm->conn,
727 XCB_PROP_MODE_REPLACE,
728 window->id,
729 wm->atom.wm_state,
730 wm->atom.wm_state,
731 32, /* format */
732 2, property);
733}
734
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400735static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500736weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
737{
738 struct weston_wm *wm = window->wm;
739 uint32_t property[1];
740 int i;
741
742 i = 0;
743 if (window->fullscreen)
744 property[i++] = wm->atom.net_wm_state_fullscreen;
745
746 xcb_change_property(wm->conn,
747 XCB_PROP_MODE_REPLACE,
748 window->id,
749 wm->atom.net_wm_state,
750 XCB_ATOM_ATOM,
751 32, /* format */
752 i, property);
753}
754
755static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700756weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400757{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700758 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400759 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400760 int x, y, width, height;
761
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400762 weston_wm_window_get_frame_size(window, &width, &height);
763 weston_wm_window_get_child_position(window, &x, &y);
764
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400765 values[0] = wm->screen->black_pixel;
766 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400767 XCB_EVENT_MASK_KEY_PRESS |
768 XCB_EVENT_MASK_KEY_RELEASE |
769 XCB_EVENT_MASK_BUTTON_PRESS |
770 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400771 XCB_EVENT_MASK_POINTER_MOTION |
772 XCB_EVENT_MASK_ENTER_WINDOW |
773 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400774 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400775 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400776 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400777
778 window->frame_id = xcb_generate_id(wm->conn);
779 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400780 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400781 window->frame_id,
782 wm->screen->root,
783 0, 0,
784 width, height,
785 0,
786 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400787 wm->visual_id,
788 XCB_CW_BORDER_PIXEL |
789 XCB_CW_EVENT_MASK |
790 XCB_CW_COLORMAP, values);
791
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400792 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
793
794 values[0] = 0;
795 xcb_configure_window(wm->conn, window->id,
796 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
797
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400798 window->cairo_surface =
799 cairo_xcb_surface_create_with_xrender_format(wm->conn,
800 wm->screen,
801 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400802 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400803 width, height);
804
805 hash_table_insert(wm->window_hash, window->frame_id, window);
806}
807
808static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700809weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
810{
811 xcb_map_request_event_t *map_request =
812 (xcb_map_request_event_t *) event;
813 struct weston_wm_window *window;
814
815 if (our_resource(wm, map_request->window)) {
816 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
817 map_request->window);
818 return;
819 }
820
821 window = hash_table_lookup(wm->window_hash, map_request->window);
822
823 weston_wm_window_read_properties(window);
824
825 if (window->frame_id == XCB_WINDOW_NONE)
826 weston_wm_window_create_frame(window);
827
828 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
829 window->id, window, window->frame_id);
830
831 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
832 weston_wm_window_set_net_wm_state(window);
833
834 xcb_map_window(wm->conn, map_request->window);
835 xcb_map_window(wm->conn, window->frame_id);
836}
837
838static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
840{
841 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
842
843 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400844 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
845 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400846 return;
847 }
848
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400849 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400850}
851
852static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400853weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
854{
855 xcb_unmap_notify_event_t *unmap_notify =
856 (xcb_unmap_notify_event_t *) event;
857 struct weston_wm_window *window;
858
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400859 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
860 unmap_notify->window,
861 unmap_notify->event,
862 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400863
864 if (our_resource(wm, unmap_notify->window))
865 return;
866
MoD31700122013-06-11 19:58:55 -0500867 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400868 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
869 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400870 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400871
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400872 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400873 if (wm->focus_window == window)
874 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400875 if (window->surface)
876 wl_list_remove(&window->surface_destroy_listener.link);
877 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200878 window->shsurf = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700879 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400880}
881
882static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400883weston_wm_window_draw_decoration(void *data)
884{
885 struct weston_wm_window *window = data;
886 struct weston_wm *wm = window->wm;
887 struct theme *t = wm->theme;
888 cairo_t *cr;
889 int x, y, width, height;
890 const char *title;
891 uint32_t flags = 0;
892
893 weston_wm_window_read_properties(window);
894
895 window->repaint_source = NULL;
896
897 weston_wm_window_get_frame_size(window, &width, &height);
898 weston_wm_window_get_child_position(window, &x, &y);
899
900 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
901 cr = cairo_create(window->cairo_surface);
902
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500903 if (window->fullscreen) {
904 /* nothing */
905 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400906 if (wm->focus_window == window)
907 flags |= THEME_FRAME_ACTIVE;
908
909 if (window->name)
910 title = window->name;
911 else
912 title = "untitled";
913
914 theme_render_frame(t, cr, width, height, title, flags);
915 } else {
916 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
917 cairo_set_source_rgba(cr, 0, 0, 0, 0);
918 cairo_paint(cr);
919
920 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
921 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
922 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
923 }
924
925 cairo_destroy(cr);
926
927 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700928 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500929 if(window->has_alpha) {
930 pixman_region32_init(&window->surface->pending.opaque);
931 } else {
932 /* We leave an extra pixel around the X window area to
933 * make sure we don't sample from the undefined alpha
934 * channel when filtering. */
935 pixman_region32_init_rect(&window->surface->pending.opaque,
936 x - 1, y - 1,
937 window->width + 2,
938 window->height + 2);
939 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200940 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500941 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400942
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500943 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500944 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500945 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400946 t->margin, t->margin,
947 width - 2 * t->margin,
948 height - 2 * t->margin);
949 }
950}
951
952static void
953weston_wm_window_schedule_repaint(struct weston_wm_window *window)
954{
955 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300956 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400957
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400958 if (window->frame_id == XCB_WINDOW_NONE) {
959 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300960 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700961 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500962 if(window->has_alpha) {
963 pixman_region32_init(&window->surface->pending.opaque);
964 } else {
965 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
966 width, height);
967 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200968 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400969 }
970 return;
971 }
972
973 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400974 return;
975
976 window->repaint_source =
977 wl_event_loop_add_idle(wm->server->loop,
978 weston_wm_window_draw_decoration,
979 window);
980}
981
982static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400983weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
984{
985 xcb_property_notify_event_t *property_notify =
986 (xcb_property_notify_event_t *) event;
987 struct weston_wm_window *window;
988
989 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000990 if (!window)
991 return;
992
993 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400994
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400995 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400996 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400997 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400998 else
999 read_and_dump_property(wm, property_notify->window,
1000 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001001
1002 if (property_notify->atom == wm->atom.net_wm_name ||
1003 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001004 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001005}
1006
1007static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001008weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001009 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001010{
1011 struct weston_wm_window *window;
1012 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001013 xcb_get_geometry_cookie_t geometry_cookie;
1014 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001015
Peter Huttererf3d62272013-08-08 11:57:05 +10001016 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001017 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001018 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001019 return;
1020 }
1021
MoD384a11a2013-06-22 11:04:21 -05001022 geometry_cookie = xcb_get_geometry(wm->conn, id);
1023
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001024 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1025 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1026
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001027 window->wm = wm;
1028 window->id = id;
1029 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001030 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001031 window->width = width;
1032 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001033 window->x = x;
1034 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001035
MoD384a11a2013-06-22 11:04:21 -05001036 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1037 /* technically we should use XRender and check the visual format's
1038 alpha_mask, but checking depth is simpler and works in all known cases */
1039 if(geometry_reply != NULL)
1040 window->has_alpha = geometry_reply->depth == 32;
1041 free(geometry_reply);
1042
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001043 hash_table_insert(wm->window_hash, id, window);
1044}
1045
1046static void
1047weston_wm_window_destroy(struct weston_wm_window *window)
1048{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001049 struct weston_wm *wm = window->wm;
1050
1051 if (window->repaint_source)
1052 wl_event_source_remove(window->repaint_source);
1053 if (window->cairo_surface)
1054 cairo_surface_destroy(window->cairo_surface);
1055
1056 if (window->frame_id) {
1057 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1058 xcb_destroy_window(wm->conn, window->frame_id);
1059 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1060 hash_table_remove(wm->window_hash, window->frame_id);
1061 window->frame_id = XCB_WINDOW_NONE;
1062 }
1063
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001064 hash_table_remove(window->wm->window_hash, window->id);
1065 free(window);
1066}
1067
1068static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001069weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1070{
1071 xcb_create_notify_event_t *create_notify =
1072 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001073
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001074 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1075 create_notify->window,
1076 create_notify->width, create_notify->height,
1077 create_notify->override_redirect ? ", override" : "",
1078 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001079
1080 if (our_resource(wm, create_notify->window))
1081 return;
1082
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001083 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001084 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001085 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001086 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001087}
1088
1089static void
1090weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1091{
1092 xcb_destroy_notify_event_t *destroy_notify =
1093 (xcb_destroy_notify_event_t *) event;
1094 struct weston_wm_window *window;
1095
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001096 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1097 destroy_notify->window,
1098 destroy_notify->event,
1099 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001100
1101 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001102 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001103
1104 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001105 weston_wm_window_destroy(window);
1106}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001107
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001108static void
1109weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1110{
1111 xcb_reparent_notify_event_t *reparent_notify =
1112 (xcb_reparent_notify_event_t *) event;
1113 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001114
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001115 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1116 reparent_notify->window,
1117 reparent_notify->parent,
1118 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001119
1120 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001121 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001122 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001123 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001124 } else if (!our_resource(wm, reparent_notify->parent)) {
1125 window = hash_table_lookup(wm->window_hash,
1126 reparent_notify->window);
1127 weston_wm_window_destroy(window);
1128 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001129}
1130
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001131struct weston_seat *
1132weston_wm_pick_seat(struct weston_wm *wm)
1133{
1134 return container_of(wm->server->compositor->seat_list.next,
1135 struct weston_seat, link);
1136}
1137
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001138static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001139weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1140 xcb_client_message_event_t *client_message)
1141{
1142 static const int map[] = {
1143 THEME_LOCATION_RESIZING_TOP_LEFT,
1144 THEME_LOCATION_RESIZING_TOP,
1145 THEME_LOCATION_RESIZING_TOP_RIGHT,
1146 THEME_LOCATION_RESIZING_RIGHT,
1147 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1148 THEME_LOCATION_RESIZING_BOTTOM,
1149 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1150 THEME_LOCATION_RESIZING_LEFT
1151 };
1152
1153 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001154 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001155 int detail;
1156 struct weston_shell_interface *shell_interface =
1157 &wm->server->compositor->shell_interface;
1158
Kristian Høgsberge3148752013-05-06 23:19:49 -04001159 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001160 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001161 return;
1162
1163 detail = client_message->data.data32[2];
1164 switch (detail) {
1165 case _NET_WM_MOVERESIZE_MOVE:
1166 shell_interface->move(window->shsurf, seat);
1167 break;
1168 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1169 case _NET_WM_MOVERESIZE_SIZE_TOP:
1170 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1171 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1172 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1173 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1174 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1175 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1176 shell_interface->resize(window->shsurf, seat, map[detail]);
1177 break;
1178 case _NET_WM_MOVERESIZE_CANCEL:
1179 break;
1180 }
1181}
1182
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001183#define _NET_WM_STATE_REMOVE 0
1184#define _NET_WM_STATE_ADD 1
1185#define _NET_WM_STATE_TOGGLE 2
1186
1187static int
1188update_state(int action, int *state)
1189{
1190 int new_state, changed;
1191
1192 switch (action) {
1193 case _NET_WM_STATE_REMOVE:
1194 new_state = 0;
1195 break;
1196 case _NET_WM_STATE_ADD:
1197 new_state = 1;
1198 break;
1199 case _NET_WM_STATE_TOGGLE:
1200 new_state = !*state;
1201 break;
1202 default:
1203 return 0;
1204 }
1205
1206 changed = (*state != new_state);
1207 *state = new_state;
1208
1209 return changed;
1210}
1211
1212static void
1213weston_wm_window_configure(void *data);
1214
1215static void
1216weston_wm_window_handle_state(struct weston_wm_window *window,
1217 xcb_client_message_event_t *client_message)
1218{
1219 struct weston_wm *wm = window->wm;
1220 struct weston_shell_interface *shell_interface =
1221 &wm->server->compositor->shell_interface;
1222 uint32_t action, property;
1223
1224 action = client_message->data.data32[0];
1225 property = client_message->data.data32[1];
1226
1227 if (property == wm->atom.net_wm_state_fullscreen &&
1228 update_state(action, &window->fullscreen)) {
1229 weston_wm_window_set_net_wm_state(window);
1230 if (window->fullscreen) {
1231 window->saved_width = window->width;
1232 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001233
1234 if (window->shsurf)
1235 shell_interface->set_fullscreen(window->shsurf,
1236 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1237 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001238 } else {
1239 shell_interface->set_toplevel(window->shsurf);
1240 window->width = window->saved_width;
1241 window->height = window->saved_height;
1242 weston_wm_window_configure(window);
1243 }
1244 }
1245}
1246
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001247static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001248weston_wm_handle_client_message(struct weston_wm *wm,
1249 xcb_generic_event_t *event)
1250{
1251 xcb_client_message_event_t *client_message =
1252 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001253 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001254
1255 window = hash_table_lookup(wm->window_hash, client_message->window);
1256
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001257 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1258 get_atom_name(wm->conn, client_message->type),
1259 client_message->data.data32[0],
1260 client_message->data.data32[1],
1261 client_message->data.data32[2],
1262 client_message->data.data32[3],
1263 client_message->data.data32[4],
1264 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001265
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001266 if (client_message->type == wm->atom.net_wm_moveresize)
1267 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001268 else if (client_message->type == wm->atom.net_wm_state)
1269 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001270}
1271
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001272enum cursor_type {
1273 XWM_CURSOR_TOP,
1274 XWM_CURSOR_BOTTOM,
1275 XWM_CURSOR_LEFT,
1276 XWM_CURSOR_RIGHT,
1277 XWM_CURSOR_TOP_LEFT,
1278 XWM_CURSOR_TOP_RIGHT,
1279 XWM_CURSOR_BOTTOM_LEFT,
1280 XWM_CURSOR_BOTTOM_RIGHT,
1281 XWM_CURSOR_LEFT_PTR,
1282};
1283
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001284/*
1285 * The following correspondences between file names and cursors was copied
1286 * from: https://bugs.kde.org/attachment.cgi?id=67313
1287 */
1288
1289static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001290 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001291 "sw-resize",
1292 "size_bdiag"
1293};
1294
1295static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001296 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001297 "se-resize",
1298 "size_fdiag"
1299};
1300
1301static const char *bottom_sides[] = {
1302 "bottom_side",
1303 "s-resize",
1304 "size_ver"
1305};
1306
1307static const char *left_ptrs[] = {
1308 "left_ptr",
1309 "default",
1310 "top_left_arrow",
1311 "left-arrow"
1312};
1313
1314static const char *left_sides[] = {
1315 "left_side",
1316 "w-resize",
1317 "size_hor"
1318};
1319
1320static const char *right_sides[] = {
1321 "right_side",
1322 "e-resize",
1323 "size_hor"
1324};
1325
1326static const char *top_left_corners[] = {
1327 "top_left_corner",
1328 "nw-resize",
1329 "size_fdiag"
1330};
1331
1332static const char *top_right_corners[] = {
1333 "top_right_corner",
1334 "ne-resize",
1335 "size_bdiag"
1336};
1337
1338static const char *top_sides[] = {
1339 "top_side",
1340 "n-resize",
1341 "size_ver"
1342};
1343
1344struct cursor_alternatives {
1345 const char **names;
1346 size_t count;
1347};
1348
1349static const struct cursor_alternatives cursors[] = {
1350 {top_sides, ARRAY_LENGTH(top_sides)},
1351 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1352 {left_sides, ARRAY_LENGTH(left_sides)},
1353 {right_sides, ARRAY_LENGTH(right_sides)},
1354 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1355 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1356 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1357 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1358 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001359};
1360
1361static void
1362weston_wm_create_cursors(struct weston_wm *wm)
1363{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001364 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001365 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001366 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001367
1368 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1369 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001370 for (j = 0; j < cursors[i].count; j++) {
1371 name = cursors[i].names[j];
1372 wm->cursors[i] =
1373 xcb_cursor_library_load_cursor(wm, name);
1374 if (wm->cursors[i] != (xcb_cursor_t)-1)
1375 break;
1376 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001377 }
1378
1379 wm->last_cursor = -1;
1380}
1381
1382static void
1383weston_wm_destroy_cursors(struct weston_wm *wm)
1384{
1385 uint8_t i;
1386
1387 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1388 xcb_free_cursor(wm->conn, wm->cursors[i]);
1389
1390 free(wm->cursors);
1391}
1392
1393static int
1394get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1395{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001396 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001397
1398 switch (location) {
1399 case THEME_LOCATION_RESIZING_TOP:
1400 return XWM_CURSOR_TOP;
1401 case THEME_LOCATION_RESIZING_BOTTOM:
1402 return XWM_CURSOR_BOTTOM;
1403 case THEME_LOCATION_RESIZING_LEFT:
1404 return XWM_CURSOR_LEFT;
1405 case THEME_LOCATION_RESIZING_RIGHT:
1406 return XWM_CURSOR_RIGHT;
1407 case THEME_LOCATION_RESIZING_TOP_LEFT:
1408 return XWM_CURSOR_TOP_LEFT;
1409 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1410 return XWM_CURSOR_TOP_RIGHT;
1411 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1412 return XWM_CURSOR_BOTTOM_LEFT;
1413 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1414 return XWM_CURSOR_BOTTOM_RIGHT;
1415 case THEME_LOCATION_EXTERIOR:
1416 case THEME_LOCATION_TITLEBAR:
1417 default:
1418 return XWM_CURSOR_LEFT_PTR;
1419 }
1420}
1421
1422static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001423weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1424 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001425{
1426 uint32_t cursor_value_list;
1427
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001428 if (wm->last_cursor == cursor)
1429 return;
1430
1431 wm->last_cursor = cursor;
1432
1433 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001434 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001435 XCB_CW_CURSOR, &cursor_value_list);
1436 xcb_flush(wm->conn);
1437}
1438
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001439static void
1440weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1441{
1442 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1443 struct weston_shell_interface *shell_interface =
1444 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001445 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001446 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001447 enum theme_location location;
1448 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001449 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001450
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001451 wm_log("XCB_BUTTON_%s (detail %d)\n",
1452 button->response_type == XCB_BUTTON_PRESS ?
1453 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001454
1455 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001456 weston_wm_window_get_frame_size(window, &width, &height);
1457
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001458 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001459 button->detail == 1) {
1460 location = theme_get_location(t,
1461 button->event_x,
1462 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001463 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001464
1465 switch (location) {
1466 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001467 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001468 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001469 case THEME_LOCATION_RESIZING_TOP:
1470 case THEME_LOCATION_RESIZING_BOTTOM:
1471 case THEME_LOCATION_RESIZING_LEFT:
1472 case THEME_LOCATION_RESIZING_RIGHT:
1473 case THEME_LOCATION_RESIZING_TOP_LEFT:
1474 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1475 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1476 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1477 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001478 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001479 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001480 default:
1481 break;
1482 }
1483 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001484}
1485
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001486static void
1487weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1488{
1489 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1490 struct weston_wm_window *window;
1491 int cursor, width, height;
1492
1493 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001494 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001495 return;
1496
1497 weston_wm_window_get_frame_size(window, &width, &height);
1498 cursor = get_cursor_for_location(wm->theme, width, height,
1499 motion->event_x, motion->event_y);
1500
Tiago Vignattic1903232012-07-16 12:15:37 -04001501 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001502}
1503
1504static void
1505weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1506{
1507 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1508 struct weston_wm_window *window;
1509 int cursor, width, height;
1510
1511 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001512 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001513 return;
1514
1515 weston_wm_window_get_frame_size(window, &width, &height);
1516 cursor = get_cursor_for_location(wm->theme, width, height,
1517 enter->event_x, enter->event_y);
1518
Tiago Vignattic1903232012-07-16 12:15:37 -04001519 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001520}
1521
1522static void
1523weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1524{
1525 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1526 struct weston_wm_window *window;
1527
1528 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001529 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001530 return;
1531
Tiago Vignattic1903232012-07-16 12:15:37 -04001532 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001533}
1534
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001535static int
1536weston_wm_handle_event(int fd, uint32_t mask, void *data)
1537{
1538 struct weston_wm *wm = data;
1539 xcb_generic_event_t *event;
1540 int count = 0;
1541
1542 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1543 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001544 free(event);
1545 count++;
1546 continue;
1547 }
1548
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001549 if (weston_wm_handle_dnd_event(wm, event)) {
1550 free(event);
1551 count++;
1552 continue;
1553 }
1554
MoD31700122013-06-11 19:58:55 -05001555 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001556 case XCB_BUTTON_PRESS:
1557 case XCB_BUTTON_RELEASE:
1558 weston_wm_handle_button(wm, event);
1559 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001560 case XCB_ENTER_NOTIFY:
1561 weston_wm_handle_enter(wm, event);
1562 break;
1563 case XCB_LEAVE_NOTIFY:
1564 weston_wm_handle_leave(wm, event);
1565 break;
1566 case XCB_MOTION_NOTIFY:
1567 weston_wm_handle_motion(wm, event);
1568 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001569 case XCB_CREATE_NOTIFY:
1570 weston_wm_handle_create_notify(wm, event);
1571 break;
1572 case XCB_MAP_REQUEST:
1573 weston_wm_handle_map_request(wm, event);
1574 break;
1575 case XCB_MAP_NOTIFY:
1576 weston_wm_handle_map_notify(wm, event);
1577 break;
1578 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001579 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001580 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001581 case XCB_REPARENT_NOTIFY:
1582 weston_wm_handle_reparent_notify(wm, event);
1583 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001584 case XCB_CONFIGURE_REQUEST:
1585 weston_wm_handle_configure_request(wm, event);
1586 break;
1587 case XCB_CONFIGURE_NOTIFY:
1588 weston_wm_handle_configure_notify(wm, event);
1589 break;
1590 case XCB_DESTROY_NOTIFY:
1591 weston_wm_handle_destroy_notify(wm, event);
1592 break;
1593 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001594 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001595 break;
1596 case XCB_PROPERTY_NOTIFY:
1597 weston_wm_handle_property_notify(wm, event);
1598 break;
1599 case XCB_CLIENT_MESSAGE:
1600 weston_wm_handle_client_message(wm, event);
1601 break;
1602 }
1603
1604 free(event);
1605 count++;
1606 }
1607
1608 xcb_flush(wm->conn);
1609
1610 return count;
1611}
1612
1613static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001614weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1615{
1616 xcb_depth_iterator_t d_iter;
1617 xcb_visualtype_iterator_t vt_iter;
1618 xcb_visualtype_t *visualtype;
1619
1620 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1621 visualtype = NULL;
1622 while (d_iter.rem > 0) {
1623 if (d_iter.data->depth == 32) {
1624 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1625 visualtype = vt_iter.data;
1626 break;
1627 }
1628
1629 xcb_depth_next(&d_iter);
1630 }
1631
1632 if (visualtype == NULL) {
1633 weston_log("no 32 bit visualtype\n");
1634 return;
1635 }
1636
1637 wm->visual_id = visualtype->visual_id;
1638 wm->colormap = xcb_generate_id(wm->conn);
1639 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1640 wm->colormap, wm->screen->root, wm->visual_id);
1641}
1642
1643static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001644weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001645{
1646
1647#define F(field) offsetof(struct weston_wm, field)
1648
1649 static const struct { const char *name; int offset; } atoms[] = {
1650 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001651 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001652 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1653 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001654 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001655 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001656 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001657 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001658 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001659 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001660 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1661 { "_NET_WM_STATE", F(atom.net_wm_state) },
1662 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1663 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1664 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1665 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1666
1667 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1668 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1669 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1670 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1671 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1672 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1673 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001674 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1675 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001676 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1677 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1678 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1679 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1680 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1681
1682 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1683 { "_NET_SUPPORTING_WM_CHECK",
1684 F(atom.net_supporting_wm_check) },
1685 { "_NET_SUPPORTED", F(atom.net_supported) },
1686 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1687 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001688 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001689 { "TARGETS", F(atom.targets) },
1690 { "UTF8_STRING", F(atom.utf8_string) },
1691 { "_WL_SELECTION", F(atom.wl_selection) },
1692 { "INCR", F(atom.incr) },
1693 { "TIMESTAMP", F(atom.timestamp) },
1694 { "MULTIPLE", F(atom.multiple) },
1695 { "UTF8_STRING" , F(atom.utf8_string) },
1696 { "COMPOUND_TEXT", F(atom.compound_text) },
1697 { "TEXT", F(atom.text) },
1698 { "STRING", F(atom.string) },
1699 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1700 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001701 { "XdndSelection", F(atom.xdnd_selection) },
1702 { "XdndAware", F(atom.xdnd_aware) },
1703 { "XdndEnter", F(atom.xdnd_enter) },
1704 { "XdndLeave", F(atom.xdnd_leave) },
1705 { "XdndDrop", F(atom.xdnd_drop) },
1706 { "XdndStatus", F(atom.xdnd_status) },
1707 { "XdndFinished", F(atom.xdnd_finished) },
1708 { "XdndTypeList", F(atom.xdnd_type_list) },
1709 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001710 };
1711#undef F
1712
1713 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1714 xcb_xfixes_query_version_reply_t *xfixes_reply;
1715 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1716 xcb_intern_atom_reply_t *reply;
1717 xcb_render_query_pict_formats_reply_t *formats_reply;
1718 xcb_render_query_pict_formats_cookie_t formats_cookie;
1719 xcb_render_pictforminfo_t *formats;
1720 uint32_t i;
1721
1722 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1723
1724 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1725
1726 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1727 cookies[i] = xcb_intern_atom (wm->conn, 0,
1728 strlen(atoms[i].name),
1729 atoms[i].name);
1730
1731 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1732 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1733 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1734 free(reply);
1735 }
1736
1737 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1738 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001739 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001740
1741 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1742 XCB_XFIXES_MAJOR_VERSION,
1743 XCB_XFIXES_MINOR_VERSION);
1744 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1745 xfixes_cookie, NULL);
1746
Martin Minarik6d118362012-06-07 18:01:59 +02001747 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001748 xfixes_reply->major_version, xfixes_reply->minor_version);
1749
1750 free(xfixes_reply);
1751
1752 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1753 formats_cookie, 0);
1754 if (formats_reply == NULL)
1755 return;
1756
1757 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001758 for (i = 0; i < formats_reply->num_formats; i++) {
1759 if (formats[i].direct.red_mask != 0xff &&
1760 formats[i].direct.red_shift != 16)
1761 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001762 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1763 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001764 wm->format_rgb = formats[i];
1765 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1766 formats[i].depth == 32 &&
1767 formats[i].direct.alpha_mask == 0xff &&
1768 formats[i].direct.alpha_shift == 24)
1769 wm->format_rgba = formats[i];
1770 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001771
1772 free(formats_reply);
1773}
1774
1775static void
1776weston_wm_create_wm_window(struct weston_wm *wm)
1777{
1778 static const char name[] = "Weston WM";
1779
1780 wm->wm_window = xcb_generate_id(wm->conn);
1781 xcb_create_window(wm->conn,
1782 XCB_COPY_FROM_PARENT,
1783 wm->wm_window,
1784 wm->screen->root,
1785 0, 0,
1786 10, 10,
1787 0,
1788 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1789 wm->screen->root_visual,
1790 0, NULL);
1791
1792 xcb_change_property(wm->conn,
1793 XCB_PROP_MODE_REPLACE,
1794 wm->wm_window,
1795 wm->atom.net_supporting_wm_check,
1796 XCB_ATOM_WINDOW,
1797 32, /* format */
1798 1, &wm->wm_window);
1799
1800 xcb_change_property(wm->conn,
1801 XCB_PROP_MODE_REPLACE,
1802 wm->wm_window,
1803 wm->atom.net_wm_name,
1804 wm->atom.utf8_string,
1805 8, /* format */
1806 strlen(name), name);
1807
1808 xcb_change_property(wm->conn,
1809 XCB_PROP_MODE_REPLACE,
1810 wm->screen->root,
1811 wm->atom.net_supporting_wm_check,
1812 XCB_ATOM_WINDOW,
1813 32, /* format */
1814 1, &wm->wm_window);
1815
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001816 /* Claim the WM_S0 selection even though we don't suport
1817 * the --replace functionality. */
1818 xcb_set_selection_owner(wm->conn,
1819 wm->wm_window,
1820 wm->atom.wm_s0,
1821 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001822
1823 xcb_set_selection_owner(wm->conn,
1824 wm->wm_window,
1825 wm->atom.net_wm_cm_s0,
1826 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001827}
1828
1829struct weston_wm *
1830weston_wm_create(struct weston_xserver *wxs)
1831{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001832 struct weston_wm *wm;
1833 struct wl_event_loop *loop;
1834 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001835 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001836 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001837 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001838
Peter Huttererf3d62272013-08-08 11:57:05 +10001839 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001840 if (wm == NULL)
1841 return NULL;
1842
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001843 wm->server = wxs;
1844 wm->window_hash = hash_table_create();
1845 if (wm->window_hash == NULL) {
1846 free(wm);
1847 return NULL;
1848 }
1849
1850 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001851 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001852 hash_table_destroy(wm->window_hash);
1853 free(wm);
1854 return NULL;
1855 }
1856
1857 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001858 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001859 close(sv[1]);
1860
1861 /* xcb_connect_to_fd takes ownership of the fd. */
1862 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1863 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001864 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001865 close(sv[0]);
1866 hash_table_destroy(wm->window_hash);
1867 free(wm);
1868 return NULL;
1869 }
1870
1871 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1872 wm->screen = s.data;
1873
1874 loop = wl_display_get_event_loop(wxs->wl_display);
1875 wm->source =
1876 wl_event_loop_add_fd(loop, sv[0],
1877 WL_EVENT_READABLE,
1878 weston_wm_handle_event, wm);
1879 wl_event_source_check(wm->source);
1880
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001881 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001882 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001883
1884 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001885 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1886 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1887 XCB_EVENT_MASK_PROPERTY_CHANGE;
1888 xcb_change_window_attributes(wm->conn, wm->screen->root,
1889 XCB_CW_EVENT_MASK, values);
1890 wm->theme = theme_create();
1891
1892 weston_wm_create_wm_window(wm);
1893
1894 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001895 supported[1] = wm->atom.net_wm_state;
1896 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001897 xcb_change_property(wm->conn,
1898 XCB_PROP_MODE_REPLACE,
1899 wm->screen->root,
1900 wm->atom.net_supported,
1901 XCB_ATOM_ATOM,
1902 32, /* format */
1903 ARRAY_LENGTH(supported), supported);
1904
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001905 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001906
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001907 weston_wm_dnd_init(wm);
1908
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001909 xcb_flush(wm->conn);
1910
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001911 wm->activate_listener.notify = weston_wm_window_activate;
1912 wl_signal_add(&wxs->compositor->activate_signal,
1913 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001914 wm->transform_listener.notify = weston_wm_window_transform;
1915 wl_signal_add(&wxs->compositor->transform_signal,
1916 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001917 wm->kill_listener.notify = weston_wm_kill_client;
1918 wl_signal_add(&wxs->compositor->kill_signal,
1919 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001920
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001921 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001922 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001923
Martin Minarik6d118362012-06-07 18:01:59 +02001924 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001925
1926 return wm;
1927}
1928
1929void
1930weston_wm_destroy(struct weston_wm *wm)
1931{
1932 /* FIXME: Free windows in hash. */
1933 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001934 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001935 xcb_disconnect(wm->conn);
1936 wl_event_source_remove(wm->source);
1937 wl_list_remove(&wm->selection_listener.link);
1938 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001939 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001940 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001941
1942 free(wm);
1943}
1944
1945static void
1946surface_destroy(struct wl_listener *listener, void *data)
1947{
1948 struct weston_wm_window *window =
1949 container_of(listener,
1950 struct weston_wm_window, surface_destroy_listener);
1951
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001952 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001953
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001954 /* This should have been freed by the shell.
1955 Don't try to use it later. */
1956 window->shsurf = NULL;
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001957 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001958}
1959
1960static struct weston_wm_window *
1961get_wm_window(struct weston_surface *surface)
1962{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001963 struct wl_listener *listener;
1964
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001965 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001966 if (listener)
1967 return container_of(listener, struct weston_wm_window,
1968 surface_destroy_listener);
1969
1970 return NULL;
1971}
1972
1973static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001974weston_wm_window_configure(void *data)
1975{
1976 struct weston_wm_window *window = data;
1977 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001978 uint32_t values[4];
1979 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001980
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001981 weston_wm_window_get_child_position(window, &x, &y);
1982 values[0] = x;
1983 values[1] = y;
1984 values[2] = window->width;
1985 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001986 xcb_configure_window(wm->conn,
1987 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001988 XCB_CONFIG_WINDOW_X |
1989 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001990 XCB_CONFIG_WINDOW_WIDTH |
1991 XCB_CONFIG_WINDOW_HEIGHT,
1992 values);
1993
1994 weston_wm_window_get_frame_size(window, &width, &height);
1995 values[0] = width;
1996 values[1] = height;
1997 xcb_configure_window(wm->conn,
1998 window->frame_id,
1999 XCB_CONFIG_WINDOW_WIDTH |
2000 XCB_CONFIG_WINDOW_HEIGHT,
2001 values);
2002
2003 window->configure_source = NULL;
2004
2005 weston_wm_window_schedule_repaint(window);
2006}
2007
2008static void
2009send_configure(struct weston_surface *surface,
2010 uint32_t edges, int32_t width, int32_t height)
2011{
2012 struct weston_wm_window *window = get_wm_window(surface);
2013 struct weston_wm *wm = window->wm;
2014 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002015 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002016
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002017 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002018 hborder = 0;
2019 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002020 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002021 hborder = 2 * (t->margin + t->width);
2022 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002023 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002024 hborder = 2 * t->margin;
2025 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002026 }
2027
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002028 if (width > hborder)
2029 window->width = width - hborder;
2030 else
2031 window->width = 1;
2032
2033 if (height > vborder)
2034 window->height = height - vborder;
2035 else
2036 window->height = 1;
2037
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002038 if (window->configure_source)
2039 return;
2040
2041 window->configure_source =
2042 wl_event_loop_add_idle(wm->server->loop,
2043 weston_wm_window_configure, window);
2044}
2045
2046static const struct weston_shell_client shell_client = {
2047 send_configure
2048};
2049
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002050static int
2051legacy_fullscreen(struct weston_wm *wm,
2052 struct weston_wm_window *window,
2053 struct weston_output **output_ret)
2054{
2055 struct weston_compositor *compositor = wm->server->compositor;
2056 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002057 uint32_t minmax = PMinSize | PMaxSize;
2058 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002059
2060 /* Heuristics for detecting legacy fullscreen windows... */
2061
2062 wl_list_for_each(output, &compositor->output_list, link) {
2063 if (output->x == window->x &&
2064 output->y == window->y &&
2065 output->width == window->width &&
2066 output->height == window->height &&
2067 window->override_redirect) {
2068 *output_ret = output;
2069 return 1;
2070 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002071
2072 matching_size = 0;
2073 if ((window->size_hints.flags & (USSize |PSize)) &&
2074 window->size_hints.width == output->width &&
2075 window->size_hints.height == output->height)
2076 matching_size = 1;
2077 if ((window->size_hints.flags & minmax) == minmax &&
2078 window->size_hints.min_width == output->width &&
2079 window->size_hints.min_height == output->height &&
2080 window->size_hints.max_width == output->width &&
2081 window->size_hints.max_height == output->height)
2082 matching_size = 1;
2083
2084 if (matching_size && !window->decorate &&
2085 (window->size_hints.flags & (USPosition | PPosition)) &&
2086 window->size_hints.x == output->x &&
2087 window->size_hints.y == output->y) {
2088 *output_ret = output;
2089 return 1;
2090 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002091 }
2092
2093 return 0;
2094}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002095static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002096xserver_map_shell_surface(struct weston_wm *wm,
2097 struct weston_wm_window *window)
2098{
2099 struct weston_shell_interface *shell_interface =
2100 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002101 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002102
2103 if (!shell_interface->create_shell_surface)
2104 return;
2105
2106 window->shsurf =
2107 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002108 window->surface,
2109 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002110
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002111 if (window->name)
2112 shell_interface->set_title(window->shsurf, window->name);
2113
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002114 if (window->fullscreen) {
2115 window->saved_width = window->width;
2116 window->saved_height = window->height;
2117 shell_interface->set_fullscreen(window->shsurf,
2118 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2119 0, NULL);
2120 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002121 } else if (legacy_fullscreen(wm, window, &output)) {
2122 window->fullscreen = 1;
2123 shell_interface->set_fullscreen(window->shsurf,
2124 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2125 0, output);
Giulio Camuffoca43f092013-09-11 17:49:13 +02002126 } else if (!window->override_redirect && !window->transient_for) {
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002127 shell_interface->set_toplevel(window->shsurf);
2128 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002129 } else {
2130 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002131 window->x,
2132 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002133 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002134 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002135}
2136
2137static void
2138xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2139 struct wl_resource *surface_resource, uint32_t id)
2140{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002141 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002142 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002143 struct weston_surface *surface =
2144 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002145 struct weston_wm_window *window;
2146
2147 if (client != wxs->client)
2148 return;
2149
2150 window = hash_table_lookup(wm->window_hash, id);
2151 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002152 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002153 return;
2154 }
2155
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002156 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002157
2158 weston_wm_window_read_properties(window);
2159
Giulio Camuffo1cf329b2013-09-20 16:16:06 +02002160 /* A weston_wm_window may have many different surfaces assigned
2161 * throughout its life, so we must make sure to remove the listener
2162 * from the old surface signal list. */
2163 if (window->surface)
2164 wl_list_remove(&window->surface_destroy_listener.link);
2165
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002166 window->surface = (struct weston_surface *) surface;
2167 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002168 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002169 &window->surface_destroy_listener);
2170
2171 weston_wm_window_schedule_repaint(window);
2172 xserver_map_shell_surface(wm, window);
2173}
2174
2175const struct xserver_interface xserver_implementation = {
2176 xserver_set_window_id
2177};