File: rootlessAquaImp.m

package info (click to toggle)
vnc4 4.1.1%2BX4.3.0-31
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 171,548 kB
  • ctags: 288,231
  • sloc: ansic: 2,205,256; cpp: 56,016; sh: 38,092; pascal: 13,773; asm: 12,656; tcl: 9,182; lisp: 7,831; perl: 3,338; makefile: 2,957; yacc: 2,902; objc: 2,698; xml: 2,614; python: 2,383; lex: 1,477; awk: 901; csh: 58; sed: 50
file content (433 lines) | stat: -rw-r--r-- 14,367 bytes parent folder | download | duplicates (6)
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
/*
 * Rootless implementation for Mac OS X Aqua environment
 */
/*
 * Copyright (c) 2001 Greg Parker. All Rights Reserved.
 * Copyright (c) 2002 Torrey T. Lyons. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name(s) of the above copyright
 * holders shall not be used in advertising or otherwise to promote the sale,
 * use or other dealings in this Software without prior written authorization.
 */
/* $XFree86: xc/programs/Xserver/hw/darwin/quartz/rootlessAquaImp.m,v 1.6 2003/01/24 00:11:39 torrey Exp $ */

#include "rootlessAquaImp.h"
#include "fakeBoxRec.h"
#include "quartzCommon.h"
#include "aquaCommon.h"
#include "pseudoramiX.h"
#import <Cocoa/Cocoa.h>
#include <ApplicationServices/ApplicationServices.h>
#import "XView.h"

extern void ErrorF(const char *, ...);


/*
 * AquaDisplayCount
 *  Return the number of displays.
 *  Multihead note: When rootless mode uses PseudoramiX, the
 *  X server only sees one screen; only PseudoramiX itself knows
 *  about all of the screens.
 */
int AquaDisplayCount()
{
    aquaNumScreens = [[NSScreen screens] count];

    if (noPseudoramiXExtension) {
        return aquaNumScreens;
    } else {
        return 1; // only PseudoramiX knows about the rest
    }
}


void AquaScreenInit(int index, int *x, int *y, int *width, int *height,
                    int *rowBytes, int *bps, int *spp, int *bpp)
{
    *spp = 3;
    *bps = CGDisplayBitsPerSample(kCGDirectMainDisplay);
    *bpp = CGDisplayBitsPerPixel(kCGDirectMainDisplay);

    if (noPseudoramiXExtension) {
        NSScreen *screen = [[NSScreen screens] objectAtIndex:index];
        NSRect frame = [screen frame];

        // set x, y so (0,0) is top left of main screen
        *x = NSMinX(frame);
        *y = NSHeight([[NSScreen mainScreen] frame]) - NSHeight(frame) -
            NSMinY(frame);

        *width =  NSWidth(frame);
        *height = NSHeight(frame);
        *rowBytes = (*width) * (*bpp) / 8;

        // Shift the usable part of main screen down to avoid the menu bar.
        if (NSEqualRects(frame, [[NSScreen mainScreen] frame])) {
            *y      += aquaMenuBarHeight;
            *height -= aquaMenuBarHeight;
        }

    } else {
        int i;
        NSRect unionRect = NSMakeRect(0, 0, 0, 0);
        NSArray *screens = [NSScreen screens];

        // Get the union of all screens (minus the menu bar on main screen)
        for (i = 0; i < [screens count]; i++) {
            NSScreen *screen = [screens objectAtIndex:i];
            NSRect frame = [screen frame];
            frame.origin.y = [[NSScreen mainScreen] frame].size.height -
                             frame.size.height - frame.origin.y;
            if (NSEqualRects([screen frame], [[NSScreen mainScreen] frame])) {
                frame.origin.y    += aquaMenuBarHeight;
                frame.size.height -= aquaMenuBarHeight;
            }
            unionRect = NSUnionRect(unionRect, frame);
        }

        // Use unionRect as the screen size for the X server.
        *x = unionRect.origin.x;
        *y = unionRect.origin.y;
        *width = unionRect.size.width;
        *height = unionRect.size.height;
        *rowBytes = (*width) * (*bpp) / 8;

        // Tell PseudoramiX about the real screens.
        // InitOutput() will move the big screen to (0,0),
        // so compensate for that here.
        for (i = 0; i < [screens count]; i++) {
            NSScreen *screen = [screens objectAtIndex:i];
            NSRect frame = [screen frame];
            int j;

            // Skip this screen if it's a mirrored copy of an earlier screen.
            for (j = 0; j < i; j++) {
                if (NSEqualRects(frame, [[screens objectAtIndex:j] frame])) {
                    ErrorF("PseudoramiX screen %d is a mirror of screen %d.\n",
                           i, j);
                    break;
                }
            }
            if (j < i) continue; // this screen is a mirrored copy

            frame.origin.y = [[NSScreen mainScreen] frame].size.height -
                             frame.size.height - frame.origin.y;

            if (NSEqualRects([screen frame], [[NSScreen mainScreen] frame])) {
                frame.origin.y    += aquaMenuBarHeight;
                frame.size.height -= aquaMenuBarHeight;
            }

            ErrorF("PseudoramiX screen %d added: %dx%d @ (%d,%d).\n", i,
                   (int)frame.size.width, (int)frame.size.height,
                   (int)frame.origin.x, (int)frame.origin.y);

            frame.origin.x -= unionRect.origin.x;
            frame.origin.y -= unionRect.origin.y;

            ErrorF("PseudoramiX screen %d placed at X11 coordinate (%d,%d).\n",
                   i, (int)frame.origin.x, (int)frame.origin.y);

            PseudoramiXAddScreen(frame.origin.x, frame.origin.y,
                                 frame.size.width, frame.size.height);
        }
    }
}


/*
 * AquaNewWindow
 *  Create a new on-screen window.
 *  Rootless windows must not autodisplay! Autodisplay can cause a deadlock.
 *    Event thread - autodisplay: locks view hierarchy, then window
 *    X Server thread - window resize: locks window, then view hierarchy
 *  Deadlock occurs if each thread gets one lock and waits for the other.
*/
void *AquaNewWindow(void *upperw, int x, int y, int w, int h, int isRoot)
{
    AquaWindowRec *winRec = (AquaWindowRec *)malloc(sizeof(AquaWindowRec));
    NSRect frame = NSMakeRect(x, NSHeight([[NSScreen mainScreen] frame]) -
                              y - h, w, h);
    NSWindow *theWindow;
    XView *theView;

    // Create an NSWindow for the new X11 window
    theWindow = [[NSWindow alloc] initWithContentRect:frame
                                  styleMask:NSBorderlessWindowMask
                                  backing:NSBackingStoreBuffered
                                  defer:YES];
    if (!theWindow) return NULL;

    [theWindow setBackgroundColor:[NSColor clearColor]];  // erase transparent
    [theWindow setAlphaValue:1.0];       // draw opaque
    [theWindow setOpaque:YES];           // changed when window is shaped

    [theWindow useOptimizedDrawing:YES]; // Has no overlapping sub-views
    [theWindow setAutodisplay:NO];       // See comment above
    [theWindow disableFlushWindow];      // We do all the flushing manually
    [theWindow setHasShadow:!isRoot];    // All windows have shadows except root
    [theWindow setReleasedWhenClosed:YES]; // Default, but we want to be sure

    theView = [[XView alloc] initWithFrame:frame];
    [theWindow setContentView:theView];
    [theWindow setInitialFirstResponder:theView];

    if (upperw) {
        AquaWindowRec *upperRec = AQUA_WINREC(upperw);
        int uppernum = [upperRec->window windowNumber];
        [theWindow orderWindow:NSWindowBelow relativeTo:uppernum];
    } else {
        if (!isRoot) {
            [theWindow orderFront:nil];
            winRec->port = NULL;
        }
    }

    [theWindow setAcceptsMouseMovedEvents:YES];

    winRec->window = theWindow;
    winRec->view = theView;

    if (!isRoot) {
        winRec->rootGWorld = NULL;
        [theView lockFocus];
        // Fill the window with white to make sure alpha channel is set
        NSEraseRect(frame);
        winRec->port = [theView qdPort];
        winRec->context = [[NSGraphicsContext currentContext] graphicsPort];
        // CreateCGContextForPort(winRec->port, &winRec->context);
        [theView unlockFocus];
    } else {
        // Allocate the offscreen graphics world for root window drawing
        GWorldPtr rootGWorld;
        Rect globalRect;

        SetRect(&globalRect, x, y, x+w, y+h);
        if (NewGWorld(&rootGWorld, 0, &globalRect, NULL, NULL, 0))
            return NULL;
        winRec->rootGWorld = rootGWorld;
    }

    return winRec;
}


void AquaDestroyWindow(void *rw)
{
    AquaWindowRec *winRec = AQUA_WINREC(rw);

    [winRec->window orderOut:nil];
    [winRec->window close];
    [winRec->view release];
    if (winRec->rootGWorld) {
        DisposeGWorld(winRec->rootGWorld);
    }
    free(rw);
}


void AquaMoveWindow(void *rw, int x, int y)
{
    AquaWindowRec *winRec = AQUA_WINREC(rw);
    NSPoint topLeft = NSMakePoint(x, NSHeight([[NSScreen mainScreen] frame]) -
                                  y);

    [winRec->window setFrameTopLeftPoint:topLeft];
}


/*
 * AquaStartResizeWindow
 *  Resize the on screen window.
 */
void AquaStartResizeWindow(void *rw, int x, int y, int w, int h)
{
    AquaWindowRec *winRec = AQUA_WINREC(rw);
    NSRect frame = NSMakeRect(x, NSHeight([[NSScreen mainScreen] frame]) -
                              y - h, w, h);

    [winRec->window setFrame:frame display:NO];
}


void AquaFinishResizeWindow(void *rw, int x, int y, int w, int h)
{
    // refresh everything? fixme yes for testing
    fakeBoxRec box = {0, 0, w, h};
    AquaUpdateRects(rw, &box, 1);
}


/*
 * AquaUpdateRects
 *  Flush rectangular regions from a window's backing buffer
 *  (or PixMap for the root window) to the screen.
 */
void AquaUpdateRects(void *rw, fakeBoxRec *fakeRects, int count)
{
    AquaWindowRec *winRec = AQUA_WINREC(rw);
    fakeBoxRec *rects, *end;
    static RgnHandle rgn = NULL;
    static RgnHandle box = NULL;

    if (!rgn) rgn = NewRgn();
    if (!box) box = NewRgn();

    if (winRec->rootGWorld) {
        // FIXME: Draw from the root PixMap to the normally
        // invisible root window.
    } else {
        for (rects = fakeRects, end = fakeRects+count; rects < end; rects++) {
            Rect qdrect;
            qdrect.left = rects->x1;
            qdrect.top = rects->y1;
            qdrect.right = rects->x2;
            qdrect.bottom = rects->y2;

            RectRgn(box, &qdrect);
            UnionRgn(rgn, box, rgn);
        }

        QDFlushPortBuffer(winRec->port, rgn);
    }

    SetEmptyRgn(rgn);
    SetEmptyRgn(box);
}


/*
 * AquaRestackWindow
 *  Change the window order. Put the window behind upperw or on top if
 *  upperw is NULL.
 */
void AquaRestackWindow(void *rw, void *upperw)
{
    AquaWindowRec *winRec = AQUA_WINREC(rw);

    if (upperw) {
        AquaWindowRec *upperRec = AQUA_WINREC(upperw);
        int uppernum = [upperRec->window windowNumber];
        [winRec->window orderWindow:NSWindowBelow relativeTo:uppernum];
    } else {
        [winRec->window makeKeyAndOrderFront:nil];
    }
}


/*
 * AquaReshapeWindow
 *  Set the shape of a window. The rectangles are the areas that are
 *  not part of the new shape.
 */
void AquaReshapeWindow(void *rw, fakeBoxRec *fakeRects, int count)
{
    AquaWindowRec *winRec = AQUA_WINREC(rw);
    NSRect frame = [winRec->view frame];
    int winHeight = NSHeight(frame);

    [winRec->view lockFocus];

    // If window is currently shaped we need to undo the previous shape
    if (![winRec->window isOpaque]) {
        [[NSColor whiteColor] set];
        NSRectFillUsingOperation(frame, NSCompositeDestinationAtop);
    }

    if (count > 0) {
        fakeBoxRec *rects, *end;

        // Make transparent if window is now shaped.
        [winRec->window setOpaque:NO];

        // Clear the areas outside the window shape
        [[NSColor clearColor] set];
        for (rects = fakeRects, end = fakeRects+count; rects < end; rects++) {
            int rectHeight = rects->y2 - rects->y1;
            NSRectFill( NSMakeRect(rects->x1,
                                   winHeight - rects->y1 - rectHeight,
                                   rects->x2 - rects->x1, rectHeight) );
        }
        [[NSGraphicsContext currentContext] flushGraphics];

        // force update of window shadow
        [winRec->window setHasShadow:NO];
        [winRec->window setHasShadow:YES];

    } else {
        fakeBoxRec bounds = {0, 0, NSWidth(frame), winHeight};

        [winRec->window setOpaque:YES];
        AquaUpdateRects(rw, &bounds, 1);
    }

    [winRec->view unlockFocus];
}


/* AquaStartDrawing
 *  When a window's buffer is not being drawn to, the CoreGraphics
 *  window server may compress or move it. Call this routine
 *  to lock down the buffer during direct drawing. It returns
 *  a pointer to the backing buffer and its depth, etc.
 */
void AquaStartDrawing(void *rw, char **bits,
                      int *rowBytes, int *depth, int *bpp)
{
    AquaWindowRec *winRec = AQUA_WINREC(rw);
    PixMapHandle pix;

    if (! winRec->rootGWorld) {
        [winRec->view lockFocus];
        winRec->port = [winRec->view qdPort];
        LockPortBits(winRec->port);
        [winRec->view unlockFocus];
        pix = GetPortPixMap(winRec->port);
    } else {
        pix = GetGWorldPixMap(winRec->rootGWorld);
        LockPixels(pix);
    }

    *bits = GetPixBaseAddr(pix);
    *rowBytes = GetPixRowBytes(pix) & 0x3fff; // fixme is mask needed?
    *depth = (**pix).cmpCount * (**pix).cmpSize; // fixme use GetPixDepth(pix)?
    *bpp = (**pix).pixelSize;
}


/*
 * AquaStopDrawing
 *  When direct access to a window's buffer is no longer needed, this
 *  routine should be called to allow CoreGraphics to compress or
 *  move it.
 */
void AquaStopDrawing(void *rw)
{
    AquaWindowRec *winRec = AQUA_WINREC(rw);

    if (! winRec->rootGWorld) {
        UnlockPortBits(winRec->port);
    } else {
        PixMapHandle pix = GetGWorldPixMap(winRec->rootGWorld);
        UnlockPixels(pix);
    }
}