summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/darwin/bundle/rootlessWindow.c
blob: 974d7c3dcafb84d96f79be4e0e005a915ba598ce (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
 * Rootless window management
 *
 * Greg Parker     gparker@cs.stanford.edu
 */
/* $XFree86: xc/programs/Xserver/hw/darwin/bundle/rootlessWindow.c,v 1.9 2001/12/22 05:28:35 torrey Exp $ */

#include "rootlessCommon.h"
#include "rootlessWindow.h"

#include "fb.h"


// RootlessCreateWindow
// For now, don't create a frame until the window is realized.
// Do reset the window size so it's not clipped by the root window.
Bool
RootlessCreateWindow(WindowPtr pWin)
{
    Bool result;
    RegionRec saveRoot;

    WINREC(pWin) = NULL;
    SCREEN_UNWRAP(pWin->drawable.pScreen, CreateWindow);
    if (!IsRoot(pWin)) {
        // win/border size set by DIX, not by wrapped CreateWindow, so
        // correct it here.
        // Don't HUGE_ROOT when pWin is the root!
        HUGE_ROOT(pWin);
        SetWinSize(pWin);
        SetBorderSize(pWin);
    }
    result = pWin->drawable.pScreen->CreateWindow(pWin);
    if (pWin->parent) {
        NORMAL_ROOT(pWin);
    }
    SCREEN_WRAP(pWin->drawable.pScreen, CreateWindow);
    return result;
}


// RootlessDestroyWindow
// For now, all window destruction takes place in UnrealizeWindow
Bool
RootlessDestroyWindow(WindowPtr pWin)
{
    Bool result;

    SCREEN_UNWRAP(pWin->drawable.pScreen, DestroyWindow);
    result = pWin->drawable.pScreen->DestroyWindow(pWin);
    SCREEN_WRAP(pWin->drawable.pScreen, DestroyWindow);
    return result;
}


#ifdef SHAPE

// RootlessSetShape
// Shape is usually set before the window is mapped, but (for now) we
// don't keep track of frames before they're mapped. So we just record
// that the shape needs to updated later.
void
RootlessSetShape(WindowPtr pWin)
{
    ScreenPtr pScreen = pWin->drawable.pScreen;

    RootlessDamageShape(pWin);
    SCREEN_UNWRAP(pScreen, SetShape);
    pScreen->SetShape(pWin);
    SCREEN_WRAP(pScreen, SetShape);
}

#endif // SHAPE


// Disallow ParentRelative background on top-level windows
// because the root window doesn't really have the right background
// and cfb will try to draw on the root instead of on the window.
// fixme what about fb?
// fixme implement ParentRelative with real transparency?
// ParentRelative prevention is also in RealizeWindow()
Bool
RootlessChangeWindowAttributes(WindowPtr pWin, unsigned long vmask)
{
    Bool result;
    ScreenPtr pScreen = pWin->drawable.pScreen;

    RL_DEBUG_MSG("change window attributes start ");

    SCREEN_UNWRAP(pScreen, ChangeWindowAttributes);
    result = pScreen->ChangeWindowAttributes(pWin, vmask);
    SCREEN_WRAP(pScreen, ChangeWindowAttributes);

    if (WINREC(pWin)) {
        // disallow ParentRelative background state
        if (pWin->backgroundState == ParentRelative) {
            XID pixel = 0;
            ChangeWindowAttributes(pWin, CWBackPixel, &pixel, serverClient);
        }
    }

    RL_DEBUG_MSG("change window attributes end\n");
    return result;
}


// Update the frame position now.
// (x, y) are *inside* position!
// After this, mi and fb are expecting the pixmap to be at the new location.
Bool
RootlessPositionWindow(WindowPtr pWin, int x, int y)
{
    RootlessWindowRec *winRec = WINREC(pWin);
    ScreenPtr pScreen = pWin->drawable.pScreen;
    Bool result;

    RL_DEBUG_MSG("positionwindow start\n");
    if (winRec) {
        winRec->frame.x = x - pWin->borderWidth;
        winRec->frame.y = y - pWin->borderWidth;
    }

    UpdatePixmap(pWin);

    SCREEN_UNWRAP(pScreen, PositionWindow);
    result = pScreen->PositionWindow(pWin, x, y);
    SCREEN_WRAP(pScreen, PositionWindow);

    RL_DEBUG_MSG("positionwindow end\n");
    return result;
}


// RootlessRealizeWindow
// The frame is created here and not in CreateWindow.
// fixme change this? probably not - would be faster, but eat more memory
Bool
RootlessRealizeWindow(WindowPtr pWin)
{
    Bool result = FALSE;
    RegionRec saveRoot;
    ScreenPtr pScreen = pWin->drawable.pScreen;

    RL_DEBUG_MSG("realizewindow start ");

    if (IsTopLevel(pWin)  ||  IsRoot(pWin)) {
        DrawablePtr d = &pWin->drawable;
        RootlessWindowRec *winRec = xalloc(sizeof(RootlessWindowRec));

        if (! winRec) goto windowcreatebad;

        winRec->frame.isRoot = (pWin == WindowTable[pScreen->myNum]);
        winRec->frame.x = d->x - pWin->borderWidth;
        winRec->frame.y = d->y - pWin->borderWidth;
        winRec->frame.w = d->width + 2*pWin->borderWidth;
        winRec->frame.h = d->height + 2*pWin->borderWidth;
        winRec->frame.win = pWin;
        winRec->frame.devPrivate = NULL;

        REGION_INIT(pScreen, &winRec->damage, NullBox, 0);
        winRec->borderWidth = pWin->borderWidth;

        winRec->pixmap = NULL;
        // UpdatePixmap() called below

        WINREC(pWin) = winRec;

        RL_DEBUG_MSG("creating frame ");
        CallFrameProc(pScreen, CreateFrame,
                      (pScreen, &WINREC(pWin)->frame,
                      pWin->prevSib ? &WINREC(pWin->prevSib)->frame : NULL));

        // fixme implement ParentRelative with transparency?
        //  need non-interfering fb first
        // Disallow ParentRelative background state
        // This might have been set before the window was mapped
        if (pWin->backgroundState == ParentRelative) {
            XID pixel = 0;
            ChangeWindowAttributes(pWin, CWBackPixel, &pixel, serverClient);
        }

#ifdef SHAPE
        // Shape is usually set before the window is mapped, but
        // (for now) we don't keep track of frames before they're mapped.
        winRec->shapeDamage = TRUE;
#endif
    }

    UpdatePixmap(pWin);

    if (!IsRoot(pWin)) HUGE_ROOT(pWin);
    SCREEN_UNWRAP(pScreen, RealizeWindow);
    result = pScreen->RealizeWindow(pWin);
    SCREEN_WRAP(pScreen, RealizeWindow);
    if (!IsRoot(pWin)) NORMAL_ROOT(pWin);

    RL_DEBUG_MSG("realizewindow end\n");
    return result;

windowcreatebad:
    RL_DEBUG_MSG("window create bad! ");
    RL_DEBUG_MSG("realizewindow end\n");
    return NULL;
}


Bool
RootlessUnrealizeWindow(WindowPtr pWin)
{
    Bool result;
    ScreenPtr pScreen = pWin->drawable.pScreen;

    RL_DEBUG_MSG("unrealizewindow start ");

    if (IsTopLevel(pWin) || IsRoot(pWin)) {
        RootlessWindowRec *winRec = WINREC(pWin);

        RootlessRedisplay(pWin);
        CallFrameProc(pScreen, DestroyFrame, (pScreen, &winRec->frame));

        REGION_UNINIT(pScreen, &winRec->damage);

        xfree(winRec);
        WINREC(pWin) = NULL;
    }

    SCREEN_UNWRAP(pScreen, UnrealizeWindow);
    result = pScreen->UnrealizeWindow(pWin);
    SCREEN_WRAP(pScreen, UnrealizeWindow);
    RL_DEBUG_MSG("unrealizewindow end\n");
    return result;
}


void
RootlessRestackWindow(WindowPtr pWin, WindowPtr pOldNextSib)
{
    RegionRec saveRoot;
    RootlessWindowRec *winRec = WINREC(pWin);
    ScreenPtr pScreen = pWin->drawable.pScreen;

    RL_DEBUG_MSG("restackwindow start ");
    if (winRec) RL_DEBUG_MSG("restack top level \n");

    HUGE_ROOT(pWin);
    SCREEN_UNWRAP(pScreen, RestackWindow);
    if (pScreen->RestackWindow) pScreen->RestackWindow(pWin, pOldNextSib);
    SCREEN_WRAP(pScreen, RestackWindow);
    NORMAL_ROOT(pWin);

    if (winRec) {
        // fixme simplify the following

        WindowPtr oldNextW, newNextW, oldPrevW, newPrevW;
        RootlessFramePtr oldNext, newNext, oldPrev, newPrev;

        oldNextW = pOldNextSib;
        while (oldNextW  &&  ! WINREC(oldNextW)) oldNextW = oldNextW->nextSib;
        oldNext = oldNextW ? &WINREC(oldNextW)->frame : NULL;

        newNextW = pWin->nextSib;
        while (newNextW  &&  ! WINREC(newNextW)) newNextW = newNextW->nextSib;
        newNext = newNextW ? &WINREC(newNextW)->frame : NULL;

        oldPrevW= pOldNextSib ? pOldNextSib->prevSib : pWin->parent->lastChild;
        while (oldPrevW  &&  ! WINREC(oldPrevW)) oldPrevW = oldPrevW->prevSib;
        oldPrev = oldPrevW ? &WINREC(oldPrevW)->frame : NULL;

        newPrevW = pWin->prevSib;
        while (newPrevW  &&  ! WINREC(newPrevW)) newPrevW = newPrevW->prevSib;
        newPrev = newPrevW ? &WINREC(newPrevW)->frame : NULL;

        if (pWin->prevSib) {
            WindowPtr w = pWin->prevSib;
            while (w) {
                RL_DEBUG_MSG("w 0x%x\n", w);
                w = w->parent;
            }
        }

        CallFrameProc(pScreen, RestackFrame,
                      (pScreen, &winRec->frame, oldPrev, newPrev));
    }

    RL_DEBUG_MSG("restackwindow end\n");
}


/*
 * Specialized window copy procedures
 */

// Globals needed during window resize and move.
static PixmapPtr gResizeDeathPix = NULL;
static pointer gResizeDeathBits = NULL;
static PixmapPtr gResizeCopyWindowSource = NULL;
static CopyWindowProcPtr gResizeOldCopyWindowProc = NULL;

// CopyWindow() that doesn't do anything.
// For MoveWindow() of top-level windows.
static void
RootlessNoCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg,
                     RegionPtr prgnSrc)
{
    // some code expects the region to be translated
    int dx = ptOldOrg.x - pWin->drawable.x;
    int dy = ptOldOrg.y - pWin->drawable.y;
    RL_DEBUG_MSG("ROOTLESSNOCOPYWINDOW ");

    REGION_TRANSLATE(pWin->drawable.pScreen, prgnSrc, -dx, -dy);
}


// CopyWindow used during ResizeWindow for gravity moves.
// Cloned from fbCopyWindow
// The original always draws on the root pixmap (which we don't have).
// Instead, draw on the parent window's pixmap.
// Resize version: the old location's pixels are in gResizeCopyWindowSource
static void
RootlessResizeCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg,
                         RegionPtr prgnSrc)
{
    ScreenPtr pScreen = pWin->drawable.pScreen;
    SCREEN_UNWRAP(pScreen, CopyWindow);
    RL_DEBUG_MSG("resizecopywindowFB start (win 0x%x) ", pWin);

    {
        RegionRec   rgnDst;
        int         dx, dy;

        dx = ptOldOrg.x - pWin->drawable.x;
        dy = ptOldOrg.y - pWin->drawable.y;
        REGION_TRANSLATE(pScreen, prgnSrc, -dx, -dy);
        REGION_INIT (pScreen, &rgnDst, NullBox, 0);
        REGION_INTERSECT(pScreen, &rgnDst, &pWin->borderClip, prgnSrc);

        fbCopyRegion (&gResizeCopyWindowSource->drawable,
                      &pScreen->GetWindowPixmap(pWin)->drawable,
                      0,
                      &rgnDst, dx, dy, fbCopyWindowProc, 0, 0);

        // don't update - resize will update everything
        // fixme DO update?
        REGION_UNINIT(pScreen, &rgnDst);
        fbValidateDrawable (&pWin->drawable);
    }

    SCREEN_WRAP(pScreen, CopyWindow);
    RL_DEBUG_MSG("resizecopywindowFB end\n");
}


/* Update *new* location of window. Old location is redrawn with
 *  PaintWindowBackground/Border.
 * Cloned from fbCopyWindow
 * The original always draws on the root pixmap (which we don't have).
 * Instead, draw on the parent window's pixmap.
 */
void
RootlessCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
{
    ScreenPtr pScreen = pWin->drawable.pScreen;
    SCREEN_UNWRAP(pScreen, CopyWindow);
    RL_DEBUG_MSG("copywindowFB start (win 0x%x) ", pWin);

    {
        RegionRec   rgnDst;
        int         dx, dy;

        dx = ptOldOrg.x - pWin->drawable.x;
        dy = ptOldOrg.y - pWin->drawable.y;
        REGION_TRANSLATE(pScreen, prgnSrc, -dx, -dy);

        REGION_INIT(pScreen, &rgnDst, NullBox, 0);
        REGION_INTERSECT(pScreen, &rgnDst, &pWin->borderClip, prgnSrc);

        fbCopyRegion ((DrawablePtr)pWin, (DrawablePtr)pWin,
                      0,
                      &rgnDst, dx, dy, fbCopyWindowProc, 0, 0);

        // prgnSrc has been translated to dst position
        RootlessDamageRegion(pWin, prgnSrc);
        REGION_UNINIT(pScreen, &rgnDst);
        fbValidateDrawable (&pWin->drawable);
    }

    SCREEN_WRAP(pScreen, CopyWindow);
    RL_DEBUG_MSG("copywindowFB end\n");
}


/*
 * Window resize procedures
 */

// Prepare to resize a window.
// The old window's pixels are saved and the implementation is told
// to change the window size.
// (x,y,w,h) is outer frame of window (outside border)
static void
StartFrameResize(WindowPtr pWin, Bool gravity,
                 int oldX, int oldY,
                 unsigned int oldW, unsigned int oldH, unsigned int oldBW,
                 int newX, int newY,
                 unsigned int newW, unsigned int newH, unsigned int newBW)
{
    ScreenPtr pScreen = pWin->drawable.pScreen;
    RootlessWindowRec *winRec = WINREC(pWin);

    RL_DEBUG_MSG("RESIZE TOPLEVEL WINDOW ");
    RL_DEBUG_MSG("%d %d %d %d %d   %d %d %d %d %d  ",
                 oldX, oldY, oldW, oldH, oldBW,
                 newX, newY, newW, newH, newBW);

    RootlessRedisplay(pWin);

    // Make a copy of the current pixmap and all its data.
    // The original will go away when we ask the frame manager to
    // allocate the new pixmap.

    gResizeDeathBits = xalloc(winRec->frame.bytesPerRow * winRec->frame.h);
    memcpy(gResizeDeathBits, winRec->frame.pixelData,
           winRec->frame.bytesPerRow * winRec->frame.h);
    gResizeDeathPix =
        GetScratchPixmapHeader(pScreen, winRec->frame.w, winRec->frame.h,
                               winRec->frame.depth, winRec->frame.bitsPerPixel,
                               winRec->frame.bytesPerRow, gResizeDeathBits);
    SetPixmapBaseToScreen(gResizeDeathPix, winRec->frame.x, winRec->frame.y);

    winRec->frame.x = newX;
    winRec->frame.y = newY;
    winRec->frame.w = newW;
    winRec->frame.h = newH;
    winRec->borderWidth = newBW;

    CallFrameProc(pScreen, StartResizeFrame,
                  (pScreen, &winRec->frame, oldX, oldY, oldW, oldH));
    UpdatePixmap(pWin);

    // Use custom CopyWindow when moving gravity bits around
    // ResizeWindow assumes the old window contents are in the same
    // pixmap, but here they're in deathPix instead.
    if (gravity) {
        gResizeCopyWindowSource = gResizeDeathPix;
        gResizeOldCopyWindowProc = pScreen->CopyWindow;
        pScreen->CopyWindow = RootlessResizeCopyWindow;
    }

    // Copy pixels in intersection from src to dst.
    // ResizeWindow assumes these pixels are already present when
    // making gravity adjustments.
    // pWin currently has new-sized pixmap but is in old position
    // fixme border width change!
    {
        RegionRec r;
        DrawablePtr src = &gResizeDeathPix->drawable;
        DrawablePtr dst = &pScreen->GetWindowPixmap(pWin)->drawable;
       // These vars are needed because implicit unsigned->signed fails
       int oldX2 = (int)(oldX + oldW), newX2 = (int)(newX + newW);
       int oldY2 = (int)(oldY + oldH), newY2 = (int)(newY + newH);

        r.data = NULL;
        r.extents.x1 = max(oldX, newX);
        r.extents.y1 = max(oldY, newY);
        r.extents.x2 = min(oldX2, newX2);
        r.extents.y2 = min(oldY2, newY2);

        // r is now intersection of of old location and new location
        if (r.extents.x2 > r.extents.x1  &&  r.extents.y2 > r.extents.y1) {
#if 0
            DDXPointRec srcPt = {r.extents.x1, r.extents.y1};
            // Correct for border width change
            // fixme need to correct for border width change
            int dx = newX - oldX;
            int dy = newY - oldY;
            REGION_TRANSLATE(pScreen, &r, dx, dy);
#endif
            fbCopyRegion(src, dst, NULL, &r, 0, 0, fbCopyWindowProc, 0, 0);
        }
        REGION_UNINIT(pScreen, &r);
    }
}


static void
FinishFrameResize(WindowPtr pWin, Bool gravity,
                  int oldX, int oldY,
                  unsigned int oldW, unsigned int oldH, unsigned int oldBW,
                  int newX, int newY,
                  unsigned int newW, unsigned int newH, unsigned int newBW)
{
    ScreenPtr pScreen = pWin->drawable.pScreen;
    RootlessWindowRec *winRec = WINREC(pWin);

    CallFrameProc(pScreen, FinishResizeFrame,
                  (pScreen, &winRec->frame, oldX, oldY, oldW, oldH));
    if (wBoundingShape(pWin)) {
        RootlessDamageShape(pWin);
    }

    // Destroy temp pixmap
    FreeScratchPixmapHeader(gResizeDeathPix);
    xfree(gResizeDeathBits);
    gResizeDeathPix = gResizeDeathBits = NULL;

    if (gravity) {
        pScreen->CopyWindow = gResizeOldCopyWindowProc;
        gResizeCopyWindowSource = NULL;
    }
}


// If kind==VTOther, window border is resizing (and borderWidth is
//   already changed!!@#$)  This case works like window resize, not move.
void
RootlessMoveWindow(WindowPtr pWin, int x, int y, WindowPtr pSib, VTKind kind)
{
    CopyWindowProcPtr oldCopyWindowProc = NULL;
    RegionRec saveRoot;
    RootlessWindowRec *winRec = WINREC(pWin);
    ScreenPtr pScreen = pWin->drawable.pScreen;
    int oldX = 0, oldY = 0, newX = 0, newY = 0;
    unsigned int oldW = 0, oldH = 0, oldBW = 0, newW = 0, newH = 0, newBW = 0;

    RL_DEBUG_MSG("movewindow start \n");

    if (winRec) {
        if (kind == VTMove) {
            oldX = winRec->frame.x;
            oldY = winRec->frame.y;
            RootlessRedisplay(pWin);
        } else {
            RL_DEBUG_MSG("movewindow border resizing ");
            oldBW = winRec->borderWidth;
            oldX = winRec->frame.x;
            oldY = winRec->frame.y;
            oldW = winRec->frame.w;
            oldH = winRec->frame.h;
            newBW = pWin->borderWidth;
            newX = x;
            newY = y;
            newW = pWin->drawable.width  + 2*newBW;
            newH = pWin->drawable.height + 2*newBW;
            StartFrameResize(pWin, FALSE, oldX, oldY, oldW, oldH, oldBW,
                             newX, newY, newW, newH, newBW);
        }
    }

    HUGE_ROOT(pWin);
    SCREEN_UNWRAP(pScreen, MoveWindow);
    if (winRec) {
        oldCopyWindowProc = pScreen->CopyWindow;
        pScreen->CopyWindow = RootlessNoCopyWindow;
    }
    pScreen->MoveWindow(pWin, x, y, pSib, kind);
    if (winRec) {
        pScreen->CopyWindow = oldCopyWindowProc;
    }
    NORMAL_ROOT(pWin);
    SCREEN_WRAP(pScreen, MoveWindow);

    if (winRec) {
        if (kind == VTMove) {
            // PositionWindow has already set the new frame position.
            CallFrameProc(pScreen, MoveFrame,
                          (pScreen, &winRec->frame, oldX, oldY));
        } else {
            FinishFrameResize(pWin, FALSE, oldX, oldY, oldW, oldH, oldBW,
                              newX, newY, newW, newH, newBW);
        }
    }

    RL_DEBUG_MSG("movewindow end\n");
}


// Note: (x, y, w, h) as passed to this procedure don't match
//  the frame definition.
// (x,y) is corner of very outer edge, *outside* border
// w,h is width and height *inside8 border, *ignoring* border width
// The rect (x, y, w, h) doesn't mean anything.
// (x, y, w+2*bw, h+2*bw) is total rect
// (x+bw, y+bw, w, h) is inner rect

void
RootlessResizeWindow(WindowPtr pWin, int x, int y,
                     unsigned int w, unsigned int h, WindowPtr pSib)
{
    RegionRec saveRoot;
    RootlessWindowRec *winRec = WINREC(pWin);
    ScreenPtr pScreen = pWin->drawable.pScreen;
    int oldX = 0, oldY = 0, newX = 0, newY = 0;
    unsigned int oldW = 0, oldH = 0, oldBW = 0, newW = 0, newH = 0, newBW = 0;

    RL_DEBUG_MSG("resizewindow start (win 0x%x) ", pWin);

    if (winRec) {
        oldBW = winRec->borderWidth;
        oldX = winRec->frame.x;
        oldY = winRec->frame.y;
        oldW = winRec->frame.w;
        oldH = winRec->frame.h;

        newBW = oldBW;
        newX = x;
        newY = y;
        newW = w + 2*newBW;
        newH = h + 2*newBW;

        StartFrameResize(pWin, TRUE, oldX, oldY, oldW, oldH, oldBW,
                         newX, newY, newW, newH, newBW);
    }

    HUGE_ROOT(pWin);
    SCREEN_UNWRAP(pScreen, ResizeWindow);
    pScreen->ResizeWindow(pWin, x, y, w, h, pSib);
    SCREEN_WRAP(pScreen, ResizeWindow);
    NORMAL_ROOT(pWin);

    if (winRec) {
        FinishFrameResize(pWin, TRUE, oldX, oldY, oldW, oldH, oldBW,
                        newX, newY, newW, newH, newBW);
    }

    RL_DEBUG_MSG("resizewindow end\n");
}


// fixme untested!
// pWin inside corner stays the same
// pWin->drawable.[xy] stays the same
// frame moves and resizes
void
RootlessChangeBorderWidth(WindowPtr pWin, unsigned int width)
{
    RegionRec saveRoot;

    RL_DEBUG_MSG("change border width ");
    if (width != pWin->borderWidth) {
        RootlessWindowRec *winRec = WINREC(pWin);
        int oldX = 0, oldY = 0, newX = 0, newY = 0;
        unsigned int oldW = 0, oldH = 0, oldBW = 0;
        unsigned int newW = 0, newH = 0, newBW = 0;

        if (winRec) {
            oldBW = winRec->borderWidth;
            oldX = winRec->frame.x;
            oldY = winRec->frame.y;
            oldW = winRec->frame.w;
            oldH = winRec->frame.h;

            newBW = width;
            newX = pWin->drawable.x - newBW;
            newY = pWin->drawable.y - newBW;
            newW = pWin->drawable.width  + 2*newBW;
            newH = pWin->drawable.height + 2*newBW;

            StartFrameResize(pWin, FALSE, oldX, oldY, oldW, oldH, oldBW,
                            newX, newY, newW, newH, newBW);
        }

        HUGE_ROOT(pWin);
        SCREEN_UNWRAP(pWin->drawable.pScreen, ChangeBorderWidth);
        pWin->drawable.pScreen->ChangeBorderWidth(pWin, width);
        SCREEN_WRAP(pWin->drawable.pScreen, ChangeBorderWidth);
        NORMAL_ROOT(pWin);

        if (winRec) {
            FinishFrameResize(pWin, FALSE, oldX, oldY, oldW, oldH, oldBW,
                              newX, newY, newW, newH, newBW);
        }
    }
    RL_DEBUG_MSG("change border width end\n");
}