xwayland.c 36 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Copyright © 2011-2014 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of the
 * copyright holders not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include "xwayland.h"

#include <stdio.h>

30
#include <X11/Xatom.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
31
32
33
34
#include <selection.h>
#include <micmap.h>
#include <misyncshm.h>
#include <compositeext.h>
35
#include <compint.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
36
#include <glx_extinit.h>
37
#include <os.h>
38
#include <xserver_poll.h>
39
#include <propertyst.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
40

41
42
43
44
45
#ifdef XF86VIDMODE
#include <X11/extensions/xf86vmproto.h>
_X_EXPORT Bool noXFree86VidModeExtension;
#endif

Kristian Høgsberg's avatar
Kristian Høgsberg committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void
ddxGiveUp(enum ExitCode error)
{
}

void
AbortDDX(enum ExitCode error)
{
    ddxGiveUp(error);
}

void
OsVendorInit(void)
{
60
61
    if (serverGeneration == 1)
        ForceClockId(CLOCK_MONOTONIC);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
}

void
OsVendorFatalError(const char *f, va_list args)
{
}

#if defined(DDXBEFORERESET)
void
ddxBeforeReset(void)
{
    return;
}
#endif

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 _X_NORETURN
static void _X_ATTRIBUTE_PRINTF(1, 2)
xwl_give_up(const char *f, ...)
{
    va_list args;

    va_start(args, f);
    VErrorFSigSafe(f, args);
    va_end(args);

    CloseWellKnownConnections();
    OsCleanup(TRUE);
    fflush(stderr);
    exit(1);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
93
94
95
96
97
98
void
ddxUseMsg(void)
{
    ErrorF("-rootless              run rootless, requires wm support\n");
    ErrorF("-wm fd                 create X client for wm on given fd\n");
    ErrorF("-listen fd             add give fd as a listen socket\n");
99
    ErrorF("-eglstream             use eglstream backend for nvidia GPUs\n");
Kristian Høgsberg's avatar
Kristian Høgsberg committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
}

int
ddxProcessArgument(int argc, char *argv[], int i)
{
    if (strcmp(argv[i], "-rootless") == 0) {
        return 1;
    }
    else if (strcmp(argv[i], "-listen") == 0) {
        NoListenAll = TRUE;
        return 2;
    }
    else if (strcmp(argv[i], "-wm") == 0) {
        return 2;
    }
    else if (strcmp(argv[i], "-shm") == 0) {
        return 1;
    }
118
119
120
    else if (strcmp(argv[i], "-eglstream") == 0) {
        return 1;
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
121
122
123
124
125
126
127

    return 0;
}

static DevPrivateKeyRec xwl_window_private_key;
static DevPrivateKeyRec xwl_screen_private_key;
static DevPrivateKeyRec xwl_pixmap_private_key;
128
static DevPrivateKeyRec xwl_damage_private_key;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
129

130
static struct xwl_window *
131
xwl_window_get(WindowPtr window)
132
{
133
    return dixLookupPrivate(&window->devPrivates, &xwl_window_private_key);
134
135
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
136
137
138
139
140
141
struct xwl_screen *
xwl_screen_get(ScreenPtr screen)
{
    return dixLookupPrivate(&screen->devPrivates, &xwl_screen_private_key);
}

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
static void
xwl_window_set_allow_commits(struct xwl_window *xwl_window, Bool allow,
                             const char *debug_msg)
{
    xwl_window->allow_commits = allow;
    DebugF("xwayland: win %d allow_commits = %d (%s)\n",
           xwl_window->window->drawable.id, allow, debug_msg);
}

static void
xwl_window_set_allow_commits_from_property(struct xwl_window *xwl_window,
                                           PropertyPtr prop)
{
    static Bool warned = FALSE;
    CARD32 *propdata;

    if (prop->propertyName != xwl_window->xwl_screen->allow_commits_prop)
        FatalError("Xwayland internal error: prop mismatch in %s.\n", __func__);

    if (prop->type != XA_CARDINAL || prop->format != 32 || prop->size != 1) {
        /* Not properly set, so fall back to safe and glitchy */
        xwl_window_set_allow_commits(xwl_window, TRUE, "WM fault");

        if (!warned) {
            LogMessage(X_WARNING, "Window manager is misusing property %s.\n",
                       NameForAtom(prop->propertyName));
            warned = TRUE;
        }
        return;
    }

    propdata = prop->data;
    xwl_window_set_allow_commits(xwl_window, !!propdata[0], "from property");
}

static void
xwl_window_property_allow_commits(struct xwl_window *xwl_window,
                                  PropertyStateRec *propstate)
{
    Bool old_allow_commits = xwl_window->allow_commits;

    switch (propstate->state) {
    case PropertyNewValue:
        xwl_window_set_allow_commits_from_property(xwl_window, propstate->prop);
        break;

    case PropertyDelete:
        xwl_window_set_allow_commits(xwl_window, TRUE, "property deleted");
        break;

    default:
        break;
    }

    /* If allow_commits turned from off to on, discard any frame
     * callback we might be waiting for so that a new buffer is posted
     * immediately through block_handler() if there is damage to post.
     */
    if (!old_allow_commits && xwl_window->allow_commits) {
        if (xwl_window->frame_callback) {
            wl_callback_destroy(xwl_window->frame_callback);
            xwl_window->frame_callback = NULL;
        }
    }
}

static void
xwl_property_callback(CallbackListPtr *pcbl, void *closure,
                      void *calldata)
{
    ScreenPtr screen = closure;
    PropertyStateRec *rec = calldata;
    struct xwl_screen *xwl_screen;
    struct xwl_window *xwl_window;

    if (rec->win->drawable.pScreen != screen)
        return;

220
    xwl_window = xwl_window_get(rec->win);
221
222
223
224
225
226
227
228
229
    if (!xwl_window)
        return;

    xwl_screen = xwl_screen_get(screen);

    if (rec->prop->propertyName == xwl_screen->allow_commits_prop)
        xwl_window_property_allow_commits(xwl_window, rec);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
230
231
232
233
234
235
236
static Bool
xwl_close_screen(ScreenPtr screen)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
    struct xwl_output *xwl_output, *next_xwl_output;
    struct xwl_seat *xwl_seat, *next_xwl_seat;

237
238
    DeleteCallback(&PropertyStateCallback, xwl_property_callback, screen);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
239
240
241
242
243
244
245
246
    xorg_list_for_each_entry_safe(xwl_output, next_xwl_output,
                                  &xwl_screen->output_list, link)
        xwl_output_destroy(xwl_output);

    xorg_list_for_each_entry_safe(xwl_seat, next_xwl_seat,
                                  &xwl_screen->seat_list, link)
        xwl_seat_destroy(xwl_seat);

247
248
    xwl_screen_release_tablet_manager(xwl_screen);

249
250
    RemoveNotifyFd(xwl_screen->wayland_fd);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
251
252
253
254
255
256
257
258
    wl_display_disconnect(xwl_screen->display);

    screen->CloseScreen = xwl_screen->CloseScreen;
    free(xwl_screen);

    return screen->CloseScreen(screen);
}

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
struct xwl_window *
xwl_window_from_window(WindowPtr window)
{
    struct xwl_window *xwl_window;

    while (window) {
        xwl_window = xwl_window_get(window);
        if (xwl_window)
            return xwl_window;

        window = window->parent;
    }

    return NULL;
}

275
276
277
static struct xwl_seat *
xwl_screen_get_default_seat(struct xwl_screen *xwl_screen)
{
278
279
280
    if (xorg_list_is_empty(&xwl_screen->seat_list))
        return NULL;

281
282
283
284
285
    return container_of(xwl_screen->seat_list.prev,
                        struct xwl_seat,
                        link);
}

286
287
288
289
290
291
292
293
294
295
296
static void
xwl_cursor_warped_to(DeviceIntPtr device,
                     ScreenPtr screen,
                     ClientPtr client,
                     WindowPtr window,
                     SpritePtr sprite,
                     int x, int y)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
    struct xwl_seat *xwl_seat = device->public.devicePrivate;
    struct xwl_window *xwl_window;
297
    WindowPtr focus;
298
299
300
301

    if (!xwl_seat)
        xwl_seat = xwl_screen_get_default_seat(xwl_screen);

302
303
304
    if (!window)
        window = XYToWindow(sprite, x, y);

305
    xwl_window = xwl_window_from_window(window);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    if (!xwl_window && xwl_seat->focus_window) {
        focus = xwl_seat->focus_window->window;

        /* Warps on non wl_surface backed Windows are only allowed
         * as long as the pointer stays within the focus window.
         */
        if (x >= focus->drawable.x &&
            y >= focus->drawable.y &&
            x < focus->drawable.x + focus->drawable.width &&
            y < focus->drawable.y + focus->drawable.height) {
            if (!window) {
                DebugF("Warp relative to pointer, assuming pointer focus\n");
                xwl_window = xwl_seat->focus_window;
            } else if (window == screen->root) {
                DebugF("Warp on root window, assuming pointer focus\n");
                xwl_window = xwl_seat->focus_window;
            }
        }
    }
325
326
327
328
329
330
    if (!xwl_window)
        return;

    xwl_seat_emulate_pointer_warp(xwl_seat, xwl_window, sprite, x, y);
}

331
332
333
334
335
336
337
338
339
340
341
342
static void
xwl_cursor_confined_to(DeviceIntPtr device,
                       ScreenPtr screen,
                       WindowPtr window)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
    struct xwl_seat *xwl_seat = device->public.devicePrivate;
    struct xwl_window *xwl_window;

    if (!xwl_seat)
        xwl_seat = xwl_screen_get_default_seat(xwl_screen);

343
344
345
346
    /* xwl_seat hasn't been setup yet, don't do anything just yet */
    if (!xwl_seat)
        return;

347
348
349
350
351
    if (window == screen->root) {
        xwl_seat_unconfine_pointer(xwl_seat);
        return;
    }

352
    xwl_window = xwl_window_from_window(window);
353
354
355
356
357
358
359
360
361
    if (!xwl_window && xwl_seat->focus_window) {
        /* Allow confining on InputOnly windows, but only if the geometry
         * is the same than the focus window.
         */
        if (window->drawable.class == InputOnly) {
            DebugF("Confine on InputOnly window, assuming pointer focus\n");
            xwl_window = xwl_seat->focus_window;
        }
    }
362
363
364
365
366
367
    if (!xwl_window)
        return;

    xwl_seat_confine_pointer(xwl_seat, xwl_window);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
368
369
370
static void
damage_report(DamagePtr pDamage, RegionPtr pRegion, void *data)
{
371
372
373
374
375
376
377
378
    WindowPtr window = data;
    struct xwl_window *xwl_window = xwl_window_get(window);
    struct xwl_screen *xwl_screen;

    if (!xwl_window)
        return;

    xwl_screen = xwl_window->xwl_screen;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
379

380
381
382
383
384
385
386
387
388
389
390
391
#ifdef GLAMOR_HAS_GBM
    if (xwl_window->present_flipped) {
        /* This damage is from a Present flip, which already committed a new
         * buffer for the surface, so we don't need to do anything in response
         */
        RegionEmpty(DamageRegion(pDamage));
        xorg_list_del(&xwl_window->link_damage);
        xwl_window->present_flipped = FALSE;
        return;
    }
#endif

Kristian Høgsberg's avatar
Kristian Høgsberg committed
392
393
394
395
396
397
398
399
    xorg_list_add(&xwl_window->link_damage, &xwl_screen->damage_window_list);
}

static void
damage_destroy(DamagePtr pDamage, void *data)
{
}

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
static Bool
register_damage(WindowPtr window)
{
    DamagePtr damage;

    damage = DamageCreate(damage_report, damage_destroy, DamageReportNonEmpty,
                          FALSE, window->drawable.pScreen, window);
    if (damage == NULL) {
        ErrorF("Failed creating damage\n");
        return FALSE;
    }

    DamageRegister(&window->drawable, damage);
    DamageSetReportAfterOp(damage, TRUE);

    dixSetPrivate(&window->devPrivates, &xwl_damage_private_key, damage);

    return TRUE;
}

static void
unregister_damage(WindowPtr window)
{
    DamagePtr damage;

    damage = dixLookupPrivate(&window->devPrivates, &xwl_damage_private_key);
    if (!damage)
        return;

    DamageUnregister(damage);
    DamageDestroy(damage);

    dixSetPrivate(&window->devPrivates, &xwl_damage_private_key, NULL);
}

static DamagePtr
window_get_damage(WindowPtr window)
{
    return dixLookupPrivate(&window->devPrivates, &xwl_damage_private_key);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
static void
shell_surface_ping(void *data,
                   struct wl_shell_surface *shell_surface, uint32_t serial)
{
    wl_shell_surface_pong(shell_surface, serial);
}

static void
shell_surface_configure(void *data,
                        struct wl_shell_surface *wl_shell_surface,
                        uint32_t edges, int32_t width, int32_t height)
{
}

static void
shell_surface_popup_done(void *data, struct wl_shell_surface *wl_shell_surface)
{
}

static const struct wl_shell_surface_listener shell_surface_listener = {
    shell_surface_ping,
    shell_surface_configure,
    shell_surface_popup_done
};

void
xwl_pixmap_set_private(PixmapPtr pixmap, struct xwl_pixmap *xwl_pixmap)
{
    dixSetPrivate(&pixmap->devPrivates, &xwl_pixmap_private_key, xwl_pixmap);
}

struct xwl_pixmap *
xwl_pixmap_get(PixmapPtr pixmap)
{
    return dixLookupPrivate(&pixmap->devPrivates, &xwl_pixmap_private_key);
}

478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
static void
xwl_window_init_allow_commits(struct xwl_window *xwl_window)
{
    PropertyPtr prop = NULL;
    int ret;

    ret = dixLookupProperty(&prop, xwl_window->window,
                            xwl_window->xwl_screen->allow_commits_prop,
                            serverClient, DixReadAccess);
    if (ret == Success && prop)
        xwl_window_set_allow_commits_from_property(xwl_window, prop);
    else
        xwl_window_set_allow_commits(xwl_window, TRUE, "no property");
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
static void
send_surface_id_event(struct xwl_window *xwl_window)
{
    static const char atom_name[] = "WL_SURFACE_ID";
    static Atom type_atom;
    DeviceIntPtr dev;
    xEvent e;

    if (type_atom == None)
        type_atom = MakeAtom(atom_name, strlen(atom_name), TRUE);

    e.u.u.type = ClientMessage;
    e.u.u.detail = 32;
    e.u.clientMessage.window = xwl_window->window->drawable.id;
    e.u.clientMessage.u.l.type = type_atom;
    e.u.clientMessage.u.l.longs0 =
        wl_proxy_get_id((struct wl_proxy *) xwl_window->surface);
    e.u.clientMessage.u.l.longs1 = 0;
    e.u.clientMessage.u.l.longs2 = 0;
    e.u.clientMessage.u.l.longs3 = 0;
    e.u.clientMessage.u.l.longs4 = 0;

    dev = PickPointer(serverClient);
    DeliverEventsToWindow(dev, xwl_window->xwl_screen->screen->root,
                          &e, 1, SubstructureRedirectMask, NullGrab);
}

static Bool
521
ensure_surface_for_window(WindowPtr window)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
522
523
524
525
526
527
{
    ScreenPtr screen = window->drawable.pScreen;
    struct xwl_screen *xwl_screen;
    struct xwl_window *xwl_window;
    struct wl_region *region;

528
529
    if (xwl_window_get(window))
        return TRUE;
530

531
    xwl_screen = xwl_screen_get(screen);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
532
533
534

    if (xwl_screen->rootless) {
        if (window->redirectDraw != RedirectDrawManual)
535
            return TRUE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
536
537
538
    }
    else {
        if (window->parent)
539
            return TRUE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
540
541
    }

542
    xwl_window = calloc(1, sizeof *xwl_window);
543
544
545
    if (xwl_window == NULL)
        return FALSE;

Kristian Høgsberg's avatar
Kristian Høgsberg committed
546
547
548
549
550
    xwl_window->xwl_screen = xwl_screen;
    xwl_window->window = window;
    xwl_window->surface = wl_compositor_create_surface(xwl_screen->compositor);
    if (xwl_window->surface == NULL) {
        ErrorF("wl_display_create_surface failed\n");
551
        goto err;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
552
553
554
555
556
    }

    if (!xwl_screen->rootless) {
        xwl_window->shell_surface =
            wl_shell_get_shell_surface(xwl_screen->shell, xwl_window->surface);
557
558
559
560
561
        if (xwl_window->shell_surface == NULL) {
            ErrorF("Failed creating shell surface\n");
            goto err_surf;
        }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
562
563
564
565
566
567
        wl_shell_surface_add_listener(xwl_window->shell_surface,
                                      &shell_surface_listener, xwl_window);

        wl_shell_surface_set_toplevel(xwl_window->shell_surface);

        region = wl_compositor_create_region(xwl_screen->compositor);
568
569
570
571
572
        if (region == NULL) {
            ErrorF("Failed creating region\n");
            goto err_surf;
        }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
573
574
575
576
577
578
579
580
581
582
583
584
        wl_region_add(region, 0, 0,
                      window->drawable.width, window->drawable.height);
        wl_surface_set_opaque_region(xwl_window->surface, region);
        wl_region_destroy(region);
    }

    wl_display_flush(xwl_screen->display);

    send_surface_id_event(xwl_window);

    wl_surface_set_user_data(xwl_window->surface, xwl_window);

585
586
    compRedirectWindow(serverClient, window, CompositeRedirectManual);

587
    dixSetPrivate(&window->devPrivates, &xwl_window_private_key, xwl_window);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
588
589
    xorg_list_init(&xwl_window->link_damage);

590
591
    xwl_window_init_allow_commits(xwl_window);

592
    return TRUE;
593
594
595
596
597
598
599
600

err_surf:
    if (xwl_window->shell_surface)
        wl_shell_surface_destroy(xwl_window->shell_surface);
    wl_surface_destroy(xwl_window->surface);
err:
    free(xwl_window);
    return FALSE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
601
602
}

603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
static Bool
xwl_realize_window(WindowPtr window)
{
    ScreenPtr screen = window->drawable.pScreen;
    struct xwl_screen *xwl_screen;
    Bool ret;

    xwl_screen = xwl_screen_get(screen);

    screen->RealizeWindow = xwl_screen->RealizeWindow;
    ret = (*screen->RealizeWindow) (window);
    xwl_screen->RealizeWindow = screen->RealizeWindow;
    screen->RealizeWindow = xwl_realize_window;

    if (!ret)
        return FALSE;

    if (xwl_screen->rootless && !window->parent) {
        BoxRec box = { 0, 0, xwl_screen->width, xwl_screen->height };

        RegionReset(&window->winSize, &box);
        RegionNull(&window->clipList);
        RegionNull(&window->borderClip);
    }

    if (xwl_screen->rootless ?
        (window->drawable.class == InputOutput &&
         window->parent == window->drawable.pScreen->root) :
        !window->parent) {
        if (!register_damage(window))
            return FALSE;
    }

    return ensure_surface_for_window(window);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
639
640
641
642
643
644
645
646
647
648
649
650
static Bool
xwl_unrealize_window(WindowPtr window)
{
    ScreenPtr screen = window->drawable.pScreen;
    struct xwl_screen *xwl_screen;
    struct xwl_window *xwl_window;
    struct xwl_seat *xwl_seat;
    Bool ret;

    xwl_screen = xwl_screen_get(screen);

    xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
651
        if (xwl_seat->focus_window && xwl_seat->focus_window->window == window)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
652
            xwl_seat->focus_window = NULL;
653
654
        if (xwl_seat->tablet_focus_window && xwl_seat->tablet_focus_window->window == window)
            xwl_seat->tablet_focus_window = NULL;
655
656
        if (xwl_seat->last_xwindow == window)
            xwl_seat->last_xwindow = NullWindow;
657
658
659
        if (xwl_seat->cursor_confinement_window &&
            xwl_seat->cursor_confinement_window->window == window)
            xwl_seat_unconfine_pointer(xwl_seat);
660
661
662
663
        if (xwl_seat->pointer_warp_emulator &&
            xwl_seat->pointer_warp_emulator->locked_window &&
            xwl_seat->pointer_warp_emulator->locked_window->window == window)
            xwl_seat_destroy_pointer_warp_emulator(xwl_seat);
664
        xwl_seat_clear_touch(xwl_seat, window);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
665
666
    }

667
668
    compUnredirectWindow(serverClient, window, CompositeRedirectManual);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
669
670
671
672
673
    screen->UnrealizeWindow = xwl_screen->UnrealizeWindow;
    ret = (*screen->UnrealizeWindow) (window);
    xwl_screen->UnrealizeWindow = screen->UnrealizeWindow;
    screen->UnrealizeWindow = xwl_unrealize_window;

674
675
676
677
678
#ifdef GLAMOR_HAS_GBM
    if (xwl_screen->present)
        xwl_present_unrealize_window(window);
#endif

679
680
    xwl_window = xwl_window_get(window);
    if (!xwl_window)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
681
682
683
        return ret;

    wl_surface_destroy(xwl_window->surface);
684
    xorg_list_del(&xwl_window->link_damage);
685
686
    unregister_damage(window);

687
688
689
    if (xwl_window->frame_callback)
        wl_callback_destroy(xwl_window->frame_callback);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
690
691
692
693
694
695
696
697
698
699
700
701
    free(xwl_window);
    dixSetPrivate(&window->devPrivates, &xwl_window_private_key, NULL);

    return ret;
}

static Bool
xwl_save_screen(ScreenPtr pScreen, int on)
{
    return TRUE;
}

702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
static void
xwl_set_window_pixmap(WindowPtr window,
                      PixmapPtr pixmap)
{
    ScreenPtr screen = window->drawable.pScreen;
    struct xwl_screen *xwl_screen;

    xwl_screen = xwl_screen_get(screen);

    screen->SetWindowPixmap = xwl_screen->SetWindowPixmap;
    (*screen->SetWindowPixmap) (window, pixmap);
    xwl_screen->SetWindowPixmap = screen->SetWindowPixmap;
    screen->SetWindowPixmap = xwl_set_window_pixmap;

    if (!RegionNotEmpty(&window->winSize))
        return;

    ensure_surface_for_window(window);
}

722
723
724
725
726
727
static void
frame_callback(void *data,
               struct wl_callback *callback,
               uint32_t time)
{
    struct xwl_window *xwl_window = data;
728
729

    wl_callback_destroy (xwl_window->frame_callback);
730
731
732
733
734
735
736
    xwl_window->frame_callback = NULL;
}

static const struct wl_callback_listener frame_listener = {
    frame_callback
};

737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
static Bool
xwl_destroy_window(WindowPtr window)
{
    ScreenPtr screen = window->drawable.pScreen;
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
    Bool ret;

#ifdef GLAMOR_HAS_GBM
    if (xwl_screen->present)
        xwl_present_cleanup(window);
#endif

    screen->DestroyWindow = xwl_screen->DestroyWindow;

    if (screen->DestroyWindow)
        ret = screen->DestroyWindow (window);
    else
        ret = TRUE;

    xwl_screen->DestroyWindow = screen->DestroyWindow;
    screen->DestroyWindow = xwl_destroy_window;

    return ret;
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
762
static void
763
xwl_window_post_damage(struct xwl_window *xwl_window)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
764
{
765
    struct xwl_screen *xwl_screen = xwl_window->xwl_screen;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
766
767
768
769
    RegionPtr region;
    BoxPtr box;
    struct wl_buffer *buffer;
    PixmapPtr pixmap;
Pekka Paalanen's avatar
Pekka Paalanen committed
770
    int i;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
771

772
    assert(!xwl_window->frame_callback);
773

774
    region = DamageRegion(window_get_damage(xwl_window->window));
775
    pixmap = (*xwl_screen->screen->GetWindowPixmap) (xwl_window->window);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
776

Lyude Paul's avatar
Lyude Paul committed
777
#ifdef XWL_HAS_GLAMOR
778
    if (xwl_screen->glamor)
779
780
        buffer = xwl_glamor_pixmap_get_wl_buffer(pixmap,
                                                 NULL);
781
    else
782
#endif
783
        buffer = xwl_shm_pixmap_get_wl_buffer(pixmap);
784

785
786
787
788
789
#ifdef XWL_HAS_GLAMOR
    if (xwl_screen->glamor)
        xwl_glamor_post_damage(xwl_window, pixmap, region);
#endif

790
    wl_surface_attach(xwl_window->surface, buffer, 0, 0);
791

Pekka Paalanen's avatar
Pekka Paalanen committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
    /* Arbitrary limit to try to avoid flooding the Wayland
     * connection. If we flood it too much anyway, this could
     * abort in libwayland-client.
     */
    if (RegionNumRects(region) > 256) {
        box = RegionExtents(region);
        wl_surface_damage(xwl_window->surface, box->x1, box->y1,
                          box->x2 - box->x1, box->y2 - box->y1);
    } else {
        box = RegionRects(region);
        for (i = 0; i < RegionNumRects(region); i++, box++)
            wl_surface_damage(xwl_window->surface, box->x1, box->y1,
                              box->x2 - box->x1, box->y2 - box->y1);
    }
806

807
808
    xwl_window->frame_callback = wl_surface_frame(xwl_window->surface);
    wl_callback_add_listener(xwl_window->frame_callback, &frame_listener, xwl_window);
809

810
    wl_surface_commit(xwl_window->surface);
811
    DamageEmpty(window_get_damage(xwl_window->window));
Kristian Høgsberg's avatar
Kristian Høgsberg committed
812

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
    xorg_list_del(&xwl_window->link_damage);
}

static void
xwl_screen_post_damage(struct xwl_screen *xwl_screen)
{
    struct xwl_window *xwl_window, *next_xwl_window;

    xorg_list_for_each_entry_safe(xwl_window, next_xwl_window,
                                  &xwl_screen->damage_window_list, link_damage) {
        /* If we're waiting on a frame callback from the server,
         * don't attach a new buffer. */
        if (xwl_window->frame_callback)
            continue;

828
829
830
        if (!xwl_window->allow_commits)
            continue;

831
#ifdef XWL_HAS_GLAMOR
832
        if (xwl_screen->glamor && !xwl_glamor_allow_commits(xwl_window))
833
834
835
            continue;
#endif

836
        xwl_window_post_damage(xwl_window);
837
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
}

static void
registry_global(void *data, struct wl_registry *registry, uint32_t id,
                const char *interface, uint32_t version)
{
    struct xwl_screen *xwl_screen = data;

    if (strcmp(interface, "wl_compositor") == 0) {
        xwl_screen->compositor =
            wl_registry_bind(registry, id, &wl_compositor_interface, 1);
    }
    else if (strcmp(interface, "wl_shm") == 0) {
        xwl_screen->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
    }
    else if (strcmp(interface, "wl_shell") == 0) {
        xwl_screen->shell =
            wl_registry_bind(registry, id, &wl_shell_interface, 1);
    }
    else if (strcmp(interface, "wl_output") == 0 && version >= 2) {
858
859
        if (xwl_output_create(xwl_screen, id))
            xwl_screen->expecting_event++;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
860
    }
861
862
863
864
865
    else if (strcmp(interface, "zxdg_output_manager_v1") == 0) {
        xwl_screen->xdg_output_manager =
            wl_registry_bind(registry, id, &zxdg_output_manager_v1_interface, 1);
        xwl_screen_init_xdg_output(xwl_screen);
    }
Lyude Paul's avatar
Lyude Paul committed
866
#ifdef XWL_HAS_GLAMOR
867
868
869
    else if (xwl_screen->glamor) {
        xwl_glamor_init_wl_registry(xwl_screen, registry, id, interface,
                                    version);
870
871
    }
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
872
873
874
875
876
}

static void
global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
877
878
879
880
881
882
    struct xwl_screen *xwl_screen = data;
    struct xwl_output *xwl_output, *tmp_xwl_output;

    xorg_list_for_each_entry_safe(xwl_output, tmp_xwl_output,
                                  &xwl_screen->output_list, link) {
        if (xwl_output->server_output_id == name) {
883
            xwl_output_remove(xwl_output);
884
885
886
            break;
        }
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
887
888
889
890
891
892
893
894
}

static const struct wl_registry_listener registry_listener = {
    registry_global,
    global_remove
};

static void
895
xwl_read_events (struct xwl_screen *xwl_screen)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
896
897
898
{
    int ret;

899
900
901
    if (xwl_screen->wait_flush)
        return;

Kristian Høgsberg's avatar
Kristian Høgsberg committed
902
903
    ret = wl_display_read_events(xwl_screen->display);
    if (ret == -1)
904
        xwl_give_up("failed to read Wayland events: %s\n", strerror(errno));
Kristian Høgsberg's avatar
Kristian Høgsberg committed
905
906
907
908
909

    xwl_screen->prepare_read = 0;

    ret = wl_display_dispatch_pending(xwl_screen->display);
    if (ret == -1)
910
        xwl_give_up("failed to dispatch Wayland events: %s\n", strerror(errno));
Kristian Høgsberg's avatar
Kristian Høgsberg committed
911
912
}

913
914
915
916
917
918
919
920
921
922
923
static int
xwl_display_pollout (struct xwl_screen *xwl_screen, int timeout)
{
    struct pollfd poll_fd;

    poll_fd.fd = wl_display_get_fd(xwl_screen->display);
    poll_fd.events = POLLOUT;

    return xserver_poll(&poll_fd, 1, timeout);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
924
static void
925
xwl_dispatch_events (struct xwl_screen *xwl_screen)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
926
{
927
928
929
930
931
    int ret = 0;
    int ready;

    if (xwl_screen->wait_flush)
        goto pollout;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
932
933
934
935
936

    while (xwl_screen->prepare_read == 0 &&
           wl_display_prepare_read(xwl_screen->display) == -1) {
        ret = wl_display_dispatch_pending(xwl_screen->display);
        if (ret == -1)
937
            xwl_give_up("failed to dispatch Wayland events: %s\n",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
938
939
940
941
942
                       strerror(errno));
    }

    xwl_screen->prepare_read = 1;

943
944
945
pollout:
    ready = xwl_display_pollout(xwl_screen, 5);
    if (ready == -1 && errno != EINTR)
946
        xwl_give_up("error polling on XWayland fd: %s\n", strerror(errno));
947
948
949
950
951

    if (ready > 0)
        ret = wl_display_flush(xwl_screen->display);

    if (ret == -1 && errno != EAGAIN)
952
        xwl_give_up("failed to write to XWayland fd: %s\n", strerror(errno));
953
954

    xwl_screen->wait_flush = (ready == 0 || ready == -1 || ret == -1);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
955
956
}

957
958
959
960
961
962
963
964
965
static void
socket_handler(int fd, int ready, void *data)
{
    struct xwl_screen *xwl_screen = data;

    xwl_read_events (xwl_screen);
}

static void
966
wakeup_handler(void *data, int err)
967
968
969
970
{
}

static void
971
block_handler(void *data, void *timeout)
972
973
974
975
976
977
978
{
    struct xwl_screen *xwl_screen = data;

    xwl_screen_post_damage(xwl_screen);
    xwl_dispatch_events (xwl_screen);
}

979
980
981
982
983
984
985
void
xwl_sync_events (struct xwl_screen *xwl_screen)
{
    xwl_dispatch_events (xwl_screen);
    xwl_read_events (xwl_screen);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
static CARD32
add_client_fd(OsTimerPtr timer, CARD32 time, void *arg)
{
    struct xwl_screen *xwl_screen = arg;

    if (!AddClientOnOpenFD(xwl_screen->wm_fd))
        FatalError("Failed to add wm client\n");

    TimerFree(timer);

    return 0;
}

static void
listen_on_fds(struct xwl_screen *xwl_screen)
{
    int i;

    for (i = 0; i < xwl_screen->listen_fd_count; i++)
1005
        ListenOnOpenFD(xwl_screen->listen_fds[i], FALSE);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
}

static void
wm_selection_callback(CallbackListPtr *p, void *data, void *arg)
{
    SelectionInfoRec *info = arg;
    struct xwl_screen *xwl_screen = data;
    static const char atom_name[] = "WM_S0";
    static Atom atom_wm_s0;

    if (atom_wm_s0 == None)
        atom_wm_s0 = MakeAtom(atom_name, strlen(atom_name), TRUE);
    if (info->selection->selection != atom_wm_s0 ||
        info->kind != SelectionSetOwner)
        return;

    listen_on_fds(xwl_screen);

    DeleteCallback(&SelectionCallback, wm_selection_callback, xwl_screen);
}

static Bool
xwl_screen_init(ScreenPtr pScreen, int argc, char **argv)
{
1030
    static const char allow_commits[] = "_XWAYLAND_ALLOW_COMMITS";
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1031
1032
1033
    struct xwl_screen *xwl_screen;
    Pixel red_mask, blue_mask, green_mask;
    int ret, bpc, green_bpc, i;
1034
    Bool use_eglstreams = FALSE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1035

1036
    xwl_screen = calloc(1, sizeof *xwl_screen);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1037
1038
    if (xwl_screen == NULL)
        return FALSE;
1039
    xwl_screen->wm_fd = -1;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1040
1041
1042
1043
1044
1045
1046

    if (!dixRegisterPrivateKey(&xwl_screen_private_key, PRIVATE_SCREEN, 0))
        return FALSE;
    if (!dixRegisterPrivateKey(&xwl_window_private_key, PRIVATE_WINDOW, 0))
        return FALSE;
    if (!dixRegisterPrivateKey(&xwl_pixmap_private_key, PRIVATE_PIXMAP, 0))
        return FALSE;
1047
1048
    if (!dixRegisterPrivateKey(&xwl_damage_private_key, PRIVATE_WINDOW, 0))
        return FALSE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1049
1050
1051
1052

    dixSetPrivate(&pScreen->devPrivates, &xwl_screen_private_key, xwl_screen);
    xwl_screen->screen = pScreen;

Lyude Paul's avatar
Lyude Paul committed
1053
#ifdef XWL_HAS_GLAMOR
1054
1055
1056
    xwl_screen->glamor = 1;
#endif

Kristian Høgsberg's avatar
Kristian Høgsberg committed
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
    for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-rootless") == 0) {
            xwl_screen->rootless = 1;
        }
        else if (strcmp(argv[i], "-wm") == 0) {
            xwl_screen->wm_fd = atoi(argv[i + 1]);
            i++;
            TimerSet(NULL, 0, 1, add_client_fd, xwl_screen);
        }
        else if (strcmp(argv[i], "-listen") == 0) {
            if (xwl_screen->listen_fd_count ==
                ARRAY_SIZE(xwl_screen->listen_fds))
1069
                FatalError("Too many -listen arguments given, max is %zu\n",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1070
1071
1072
1073
1074
1075
                           ARRAY_SIZE(xwl_screen->listen_fds));

            xwl_screen->listen_fds[xwl_screen->listen_fd_count++] =
                atoi(argv[i + 1]);
            i++;
        }
1076
1077
1078
        else if (strcmp(argv[i], "-shm") == 0) {
            xwl_screen->glamor = 0;
        }
1079
        else if (strcmp(argv[i], "-eglstream") == 0) {
1080
#ifdef XWL_HAS_EGLSTREAM
1081
            use_eglstreams = TRUE;
1082
1083
#else
            ErrorF("xwayland glamor: this build does not have EGLStream support\n");
1084
#endif
1085
        }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1086
1087
    }

Lyude Paul's avatar
Lyude Paul committed
1088
#ifdef XWL_HAS_GLAMOR
1089
1090
    if (xwl_screen->glamor)
        xwl_glamor_init_backends(xwl_screen, use_eglstreams);
Lyude Paul's avatar
Lyude Paul committed
1091
#endif
1092

1093
1094
1095
1096
1097
1098
1099
    /* In rootless mode, we don't have any screen storage, and the only
     * rendering should be to redirected mode. */
    if (xwl_screen->rootless)
        xwl_screen->root_clip_mode = ROOT_CLIP_INPUT_ONLY;
    else
        xwl_screen->root_clip_mode = ROOT_CLIP_FULL;

Kristian Høgsberg's avatar
Kristian Høgsberg committed
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
    if (xwl_screen->listen_fd_count > 0) {
        if (xwl_screen->wm_fd >= 0)
            AddCallback(&SelectionCallback, wm_selection_callback, xwl_screen);
        else
            listen_on_fds(xwl_screen);
    }

    xorg_list_init(&xwl_screen->output_list);
    xorg_list_init(&xwl_screen->seat_list);
    xorg_list_init(&xwl_screen->damage_window_list);
    xwl_screen->depth = 24;

    xwl_screen->display = wl_display_connect(NULL);
    if (xwl_screen->display == NULL) {
        ErrorF("could not connect to wayland server\n");
        return FALSE;
    }

    if (!xwl_screen_init_output(xwl_screen))
        return FALSE;

    xwl_screen->expecting_event = 0;
    xwl_screen->registry = wl_display_get_registry(xwl_screen->display);
    wl_registry_add_listener(xwl_screen->registry,
                             &registry_listener, xwl_screen);
    ret = wl_display_roundtrip(xwl_screen->display);
    if (ret == -1) {
        ErrorF("could not connect to wayland server\n");
        return FALSE;
    }

    while (xwl_screen->expecting_event > 0)
        wl_display_roundtrip(xwl_screen->display);

    bpc = xwl_screen->depth / 3;
    green_bpc = xwl_screen->depth - 2 * bpc;
    blue_mask = (1 << bpc) - 1;
    green_mask = ((1 << green_bpc) - 1) << bpc;
    red_mask = blue_mask << (green_bpc + bpc);

    miSetVisualTypesAndMasks(xwl_screen->depth,
                             ((1 << TrueColor) | (1 << DirectColor)),
                             green_bpc, TrueColor,
                             red_mask, green_mask, blue_mask);

    miSetPixmapDepths();

    ret = fbScreenInit(pScreen, NULL,
                       xwl_screen->width, xwl_screen->height,
                       96, 96, 0,
                       BitsPerPixel(xwl_screen->depth));
    if (!ret)
        return FALSE;

    fbPictureInit(pScreen, 0, 0);

1156
#ifdef HAVE_XSHMFENCE
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1157
1158
    if (!miSyncShmScreenInit(pScreen))
        return FALSE;
1159
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1160
1161

    xwl_screen->wayland_fd = wl_display_get_fd(xwl_screen->display);
1162
    SetNotifyFd(xwl_screen->wayland_fd, socket_handler, X_NOTIFY_READ, xwl_screen);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
    RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, xwl_screen);

    pScreen->SaveScreen = xwl_save_screen;

    pScreen->blackPixel = 0;
    pScreen->whitePixel = 1;

    ret = fbCreateDefColormap(pScreen);

    if (!xwl_screen_init_cursor(xwl_screen))
        return FALSE;

Lyude Paul's avatar
Lyude Paul committed
1175
#ifdef XWL_HAS_GLAMOR
1176
1177
1178
1179
1180
1181
1182
    if (xwl_screen->glamor) {
        xwl_glamor_select_backend(xwl_screen, use_eglstreams);

        if (xwl_screen->egl_backend == NULL || !xwl_glamor_init(xwl_screen)) {
           ErrorF("Failed to initialize glamor, falling back to sw\n");
           xwl_screen->glamor = 0;
        }
1183
1184
    }

1185
1186
    if (xwl_screen->glamor && xwl_screen->rootless)
        xwl_screen->present = xwl_present_init(pScreen);
1187
#endif
1188

1189
1190
1191
1192
1193
1194
    if (!xwl_screen->glamor) {
        xwl_screen->CreateScreenResources = pScreen->CreateScreenResources;
        pScreen->CreateScreenResources = xwl_shm_create_screen_resources;
        pScreen->CreatePixmap = xwl_shm_create_pixmap;
        pScreen->DestroyPixmap = xwl_shm_destroy_pixmap;
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1195
1196
1197
1198
1199
1200
1201

    xwl_screen->RealizeWindow = pScreen->RealizeWindow;
    pScreen->RealizeWindow = xwl_realize_window;

    xwl_screen->UnrealizeWindow = pScreen->UnrealizeWindow;
    pScreen->UnrealizeWindow = xwl_unrealize_window;

1202
1203
1204
    xwl_screen->DestroyWindow = pScreen->DestroyWindow;
    pScreen->DestroyWindow = xwl_destroy_window;

Kristian Høgsberg's avatar
Kristian Høgsberg committed
1205
1206
1207
    xwl_screen->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = xwl_close_screen;

1208
1209
1210
1211
1212
    if (xwl_screen->rootless) {
        xwl_screen->SetWindowPixmap = pScreen->SetWindowPixmap;
        pScreen->SetWindowPixmap = xwl_set_window_pixmap;
    }

1213
    pScreen->CursorWarpedTo = xwl_cursor_warped_to;
1214
1215
    pScreen->CursorConfinedTo = xwl_cursor_confined_to;

1216
1217
1218
1219
1220
1221
1222
1223
    xwl_screen->allow_commits_prop = MakeAtom(allow_commits,
                                              strlen(allow_commits),
                                              TRUE);
    if (xwl_screen->allow_commits_prop == BAD_RESOURCE)
        return FALSE;

    AddCallback(&PropertyStateCallback, xwl_property_callback, pScreen);

1224
1225
1226
1227
    wl_display_roundtrip(xwl_screen->display);
    while (xwl_screen->expecting_event)
        wl_display_roundtrip(xwl_screen->display);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
1228
1229
1230
    return ret;
}

1231
_X_NORETURN
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1232
1233
1234
1235
1236
1237
1238
1239
1240
static void _X_ATTRIBUTE_PRINTF(1, 0)
xwl_log_handler(const char *format, va_list args)
{
    char msg[256];

    vsnprintf(msg, sizeof msg, format, args);
    FatalError("%s", msg);
}

1241
static const ExtensionModule xwayland_extensions[] = {
1242
1243
1244
#ifdef XF86VIDMODE
    { xwlVidModeExtensionInit, XF86VIDMODENAME, &noXFree86VidModeExtension },
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
};

void
InitOutput(ScreenInfo * screen_info, int argc, char **argv)
{
    int depths[] = { 1, 4, 8, 15, 16, 24, 32 };
    int bpp[] =    { 1, 8, 8, 16, 16, 32, 32 };
    int i;

    for (i = 0; i < ARRAY_SIZE(depths); i++) {
        screen_info->formats[i].depth = depths[i];
        screen_info->formats[i].bitsPerPixel = bpp[i];
        screen_info->formats[i].scanlinePad = BITMAP_SCANLINE_PAD;
    }

    screen_info->imageByteOrder = IMAGE_BYTE_ORDER;
    screen_info->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
    screen_info->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
    screen_info->bitmapBitOrder = BITMAP_BIT_ORDER;
    screen_info->numPixmapFormats = ARRAY_SIZE(depths);

1266
1267
1268
    if (serverGeneration == 1)
        LoadExtensionList(xwayland_extensions,
                          ARRAY_SIZE(xwayland_extensions), FALSE);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1269
1270
1271
1272
1273
1274
1275
1276
1277

    /* Cast away warning from missing printf annotation for
     * wl_log_func_t.  Wayland 1.5 will have the annotation, so we can
     * remove the cast and require that when it's released. */
    wl_log_set_handler_client((void *) xwl_log_handler);

    if (AddScreen(xwl_screen_init, argc, argv) == -1) {
        FatalError("Couldn't add screen\n");
    }
1278

1279
1280
    xorgGlxCreateVendor();

1281
    LocalAccessScopeUser();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1282
}