#include "quartzCommon.h"
#include "darwin.h"
#include "quartz.h"
#include "quartzCursor.h"
#include "colormapst.h"
#include "scrnintstr.h"
#include "micmap.h"
#include "shadow.h"
typedef struct {
CGDirectDisplayID displayID;
CFDictionaryRef xDisplayMode;
CFDictionaryRef aquaDisplayMode;
CGDirectPaletteRef xPalette;
CGDirectPaletteRef aquaPalette;
unsigned char *framebuffer;
unsigned char *shadowPtr;
} FSScreenRec, *FSScreenPtr;
#define FULLSCREEN_PRIV(pScreen) \
((FSScreenPtr)pScreen->devPrivates[fsScreenIndex].ptr)
static int fsScreenIndex;
static CGDirectDisplayID *quartzDisplayList = NULL;
static int quartzNumScreens = 0;
static FSScreenPtr quartzScreens[MAXSCREENS];
static int darwinCmapPrivateIndex = -1;
static unsigned long darwinCmapGeneration = 0;
#define CMAP_PRIV(pCmap) \
((CGDirectPaletteRef) (pCmap)->devPrivates[darwinCmapPrivateIndex].ptr)
static Bool
FSInitCmapPrivates(
ColormapPtr pCmap)
{
return TRUE;
}
static Bool
FSCreateColormap(
ColormapPtr pCmap)
{
CGDirectPaletteRef pallete;
if (darwinCmapGeneration != serverGeneration) {
if ((darwinCmapPrivateIndex =
AllocateColormapPrivateIndex(FSInitCmapPrivates)) < 0)
{
return FALSE;
}
darwinCmapGeneration = serverGeneration;
}
pallete = CGPaletteCreateDefaultColorPalette();
if (!pallete) return FALSE;
CMAP_PRIV(pCmap) = pallete;
return TRUE;
}
static void
FSDestroyColormap(
ColormapPtr pCmap)
{
CGPaletteRelease( CMAP_PRIV(pCmap) );
}
static void
FSInstallColormap(
ColormapPtr pCmap)
{
CGDirectPaletteRef palette = CMAP_PRIV(pCmap);
ScreenPtr pScreen = pCmap->pScreen;
FSScreenPtr fsDisplayInfo = FULLSCREEN_PRIV(pScreen);
miInstallColormap(pCmap);
if (quartzServerVisible)
CGDisplaySetPalette(fsDisplayInfo->displayID, palette);
fsDisplayInfo->xPalette = palette;
}
static void
FSStoreColors(
ColormapPtr pCmap,
int numEntries,
xColorItem *pdefs)
{
CGDirectPaletteRef palette = CMAP_PRIV(pCmap);
ScreenPtr pScreen = pCmap->pScreen;
FSScreenPtr fsDisplayInfo = FULLSCREEN_PRIV(pScreen);
CGDeviceColor color;
int i;
if (! palette)
return;
for (i = 0; i < numEntries; i++) {
color.red = pdefs[i].red / 65535.0;
color.green = pdefs[i].green / 65535.0;
color.blue = pdefs[i].blue / 65535.0;
CGPaletteSetColorAtIndex(palette, color, pdefs[i].pixel);
}
if (quartzServerVisible)
CGDisplaySetPalette(fsDisplayInfo->displayID, palette);
}
static void FSCapture(void)
{
int i;
if (quartzRootless) return;
for (i = 0; i < quartzNumScreens; i++) {
FSScreenPtr fsDisplayInfo = quartzScreens[i];
CGDirectDisplayID cgID = fsDisplayInfo->displayID;
if (!CGDisplayIsCaptured(cgID)) {
CGDisplayCapture(cgID);
fsDisplayInfo->aquaDisplayMode = CGDisplayCurrentMode(cgID);
if (fsDisplayInfo->xDisplayMode != fsDisplayInfo->aquaDisplayMode)
CGDisplaySwitchToMode(cgID, fsDisplayInfo->xDisplayMode);
if (fsDisplayInfo->xPalette)
CGDisplaySetPalette(cgID, fsDisplayInfo->xPalette);
}
}
}
static void FSRelease(void)
{
int i;
if (quartzRootless) return;
for (i = 0; i < quartzNumScreens; i++) {
FSScreenPtr fsDisplayInfo = quartzScreens[i];
CGDirectDisplayID cgID = fsDisplayInfo->displayID;
if (CGDisplayIsCaptured(cgID)) {
if (fsDisplayInfo->xDisplayMode != fsDisplayInfo->aquaDisplayMode)
CGDisplaySwitchToMode(cgID, fsDisplayInfo->aquaDisplayMode);
if (fsDisplayInfo->aquaPalette)
CGDisplaySetPalette(cgID, fsDisplayInfo->aquaPalette);
CGDisplayRelease(cgID);
}
}
}
static void FSSuspendScreen(
ScreenPtr pScreen)
{
QuartzSuspendXCursor(pScreen);
xf86SetRootClip(pScreen, FALSE);
}
static void FSResumeScreen(
ScreenPtr pScreen,
int x, int y )
{
QuartzResumeXCursor(pScreen, x, y);
xf86SetRootClip(pScreen, TRUE);
}
static void FSDisplayInit(void)
{
static unsigned long generation = 0;
CGDisplayCount quartzDisplayCount = 0;
ErrorF("Display mode: Full screen Quartz -- Direct Display\n");
if (generation != serverGeneration) {
fsScreenIndex = AllocateScreenPrivateIndex();
generation = serverGeneration;
}
CGGetActiveDisplayList(0, NULL, &quartzDisplayCount);
quartzDisplayList = xalloc(quartzDisplayCount * sizeof(CGDirectDisplayID));
CGGetActiveDisplayList(quartzDisplayCount, quartzDisplayList,
&quartzDisplayCount);
darwinScreensFound = quartzDisplayCount;
atexit(FSRelease);
}
static Bool FSFindDisplayMode(
FSScreenPtr fsDisplayInfo)
{
CGDirectDisplayID cgID = fsDisplayInfo->displayID;
size_t height, width, bpp;
boolean_t exactMatch;
fsDisplayInfo->aquaDisplayMode = CGDisplayCurrentMode(cgID);
if (darwinDesiredWidth == 0 && darwinDesiredDepth == -1 &&
darwinDesiredRefresh == -1)
{
fsDisplayInfo->xDisplayMode = fsDisplayInfo->aquaDisplayMode;
return TRUE;
}
if (darwinDesiredWidth == 0) {
width = CGDisplayPixelsWide(cgID);
height = CGDisplayPixelsHigh(cgID);
} else {
width = darwinDesiredWidth;
height = darwinDesiredHeight;
}
switch (darwinDesiredDepth) {
case 0:
bpp = 8;
break;
case 1:
bpp = 16;
break;
case 2:
bpp = 32;
break;
default:
bpp = CGDisplayBitsPerPixel(cgID);
}
if (darwinDesiredRefresh == -1) {
fsDisplayInfo->xDisplayMode =
CGDisplayBestModeForParameters(cgID, bpp, width, height,
&exactMatch);
} else {
fsDisplayInfo->xDisplayMode =
CGDisplayBestModeForParametersAndRefreshRate(cgID, bpp,
width, height, darwinDesiredRefresh, &exactMatch);
}
if (!exactMatch) {
fsDisplayInfo->xDisplayMode = fsDisplayInfo->aquaDisplayMode;
return FALSE;
}
CGDisplaySwitchToMode(cgID, fsDisplayInfo->xDisplayMode);
return TRUE;
}
static Bool FSAddScreen(
int index,
ScreenPtr pScreen)
{
DarwinFramebufferPtr dfb = SCREEN_PRIV(pScreen);
QuartzScreenPtr displayInfo = QUARTZ_PRIV(pScreen);
CGDirectDisplayID cgID = quartzDisplayList[index];
CGRect bounds;
FSScreenPtr fsDisplayInfo;
fsDisplayInfo = xalloc(sizeof(FSScreenRec));
FULLSCREEN_PRIV(pScreen) = fsDisplayInfo;
displayInfo->displayCount = 1;
displayInfo->displayIDs = xrealloc(displayInfo->displayIDs,
1 * sizeof(CGDirectDisplayID));
displayInfo->displayIDs[0] = cgID;
fsDisplayInfo->displayID = cgID;
fsDisplayInfo->xDisplayMode = 0;
fsDisplayInfo->aquaDisplayMode = 0;
fsDisplayInfo->xPalette = 0;
fsDisplayInfo->aquaPalette = 0;
CGDisplayCapture(cgID);
if (! FSFindDisplayMode(fsDisplayInfo)) {
ErrorF("Could not support specified display mode on screen %i.\n",
index);
xfree(fsDisplayInfo);
return FALSE;
}
bounds = CGDisplayBounds(cgID);
dfb->x = bounds.origin.x;
dfb->y = bounds.origin.y;
dfb->width = bounds.size.width;
dfb->height = bounds.size.height;
dfb->pitch = CGDisplayBytesPerRow(cgID);
dfb->bitsPerPixel = CGDisplayBitsPerPixel(cgID);
if (dfb->bitsPerPixel == 8) {
if (CGDisplayCanSetPalette(cgID)) {
dfb->colorType = PseudoColor;
} else {
dfb->colorType = StaticColor;
}
dfb->bitsPerComponent = 8;
dfb->colorBitsPerPixel = 8;
} else {
dfb->colorType = TrueColor;
dfb->bitsPerComponent = CGDisplayBitsPerSample(cgID);
dfb->colorBitsPerPixel = CGDisplaySamplesPerPixel(cgID) *
dfb->bitsPerComponent;
}
fsDisplayInfo->framebuffer = CGDisplayBaseAddress(cgID);
fsDisplayInfo->shadowPtr = xalloc(dfb->pitch * dfb->height);
dfb->framebuffer = fsDisplayInfo->shadowPtr;
return TRUE;
}
static void FSShadowUpdate(
ScreenPtr pScreen,
shadowBufPtr pBuf)
{
DarwinFramebufferPtr dfb = SCREEN_PRIV(pScreen);
FSScreenPtr fsDisplayInfo = FULLSCREEN_PRIV(pScreen);
RegionPtr damage = &pBuf->damage;
int numBox = REGION_NUM_RECTS(damage);
BoxPtr pBox = REGION_RECTS(damage);
int pitch = dfb->pitch;
int bpp = dfb->bitsPerPixel/8;
if (!quartzServerVisible)
return;
while (numBox--) {
int width, height, offset;
unsigned char *src, *dst;
width = (pBox->x2 - pBox->x1) * bpp;
height = pBox->y2 - pBox->y1;
offset = (pBox->y1 * pitch) + (pBox->x1 * bpp);
src = fsDisplayInfo->shadowPtr + offset;
dst = fsDisplayInfo->framebuffer + offset;
while (height--) {
memcpy(dst, src, width);
dst += pitch;
src += pitch;
}
pBox++;
}
}
static Bool FSSetupScreen(
int index,
ScreenPtr pScreen)
{
DarwinFramebufferPtr dfb = SCREEN_PRIV(pScreen);
FSScreenPtr fsDisplayInfo = FULLSCREEN_PRIV(pScreen);
CGDirectDisplayID cgID = fsDisplayInfo->displayID;
if (! shadowInit(pScreen, FSShadowUpdate, NULL)) {
ErrorF("Failed to initalize shadow framebuffer for screen %i.\n",
index);
return FALSE;
}
if (dfb->colorType == PseudoColor) {
size_t aquaBpp;
CFNumberGetValue(CFDictionaryGetValue(fsDisplayInfo->aquaDisplayMode,
kCGDisplayBitsPerPixel), kCFNumberLongType, &aquaBpp);
if (aquaBpp <= 8)
fsDisplayInfo->aquaPalette = CGPaletteCreateWithDisplay(cgID);
pScreen->CreateColormap = FSCreateColormap;
pScreen->DestroyColormap = FSDestroyColormap;
pScreen->InstallColormap = FSInstallColormap;
pScreen->StoreColors = FSStoreColors;
}
quartzScreens[quartzNumScreens++] = fsDisplayInfo;
return TRUE;
}
static QuartzModeProcsRec fsModeProcs = {
FSDisplayInit,
FSAddScreen,
FSSetupScreen,
NULL, QuartzInitCursor,
QuartzReallySetCursor,
FSSuspendScreen,
FSResumeScreen,
FSCapture,
FSRelease,
NULL, NULL,
NULL,
NULL,
NULL, NULL
};
Bool
QuartzModeBundleInit(void)
{
quartzProcs = &fsModeProcs;
quartzOpenGLBundle = NULL; return TRUE;
}