summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/darwin/bundle/quartz.c
blob: 0809372880ae13ab0afb755b8c5fbd0ccbcee595 (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
/**************************************************************
 *
 * Quartz-specific support for the Darwin X Server
 *
 * By Gregory Robert Parker
 *
 **************************************************************/
/* $XFree86: xc/programs/Xserver/hw/darwin/bundle/quartz.c,v 1.7 2001/04/16 06:51:48 torrey Exp $ */

// X headers
#include "scrnintstr.h"

// System headers
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <IOKit/pwr_mgt/IOPMLib.h>

// We need CoreGraphics in ApplicationServices, but we leave out
// QuickDraw, which has symbol conflicts with the basic X includes.
#define __QD__
#define __PRINTCORE__
#include <ApplicationServices/ApplicationServices.h>

#include "../darwin.h"
#include "quartz.h"
#include "quartzAudio.h"
#include "quartzCursor.h"

#define kDarwinMaxScreens 100
static ScreenPtr darwinScreens[kDarwinMaxScreens];
static int darwinNumScreens = 0;
static BOOL xhidden = FALSE;


/*
 * QuartzStoreColors
 *  FIXME: need to implement if Quartz supports PsuedoColor
 */
static void QuartzStoreColors(
    ColormapPtr     pmap,
    int             numEntries,
    xColorItem      *pdefs)
{
}

/*
===========================================================================

 Screen functions

===========================================================================
*/

/*
 * QuartzPMThread
 * Handle power state notifications, FIXME
 */
#if 0
static void *QuartzPMThread(void *arg)
{
    for (;;) {
        mach_msg_return_t       kr;
        mach_msg_empty_rcv_t    msg;

        kr = mach_msg((mach_msg_header_t*) &msg, MACH_RCV_MSG, 0,
                      sizeof(msg), pmNotificationPort, 0, MACH_PORT_NULL);
        kern_assert(kr);

        // computer just woke up
        if (msg.header.msgh_id == 1) {
            if (!xhidden) {
                int i;

                for (i = 0; i < darwinNumScreens; i++) {
                    if (darwinScreens[i]) 
                        xf86SetRootClip(darwinScreens[i], true);
                }
            }
        }
    }
    return NULL;
}
#endif


/* 
 * QuartzAddScreen
 *  Quartz keeps a list of all screens for QuartzShow and QuartzHide.
 *  FIXME: So does ddx, use that instead.
 */
Bool QuartzAddScreen(ScreenPtr pScreen) 
{
    if (darwinNumScreens == kDarwinMaxScreens) {
        return FALSE;
    }

    darwinScreens[darwinNumScreens++] = pScreen;

    // setup cursor support
    if (! QuartzInitCursor(pScreen)) {
        return FALSE;
    }

    // initialize colormap handling as needed
    if (dfb.pixelInfo.pixelType == kIOCLUTPixels) {
        pScreen->StoreColors = QuartzStoreColors;
    }

    return TRUE;
}


/* 
 * QuartzCapture
 *  Capture the screen so we can draw.
 */
static void QuartzCapture(void)
{
    if (! CGDisplayIsCaptured(kCGDirectMainDisplay)) {
        CGDisplayCapture(kCGDirectMainDisplay);
    }
}


/* 
 * QuartzRelease
 *  Release the screen so others can draw.
 */
static void QuartzRelease(void)
{
    if (CGDisplayIsCaptured(kCGDirectMainDisplay)) {
        CGDisplayRelease(kCGDirectMainDisplay);
    }
}


/*
 * QuartzDisplayInit
 *  Init the framebuffer and claim the display from CoreGraphics.
 */
static void QuartzDisplayInit(void) 
{
    dfb.pixelInfo.pixelType = kIORGBDirectPixels;
    dfb.pixelInfo.bitsPerComponent=CGDisplayBitsPerSample(kCGDirectMainDisplay);
    dfb.pixelInfo.componentCount=CGDisplaySamplesPerPixel(kCGDirectMainDisplay);
#if FALSE
    // FIXME: endian and 24 bit color specific
    dfb.pixelInfo.componentMasks[0] = 0x00ff0000;
    dfb.pixelInfo.componentMasks[1] = 0x0000ff00;
    dfb.pixelInfo.componentMasks[2] = 0x000000ff;
#endif

    dfb.width  = CGDisplayPixelsWide(kCGDirectMainDisplay);
    dfb.height = CGDisplayPixelsHigh(kCGDirectMainDisplay);
    dfb.pitch = CGDisplayBytesPerRow(kCGDirectMainDisplay);
    dfb.bitsPerPixel = CGDisplayBitsPerPixel(kCGDirectMainDisplay);
    dfb.colorBitsPerPixel = (dfb.pixelInfo.componentCount * 
                            dfb.pixelInfo.bitsPerComponent);

    dfb.framebuffer = CGDisplayBaseAddress(kCGDirectMainDisplay);

    // need to capture because X doesn't like read-only framebuffer...
    QuartzCapture(); 
    atexit(QuartzRelease);
}


/*
 * QuartzOsVendorInit
 *  Quartz display initialization.
 */
void QuartzOsVendorInit(void)
{
    ErrorF("Display mode: Quartz\n");

    QuartzAudioInit();
    QuartzDisplayInit();
}


/* 
 * QuartzShow
 *  Show the X server on screen. Does nothing if already shown.
 *  Recapture the screen, restore the X clip regions, and restore
 *  the X server cursor state.
 */
void QuartzShow(
    int x,	// cursor location
    int y )
{
    int i;

    QuartzCapture();
    if (xhidden) {
        for (i = 0; i < darwinNumScreens; i++) {
            if (darwinScreens[i]) {
                xf86SetRootClip(darwinScreens[i], true);
                QuartzResumeXCursor(darwinScreens[i], x, y);
            }
        }
    }
    xhidden = FALSE;
}


/* 
 * QuartzHide
 *  Remove the X server display from the screen. Does nothing if already hidden.
 *  Release the screen, set X clip regions to prevent drawing, and restore the
 *  Aqua cursor.
 */
void QuartzHide(void)
{
    int i;

    if (!xhidden) {
        for (i = 0; i < darwinNumScreens; i++) {
            if (darwinScreens[i]) {
                QuartzSuspendXCursor(darwinScreens[i]);
                xf86SetRootClip(darwinScreens[i], false);
            }
        }
    } 
    QuartzRelease();
    xhidden = TRUE;
}


/*
 * QuartzGiveUp
 *  Cleanup before X server shutdown
 *  Release the screen
 */
void QuartzGiveUp(void)
{
    QuartzRelease();
}