1 /*
2  * Copyright © 2011-2014 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without
6  * fee, provided that the above copyright notice appear in all copies
7  * and that both that copyright notice and this permission notice
8  * appear in supporting documentation, and that the name of the
9  * copyright holders not be used in advertising or publicity
10  * pertaining to distribution of the software without specific,
11  * written prior permission.  The copyright holders make no
12  * representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
21  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
22  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23  * SOFTWARE.
24  */
25 
26 #ifdef HAVE_DIX_CONFIG_H
27 #include <dix-config.h>
28 #endif
29 
30 #include <X11/X.h>
31 #include <X11/Xatom.h>
32 
33 #include "compositeext.h"
34 #include "compint.h"
35 #include "inputstr.h"
36 #include "propertyst.h"
37 
38 #include "xwayland-types.h"
39 #include "xwayland-input.h"
40 #include "xwayland-present.h"
41 #include "xwayland-screen.h"
42 #include "xwayland-window.h"
43 #include "xwayland-window-buffers.h"
44 #include "xwayland-shm.h"
45 
46 #include "viewporter-client-protocol.h"
47 #include "xdg-shell-client-protocol.h"
48 
49 static DevPrivateKeyRec xwl_window_private_key;
50 static DevPrivateKeyRec xwl_damage_private_key;
51 
52 static void
xwl_window_set_allow_commits(struct xwl_window * xwl_window,Bool allow,const char * debug_msg)53 xwl_window_set_allow_commits(struct xwl_window *xwl_window, Bool allow,
54                              const char *debug_msg)
55 {
56     xwl_window->allow_commits = allow;
57     DebugF("xwayland: win %d allow_commits = %d (%s)\n",
58            xwl_window->window->drawable.id, allow, debug_msg);
59 }
60 
61 static void
xwl_window_set_allow_commits_from_property(struct xwl_window * xwl_window,PropertyPtr prop)62 xwl_window_set_allow_commits_from_property(struct xwl_window *xwl_window,
63                                            PropertyPtr prop)
64 {
65     static Bool warned = FALSE;
66     CARD32 *propdata;
67 
68     if (prop->propertyName != xwl_window->xwl_screen->allow_commits_prop)
69         FatalError("Xwayland internal error: prop mismatch in %s.\n", __func__);
70 
71     if (prop->type != XA_CARDINAL || prop->format != 32 || prop->size != 1) {
72         /* Not properly set, so fall back to safe and glitchy */
73         xwl_window_set_allow_commits(xwl_window, TRUE, "WM fault");
74 
75         if (!warned) {
76             LogMessage(X_WARNING, "Window manager is misusing property %s.\n",
77                        NameForAtom(prop->propertyName));
78             warned = TRUE;
79         }
80         return;
81     }
82 
83     propdata = prop->data;
84     xwl_window_set_allow_commits(xwl_window, !!propdata[0], "from property");
85 }
86 
87 struct xwl_window *
xwl_window_get(WindowPtr window)88 xwl_window_get(WindowPtr window)
89 {
90     return dixLookupPrivate(&window->devPrivates, &xwl_window_private_key);
91 }
92 
93 static DamagePtr
window_get_damage(WindowPtr window)94 window_get_damage(WindowPtr window)
95 {
96     return dixLookupPrivate(&window->devPrivates, &xwl_damage_private_key);
97 }
98 
99 struct xwl_window *
xwl_window_from_window(WindowPtr window)100 xwl_window_from_window(WindowPtr window)
101 {
102     struct xwl_window *xwl_window;
103 
104     while (window) {
105         xwl_window = xwl_window_get(window);
106         if (xwl_window)
107             return xwl_window;
108 
109         window = window->parent;
110     }
111 
112     return NULL;
113 }
114 
115 void
xwl_window_update_property(struct xwl_window * xwl_window,PropertyStateRec * propstate)116 xwl_window_update_property(struct xwl_window *xwl_window,
117                            PropertyStateRec *propstate)
118 {
119     switch (propstate->state) {
120     case PropertyNewValue:
121         xwl_window_set_allow_commits_from_property(xwl_window, propstate->prop);
122         break;
123 
124     case PropertyDelete:
125         xwl_window_set_allow_commits(xwl_window, TRUE, "property deleted");
126         break;
127 
128     default:
129         break;
130     }
131 }
132 
133 static void
damage_report(DamagePtr pDamage,RegionPtr pRegion,void * data)134 damage_report(DamagePtr pDamage, RegionPtr pRegion, void *data)
135 {
136     WindowPtr window = data;
137     struct xwl_window *xwl_window = xwl_window_get(window);
138     struct xwl_screen *xwl_screen;
139 
140     if (!xwl_window)
141         return;
142 
143     xwl_screen = xwl_window->xwl_screen;
144 
145 #ifdef GLAMOR_HAS_GBM
146     if (xwl_window->present_flipped) {
147         /* This damage is from a Present flip, which already committed a new
148          * buffer for the surface, so we don't need to do anything in response
149          */
150         RegionEmpty(DamageRegion(pDamage));
151         xorg_list_del(&xwl_window->link_damage);
152         xwl_window->present_flipped = FALSE;
153         return;
154     }
155 #endif
156 
157     xorg_list_add(&xwl_window->link_damage, &xwl_screen->damage_window_list);
158 }
159 
160 static void
damage_destroy(DamagePtr pDamage,void * data)161 damage_destroy(DamagePtr pDamage, void *data)
162 {
163 }
164 
165 static Bool
register_damage(WindowPtr window)166 register_damage(WindowPtr window)
167 {
168     DamagePtr damage;
169 
170     damage = DamageCreate(damage_report, damage_destroy, DamageReportNonEmpty,
171                           FALSE, window->drawable.pScreen, window);
172     if (damage == NULL) {
173         ErrorF("Failed creating damage\n");
174         return FALSE;
175     }
176 
177     DamageRegister(&window->drawable, damage);
178     DamageSetReportAfterOp(damage, TRUE);
179 
180     dixSetPrivate(&window->devPrivates, &xwl_damage_private_key, damage);
181 
182     return TRUE;
183 }
184 
185 static void
unregister_damage(WindowPtr window)186 unregister_damage(WindowPtr window)
187 {
188     DamagePtr damage;
189 
190     damage = dixLookupPrivate(&window->devPrivates, &xwl_damage_private_key);
191     if (!damage)
192         return;
193 
194     DamageUnregister(damage);
195     DamageDestroy(damage);
196 
197     dixSetPrivate(&window->devPrivates, &xwl_damage_private_key, NULL);
198 }
199 
200 Bool
xwl_window_has_viewport_enabled(struct xwl_window * xwl_window)201 xwl_window_has_viewport_enabled(struct xwl_window *xwl_window)
202 {
203     return (xwl_window->viewport != NULL);
204 }
205 
206 static void
xwl_window_disable_viewport(struct xwl_window * xwl_window)207 xwl_window_disable_viewport(struct xwl_window *xwl_window)
208 {
209     assert (xwl_window->viewport);
210 
211     DebugF("XWAYLAND: disabling viewport\n");
212     wp_viewport_destroy(xwl_window->viewport);
213     xwl_window->viewport = NULL;
214 }
215 
216 static void
xwl_window_enable_viewport(struct xwl_window * xwl_window,struct xwl_output * xwl_output,struct xwl_emulated_mode * emulated_mode)217 xwl_window_enable_viewport(struct xwl_window *xwl_window,
218                            struct xwl_output *xwl_output,
219                            struct xwl_emulated_mode *emulated_mode)
220 {
221     if (!xwl_window_has_viewport_enabled(xwl_window)) {
222         DebugF("XWAYLAND: enabling viewport %dx%d -> %dx%d\n",
223                emulated_mode->width, emulated_mode->height,
224                xwl_output->width, xwl_output->height);
225         xwl_window->viewport = wp_viewporter_get_viewport(xwl_window->xwl_screen->viewporter,
226                                                           xwl_window->surface);
227     }
228 
229     wp_viewport_set_source(xwl_window->viewport,
230                            wl_fixed_from_int(0),
231                            wl_fixed_from_int(0),
232                            wl_fixed_from_int(emulated_mode->width),
233                            wl_fixed_from_int(emulated_mode->height));
234     wp_viewport_set_destination(xwl_window->viewport,
235                                 xwl_output->width,
236                                 xwl_output->height);
237 
238     xwl_window->scale_x = (float)emulated_mode->width  / xwl_output->width;
239     xwl_window->scale_y = (float)emulated_mode->height / xwl_output->height;
240 }
241 
242 static Bool
window_is_wm_window(WindowPtr window)243 window_is_wm_window(WindowPtr window)
244 {
245     struct xwl_screen *xwl_screen = xwl_screen_get(window->drawable.pScreen);
246 
247     return CLIENT_ID(window->drawable.id) == xwl_screen->wm_client_id;
248 }
249 
250 static WindowPtr
window_get_client_toplevel(WindowPtr window)251 window_get_client_toplevel(WindowPtr window)
252 {
253     assert(window);
254 
255     /* If the toplevel window is owned by the window-manager, then the
256      * actual client toplevel window has been reparented to some window-manager
257      * decoration/wrapper windows. In that case recurse by checking the client
258      * of the first *and only* child of the decoration/wrapper window.
259      */
260     if (window_is_wm_window(window)) {
261         if (window->firstChild && window->firstChild == window->lastChild)
262             return window_get_client_toplevel(window->firstChild);
263         else
264             return NULL; /* Should never happen, skip resolution emulation */
265     }
266 
267     return window;
268 }
269 
270 static Bool
xwl_window_should_enable_viewport(struct xwl_window * xwl_window,struct xwl_output ** xwl_output_ret,struct xwl_emulated_mode ** emulated_mode_ret)271 xwl_window_should_enable_viewport(struct xwl_window *xwl_window,
272                                   struct xwl_output **xwl_output_ret,
273                                   struct xwl_emulated_mode **emulated_mode_ret)
274 {
275     struct xwl_screen *xwl_screen = xwl_window->xwl_screen;
276     struct xwl_emulated_mode *emulated_mode;
277     struct xwl_output *xwl_output;
278     ClientPtr owner;
279     WindowPtr window;
280     DrawablePtr drawable;
281 
282     if (!xwl_screen_has_resolution_change_emulation(xwl_screen))
283         return FALSE;
284 
285     window = window_get_client_toplevel(xwl_window->window);
286     if (!window)
287         return FALSE;
288 
289     owner = wClient(window);
290     drawable = &window->drawable;
291 
292     /* 1. Test if the window matches the emulated mode on one of the outputs
293      * This path gets hit by most games / libs (e.g. SDL, SFML, OGRE)
294      */
295     xorg_list_for_each_entry(xwl_output, &xwl_screen->output_list, link) {
296         emulated_mode = xwl_output_get_emulated_mode_for_client(xwl_output, owner);
297         if (!emulated_mode)
298             continue;
299 
300         if (drawable->x == xwl_output->x &&
301             drawable->y == xwl_output->y &&
302             drawable->width  == emulated_mode->width &&
303             drawable->height == emulated_mode->height) {
304 
305             *emulated_mode_ret = emulated_mode;
306             *xwl_output_ret = xwl_output;
307             return TRUE;
308         }
309     }
310 
311     /* 2. Test if the window uses override-redirect + vidmode
312      * and matches (fully covers) the entire screen.
313      * This path gets hit by: allegro4, ClanLib-1.0.
314      */
315     xwl_output = xwl_screen_get_first_output(xwl_screen);
316     emulated_mode = xwl_output_get_emulated_mode_for_client(xwl_output, owner);
317     if (xwl_output && xwl_window->window->overrideRedirect &&
318         emulated_mode && emulated_mode->from_vidmode &&
319         drawable->x == 0 && drawable->y == 0 &&
320         drawable->width  == xwl_screen->width &&
321         drawable->height == xwl_screen->height) {
322 
323         *emulated_mode_ret = emulated_mode;
324         *xwl_output_ret = xwl_output;
325         return TRUE;
326     }
327 
328     return FALSE;
329 }
330 
331 void
xwl_window_check_resolution_change_emulation(struct xwl_window * xwl_window)332 xwl_window_check_resolution_change_emulation(struct xwl_window *xwl_window)
333 {
334     struct xwl_emulated_mode *emulated_mode;
335     struct xwl_output *xwl_output;
336 
337     if (xwl_window_should_enable_viewport(xwl_window, &xwl_output, &emulated_mode))
338         xwl_window_enable_viewport(xwl_window, xwl_output, emulated_mode);
339     else if (xwl_window_has_viewport_enabled(xwl_window))
340         xwl_window_disable_viewport(xwl_window);
341 }
342 
343 /* This checks if the passed in Window is a toplevel client window, note this
344  * returns false for window-manager decoration windows and returns true for
345  * the actual client top-level window even if it has been reparented to
346  * a window-manager decoration window.
347  */
348 Bool
xwl_window_is_toplevel(WindowPtr window)349 xwl_window_is_toplevel(WindowPtr window)
350 {
351     if (window_is_wm_window(window))
352         return FALSE;
353 
354     /* CSD and override-redirect toplevel windows */
355     if (window_get_damage(window))
356         return TRUE;
357 
358     /* Normal toplevel client windows, reparented to a window-manager window */
359     return window->parent && window_is_wm_window(window->parent);
360 }
361 
362 static void
xwl_window_init_allow_commits(struct xwl_window * xwl_window)363 xwl_window_init_allow_commits(struct xwl_window *xwl_window)
364 {
365     PropertyPtr prop = NULL;
366     int ret;
367 
368     ret = dixLookupProperty(&prop, xwl_window->window,
369                             xwl_window->xwl_screen->allow_commits_prop,
370                             serverClient, DixReadAccess);
371     if (ret == Success && prop)
372         xwl_window_set_allow_commits_from_property(xwl_window, prop);
373     else
374         xwl_window_set_allow_commits(xwl_window, TRUE, "no property");
375 }
376 
377 static void
send_surface_id_event(struct xwl_window * xwl_window)378 send_surface_id_event(struct xwl_window *xwl_window)
379 {
380     static const char atom_name[] = "WL_SURFACE_ID";
381     static Atom type_atom;
382     DeviceIntPtr dev;
383     xEvent e;
384 
385     if (type_atom == None)
386         type_atom = MakeAtom(atom_name, strlen(atom_name), TRUE);
387 
388     e.u.u.type = ClientMessage;
389     e.u.u.detail = 32;
390     e.u.clientMessage.window = xwl_window->window->drawable.id;
391     e.u.clientMessage.u.l.type = type_atom;
392     e.u.clientMessage.u.l.longs0 =
393         wl_proxy_get_id((struct wl_proxy *) xwl_window->surface);
394     e.u.clientMessage.u.l.longs1 = 0;
395     e.u.clientMessage.u.l.longs2 = 0;
396     e.u.clientMessage.u.l.longs3 = 0;
397     e.u.clientMessage.u.l.longs4 = 0;
398 
399     dev = PickPointer(serverClient);
400     DeliverEventsToWindow(dev, xwl_window->xwl_screen->screen->root,
401                           &e, 1, SubstructureRedirectMask, NullGrab);
402 }
403 
404 static void
xdg_surface_handle_configure(void * data,struct xdg_surface * xdg_surface,uint32_t serial)405 xdg_surface_handle_configure(void *data,
406                              struct xdg_surface *xdg_surface,
407                              uint32_t serial)
408 {
409     xdg_surface_ack_configure(xdg_surface, serial);
410 }
411 
412 static const struct xdg_surface_listener xdg_surface_listener = {
413     xdg_surface_handle_configure,
414 };
415 
416 static Bool
ensure_surface_for_window(WindowPtr window)417 ensure_surface_for_window(WindowPtr window)
418 {
419     ScreenPtr screen = window->drawable.pScreen;
420     struct xwl_screen *xwl_screen;
421     struct xwl_window *xwl_window;
422     struct wl_region *region;
423     WindowPtr toplevel;
424 
425     if (xwl_window_get(window))
426         return TRUE;
427 
428     xwl_screen = xwl_screen_get(screen);
429 
430     if (xwl_screen->rootless) {
431         if (window->redirectDraw != RedirectDrawManual)
432             return TRUE;
433     }
434     else {
435         if (window->parent)
436             return TRUE;
437     }
438 
439     xwl_window = calloc(1, sizeof *xwl_window);
440     if (xwl_window == NULL)
441         return FALSE;
442 
443     xwl_window->xwl_screen = xwl_screen;
444     xwl_window->window = window;
445     xwl_window->surface = wl_compositor_create_surface(xwl_screen->compositor);
446     if (xwl_window->surface == NULL) {
447         ErrorF("wl_display_create_surface failed\n");
448         goto err;
449     }
450 
451     if (!xwl_screen->rootless) {
452         xwl_window->xdg_surface =
453             xdg_wm_base_get_xdg_surface(xwl_screen->xdg_wm_base, xwl_window->surface);
454         if (xwl_window->xdg_surface == NULL) {
455             ErrorF("Failed creating xdg_wm_base xdg_surface\n");
456             goto err_surf;
457         }
458 
459         xdg_surface_add_listener(xwl_window->xdg_surface,
460                                  &xdg_surface_listener, xwl_window);
461 
462         xdg_surface_get_toplevel(xwl_window->xdg_surface);
463 
464         wl_surface_commit(xwl_window->surface);
465 
466         region = wl_compositor_create_region(xwl_screen->compositor);
467         if (region == NULL) {
468             ErrorF("Failed creating region\n");
469             goto err_surf;
470         }
471 
472         wl_region_add(region, 0, 0,
473                       window->drawable.width, window->drawable.height);
474         wl_surface_set_opaque_region(xwl_window->surface, region);
475         wl_region_destroy(region);
476     }
477 
478     wl_display_flush(xwl_screen->display);
479 
480     send_surface_id_event(xwl_window);
481 
482     wl_surface_set_user_data(xwl_window->surface, xwl_window);
483 
484     compRedirectWindow(serverClient, window, CompositeRedirectManual);
485 
486     dixSetPrivate(&window->devPrivates, &xwl_window_private_key, xwl_window);
487     xorg_list_init(&xwl_window->link_damage);
488     xorg_list_add(&xwl_window->link_window, &xwl_screen->window_list);
489 
490 #ifdef GLAMOR_HAS_GBM
491     xorg_list_init(&xwl_window->frame_callback_list);
492 #endif
493 
494     xwl_window_buffers_init(xwl_window);
495 
496     xwl_window_init_allow_commits(xwl_window);
497 
498     /* When a new window-manager window is realized, then the randr emulation
499      * props may have not been set on the managed client window yet.
500      */
501     if (window_is_wm_window(window)) {
502         toplevel = window_get_client_toplevel(window);
503         if (toplevel)
504             xwl_output_set_window_randr_emu_props(xwl_screen, toplevel);
505     } else {
506         /* CSD or O-R toplevel window, check viewport on creation */
507         xwl_window_check_resolution_change_emulation(xwl_window);
508     }
509 
510     return TRUE;
511 
512 err_surf:
513     if (xwl_window->xdg_surface)
514         xdg_surface_destroy(xwl_window->xdg_surface);
515     wl_surface_destroy(xwl_window->surface);
516 err:
517     free(xwl_window);
518     return FALSE;
519 }
520 
521 Bool
xwl_realize_window(WindowPtr window)522 xwl_realize_window(WindowPtr window)
523 {
524     ScreenPtr screen = window->drawable.pScreen;
525     struct xwl_screen *xwl_screen;
526     Bool ret;
527 
528     xwl_screen = xwl_screen_get(screen);
529 
530     screen->RealizeWindow = xwl_screen->RealizeWindow;
531     ret = (*screen->RealizeWindow) (window);
532     xwl_screen->RealizeWindow = screen->RealizeWindow;
533     screen->RealizeWindow = xwl_realize_window;
534 
535     if (!ret)
536         return FALSE;
537 
538     if (xwl_screen->rootless && !window->parent) {
539         BoxRec box = { 0, 0, xwl_screen->width, xwl_screen->height };
540 
541         RegionReset(&window->winSize, &box);
542         RegionNull(&window->clipList);
543         RegionNull(&window->borderClip);
544     }
545 
546     if (xwl_screen->rootless ?
547         (window->drawable.class == InputOutput &&
548          window->parent == window->drawable.pScreen->root) :
549         !window->parent) {
550         if (!register_damage(window))
551             return FALSE;
552     }
553 
554     return ensure_surface_for_window(window);
555 }
556 
557 Bool
xwl_unrealize_window(WindowPtr window)558 xwl_unrealize_window(WindowPtr window)
559 {
560     ScreenPtr screen = window->drawable.pScreen;
561     struct xwl_screen *xwl_screen;
562     struct xwl_window *xwl_window;
563     struct xwl_seat *xwl_seat;
564     Bool ret;
565 
566     xwl_screen = xwl_screen_get(screen);
567 
568     xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
569         if (xwl_seat->focus_window && xwl_seat->focus_window->window == window)
570             xwl_seat->focus_window = NULL;
571         if (xwl_seat->tablet_focus_window && xwl_seat->tablet_focus_window->window == window)
572             xwl_seat->tablet_focus_window = NULL;
573         if (xwl_seat->last_xwindow == window)
574             xwl_seat->last_xwindow = NullWindow;
575         if (xwl_seat->cursor_confinement_window &&
576             xwl_seat->cursor_confinement_window->window == window)
577             xwl_seat_unconfine_pointer(xwl_seat);
578         if (xwl_seat->pointer_warp_emulator &&
579             xwl_seat->pointer_warp_emulator->locked_window &&
580             xwl_seat->pointer_warp_emulator->locked_window->window == window)
581             xwl_seat_destroy_pointer_warp_emulator(xwl_seat);
582         xwl_seat_clear_touch(xwl_seat, window);
583     }
584 
585     compUnredirectWindow(serverClient, window, CompositeRedirectManual);
586 
587     screen->UnrealizeWindow = xwl_screen->UnrealizeWindow;
588     ret = (*screen->UnrealizeWindow) (window);
589     xwl_screen->UnrealizeWindow = screen->UnrealizeWindow;
590     screen->UnrealizeWindow = xwl_unrealize_window;
591 
592     xwl_window = xwl_window_get(window);
593     if (!xwl_window)
594         return ret;
595 
596     if (xwl_window_has_viewport_enabled(xwl_window))
597         xwl_window_disable_viewport(xwl_window);
598 
599     wl_surface_destroy(xwl_window->surface);
600     xorg_list_del(&xwl_window->link_damage);
601     xorg_list_del(&xwl_window->link_window);
602     unregister_damage(window);
603 
604     xwl_window_buffers_dispose(xwl_window);
605 
606     if (xwl_window->frame_callback)
607         wl_callback_destroy(xwl_window->frame_callback);
608 
609 #ifdef GLAMOR_HAS_GBM
610     if (xwl_screen->present) {
611         struct xwl_present_window *xwl_present_window, *tmp;
612 
613         xorg_list_for_each_entry_safe(xwl_present_window, tmp,
614                                       &xwl_window->frame_callback_list,
615                                       frame_callback_list) {
616             xwl_present_unrealize_window(xwl_present_window);
617         }
618     }
619 #endif
620 
621     free(xwl_window);
622     dixSetPrivate(&window->devPrivates, &xwl_window_private_key, NULL);
623 
624     return ret;
625 }
626 
627 void
xwl_window_set_window_pixmap(WindowPtr window,PixmapPtr pixmap)628 xwl_window_set_window_pixmap(WindowPtr window,
629                              PixmapPtr pixmap)
630 {
631     ScreenPtr screen = window->drawable.pScreen;
632     struct xwl_screen *xwl_screen;
633     struct xwl_window *xwl_window;
634     PixmapPtr old_pixmap;
635 
636     old_pixmap = (*screen->GetWindowPixmap) (window);
637     xwl_screen = xwl_screen_get(screen);
638 
639     screen->SetWindowPixmap = xwl_screen->SetWindowPixmap;
640     (*screen->SetWindowPixmap) (window, pixmap);
641     xwl_screen->SetWindowPixmap = screen->SetWindowPixmap;
642     screen->SetWindowPixmap = xwl_window_set_window_pixmap;
643 
644     if (!RegionNotEmpty(&window->winSize))
645         return;
646 
647     ensure_surface_for_window(window);
648 
649     if (old_pixmap->drawable.width == pixmap->drawable.width &&
650         old_pixmap->drawable.height == pixmap->drawable.height)
651        return;
652 
653     xwl_window = xwl_window_get(window);
654     if (xwl_window)
655             xwl_window_buffers_recycle(xwl_window);
656 }
657 
658 Bool
xwl_change_window_attributes(WindowPtr window,unsigned long mask)659 xwl_change_window_attributes(WindowPtr window, unsigned long mask)
660 {
661     ScreenPtr screen = window->drawable.pScreen;
662     struct xwl_screen *xwl_screen = xwl_screen_get(screen);
663     OtherClients *others;
664     Bool ret;
665 
666     screen->ChangeWindowAttributes = xwl_screen->ChangeWindowAttributes;
667     ret = (*screen->ChangeWindowAttributes) (window, mask);
668     xwl_screen->ChangeWindowAttributes = screen->ChangeWindowAttributes;
669     screen->ChangeWindowAttributes = xwl_change_window_attributes;
670 
671     if (window != screen->root || !(mask & CWEventMask))
672         return ret;
673 
674     for (others = wOtherClients(window); others; others = others->next) {
675         if (others->mask & (SubstructureRedirectMask | ResizeRedirectMask))
676             xwl_screen->wm_client_id = CLIENT_ID(others->resource);
677     }
678 
679     return ret;
680 }
681 
682 void
xwl_resize_window(WindowPtr window,int x,int y,unsigned int width,unsigned int height,WindowPtr sib)683 xwl_resize_window(WindowPtr window,
684                   int x, int y,
685                   unsigned int width, unsigned int height,
686                   WindowPtr sib)
687 {
688     ScreenPtr screen = window->drawable.pScreen;
689     struct xwl_screen *xwl_screen;
690     struct xwl_window *xwl_window;
691 
692     xwl_screen = xwl_screen_get(screen);
693     xwl_window = xwl_window_from_window(window);
694 
695     screen->ResizeWindow = xwl_screen->ResizeWindow;
696     (*screen->ResizeWindow) (window, x, y, width, height, sib);
697     xwl_screen->ResizeWindow = screen->ResizeWindow;
698     screen->ResizeWindow = xwl_resize_window;
699 
700     if (xwl_window && (xwl_window_get(window) || xwl_window_is_toplevel(window)))
701         xwl_window_check_resolution_change_emulation(xwl_window);
702 }
703 
704 void
xwl_move_window(WindowPtr window,int x,int y,WindowPtr next_sib,VTKind kind)705 xwl_move_window(WindowPtr window,
706                 int x, int y,
707                 WindowPtr next_sib,
708                 VTKind kind)
709 {
710     ScreenPtr screen = window->drawable.pScreen;
711     struct xwl_screen *xwl_screen;
712     struct xwl_window *xwl_window;
713 
714     xwl_screen = xwl_screen_get(screen);
715     xwl_window = xwl_window_from_window(window);
716 
717     screen->MoveWindow = xwl_screen->MoveWindow;
718     (*screen->MoveWindow) (window, x, y, next_sib, kind);
719     xwl_screen->MoveWindow = screen->MoveWindow;
720     screen->MoveWindow = xwl_move_window;
721 
722     if (xwl_window && (xwl_window_get(window) || xwl_window_is_toplevel(window)))
723         xwl_window_check_resolution_change_emulation(xwl_window);
724 }
725 
726 static void
frame_callback(void * data,struct wl_callback * callback,uint32_t time)727 frame_callback(void *data,
728                struct wl_callback *callback,
729                uint32_t time)
730 {
731     struct xwl_window *xwl_window = data;
732 
733     wl_callback_destroy (xwl_window->frame_callback);
734     xwl_window->frame_callback = NULL;
735 
736 #ifdef GLAMOR_HAS_GBM
737     if (xwl_window->xwl_screen->present) {
738         struct xwl_present_window *xwl_present_window, *tmp;
739 
740         xorg_list_for_each_entry_safe(xwl_present_window, tmp,
741                                       &xwl_window->frame_callback_list,
742                                       frame_callback_list) {
743             xwl_present_frame_callback(xwl_present_window);
744         }
745     }
746 #endif
747 }
748 
749 static const struct wl_callback_listener frame_listener = {
750     frame_callback
751 };
752 
753 void
xwl_window_create_frame_callback(struct xwl_window * xwl_window)754 xwl_window_create_frame_callback(struct xwl_window *xwl_window)
755 {
756     xwl_window->frame_callback = wl_surface_frame(xwl_window->surface);
757     wl_callback_add_listener(xwl_window->frame_callback, &frame_listener,
758                              xwl_window);
759 }
760 
761 Bool
xwl_destroy_window(WindowPtr window)762 xwl_destroy_window(WindowPtr window)
763 {
764     ScreenPtr screen = window->drawable.pScreen;
765     struct xwl_screen *xwl_screen = xwl_screen_get(screen);
766     Bool ret;
767 
768 #ifdef GLAMOR_HAS_GBM
769     if (xwl_screen->present)
770         xwl_present_cleanup(window);
771 #endif
772 
773     screen->DestroyWindow = xwl_screen->DestroyWindow;
774 
775     if (screen->DestroyWindow)
776         ret = screen->DestroyWindow (window);
777     else
778         ret = TRUE;
779 
780     xwl_screen->DestroyWindow = screen->DestroyWindow;
781     screen->DestroyWindow = xwl_destroy_window;
782 
783     return ret;
784 }
785 
786 void
xwl_window_post_damage(struct xwl_window * xwl_window)787 xwl_window_post_damage(struct xwl_window *xwl_window)
788 {
789     struct xwl_screen *xwl_screen = xwl_window->xwl_screen;
790     RegionPtr region;
791     BoxPtr box;
792     struct wl_buffer *buffer;
793     PixmapPtr pixmap;
794     int i;
795 
796     assert(!xwl_window->frame_callback);
797 
798     region = DamageRegion(window_get_damage(xwl_window->window));
799     pixmap = xwl_window_buffers_get_pixmap(xwl_window, region);
800 
801 #ifdef XWL_HAS_GLAMOR
802     if (xwl_screen->glamor)
803         buffer = xwl_glamor_pixmap_get_wl_buffer(pixmap,
804                                                  NULL);
805     else
806 #endif
807         buffer = xwl_shm_pixmap_get_wl_buffer(pixmap);
808 
809 #ifdef XWL_HAS_GLAMOR
810     if (xwl_screen->glamor)
811         xwl_glamor_post_damage(xwl_window, pixmap, region);
812 #endif
813 
814     wl_surface_attach(xwl_window->surface, buffer, 0, 0);
815 
816     /* Arbitrary limit to try to avoid flooding the Wayland
817      * connection. If we flood it too much anyway, this could
818      * abort in libwayland-client.
819      */
820     if (RegionNumRects(region) > 256) {
821         box = RegionExtents(region);
822         xwl_surface_damage(xwl_screen, xwl_window->surface,
823                            box->x1 + xwl_window->window->borderWidth,
824                            box->y1 + xwl_window->window->borderWidth,
825                            box->x2 - box->x1, box->y2 - box->y1);
826     } else {
827         box = RegionRects(region);
828         for (i = 0; i < RegionNumRects(region); i++, box++) {
829             xwl_surface_damage(xwl_screen, xwl_window->surface,
830                                box->x1 + xwl_window->window->borderWidth,
831                                box->y1 + xwl_window->window->borderWidth,
832                                box->x2 - box->x1, box->y2 - box->y1);
833         }
834     }
835 
836     xwl_window_create_frame_callback(xwl_window);
837     DamageEmpty(window_get_damage(xwl_window->window));
838 }
839 
840 Bool
xwl_window_init(void)841 xwl_window_init(void)
842 {
843     if (!dixRegisterPrivateKey(&xwl_window_private_key, PRIVATE_WINDOW, 0))
844         return FALSE;
845 
846     if (!dixRegisterPrivateKey(&xwl_damage_private_key, PRIVATE_WINDOW, 0))
847         return FALSE;
848 
849     return TRUE;
850 }
851