#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif
#include "win.h"
extern const GUID _IID_IDirectDraw2;
extern HWND g_hDlgExit;
static Bool
winAllocateFBPrimaryDD (ScreenPtr pScreen);
static Bool
winCloseScreenPrimaryDD (int nIndex, ScreenPtr pScreen);
static Bool
winInitVisualsPrimaryDD (ScreenPtr pScreen);
static Bool
winAdjustVideoModePrimaryDD (ScreenPtr pScreen);
static Bool
winActivateAppPrimaryDD (ScreenPtr pScreen);
static Bool
winHotKeyAltTabPrimaryDD (ScreenPtr pScreen);
static Bool
winAllocateFBPrimaryDD (ScreenPtr pScreen)
{
winScreenPriv(pScreen);
winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
HRESULT ddrval = DD_OK;
DDSURFACEDESC ddsd;
DDSURFACEDESC *pddsdPrimary = NULL;
DDSURFACEDESC *pddsdOffscreen = NULL;
RECT rcClient;
ErrorF ("winAllocateFBPrimaryDD\n");
GetClientRect (pScreenPriv->hwndScreen, &rcClient);
MapWindowPoints (pScreenPriv->hwndScreen,
HWND_DESKTOP,
(LPPOINT)&rcClient, 2);
ddrval = (*g_fpDirectDrawCreate) (NULL, &pScreenPriv->pdd, NULL);
if (ddrval != DD_OK)
FatalError ("winAllocateFBPrimaryDD - Could not start DirectDraw\n");
ddrval = IDirectDraw_QueryInterface (pScreenPriv->pdd,
&IID_IDirectDraw2,
(LPVOID*) &pScreenPriv->pdd2);
if (FAILED (ddrval))
{
ErrorF ("winAllocateFBShadowDD - Failed DD2 query: %08x\n",
(unsigned int) ddrval);
return FALSE;
}
ErrorF ("winAllocateFBPrimaryDD - Created and initialized DD\n");
if (pScreenInfo->fFullScreen)
{
ddrval = IDirectDraw2_SetCooperativeLevel (pScreenPriv->pdd2,
pScreenPriv->hwndScreen,
DDSCL_FULLSCREEN
| DDSCL_EXCLUSIVE);
if (FAILED (ddrval))
FatalError ("winAllocateFBPrimaryDD - Could not set "
"cooperative level\n");
ddrval = IDirectDraw2_SetDisplayMode (pScreenPriv->pdd2,
pScreenInfo->dwWidth,
pScreenInfo->dwHeight,
pScreenInfo->dwBPP,
pScreenInfo->dwRefreshRate,
0);
if (FAILED (ddrval))
FatalError ("winAllocateFBPrimaryDD - Could not set "
"full screen display mode\n");
}
else
{
ddrval = IDirectDraw2_SetCooperativeLevel (pScreenPriv->pdd2,
pScreenPriv->hwndScreen,
DDSCL_NORMAL);
if (FAILED (ddrval))
FatalError ("winAllocateFBPrimaryDD - Could not set "
"cooperative level\n");
}
ZeroMemory (&ddsd, sizeof (ddsd));
ddsd.dwSize = sizeof (ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
ddrval = IDirectDraw2_CreateSurface (pScreenPriv->pdd2,
&ddsd,
&pScreenPriv->pddsPrimary,
NULL);
if (FAILED (ddrval))
FatalError ("winAllocateFBPrimaryDD - Could not create primary "
"surface %08x\n", (unsigned int) ddrval);
ErrorF ("winAllocateFBPrimaryDD - Created primary\n");
pddsdPrimary = pScreenPriv->pddsdPrimary
= malloc (sizeof (DDSURFACEDESC));
if (pddsdPrimary == NULL)
FatalError ("winAllocateFBPrimaryDD - Could not allocate surface "
"description memory\n");
ZeroMemory (pddsdPrimary, sizeof (*pddsdPrimary));
pddsdPrimary->dwSize = sizeof (*pddsdPrimary);
ZeroMemory (&ddsd, sizeof (ddsd));
ddsd.dwSize = sizeof (ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
ddsd.dwHeight = pScreenInfo->dwHeight;
ddsd.dwWidth = pScreenInfo->dwWidth;
ddrval = IDirectDraw2_CreateSurface (pScreenPriv->pdd2,
&ddsd,
&pScreenPriv->pddsOffscreen,
NULL);
if (ddrval != DD_OK)
FatalError ("winAllocateFBPrimaryDD - Could not create shadow "
"surface\n");
ErrorF ("winAllocateFBPrimaryDD - Created offscreen\n");
pddsdOffscreen = pScreenPriv->pddsdOffscreen
= malloc (sizeof (DDSURFACEDESC));
if (pddsdOffscreen == NULL)
FatalError ("winAllocateFBPrimaryDD - Could not allocate surface "
"description memory\n");
ZeroMemory (pddsdOffscreen, sizeof (*pddsdOffscreen));
pddsdOffscreen->dwSize = sizeof (*pddsdOffscreen);
ErrorF ("winAllocateFBPrimaryDD - Locking primary\n");
ddrval = IDirectDrawSurface2_Lock (pScreenPriv->pddsPrimary,
pScreenInfo->fFullScreen ? NULL:&rcClient,
pddsdPrimary,
DDLOCK_WAIT,
NULL);
if (ddrval != DD_OK || pddsdPrimary->lpSurface == NULL)
FatalError ("winAllocateFBPrimaryDD - Could not lock "
"primary surface\n");
ErrorF ("winAllocateFBPrimaryDD - Locked primary\n");
if (!(pddsdPrimary->ddpfPixelFormat.dwFlags & DDPF_RGB))
FatalError ("winAllocateFBPrimaryDD - Color format other than RGB\n");
pScreenInfo->dwStride = (pddsdPrimary->u1.lPitch * 8)
/ pScreenInfo->dwBPP;
pScreenInfo->pfb = pddsdPrimary->lpSurface;
pScreenPriv->dwRedMask = pddsdPrimary->ddpfPixelFormat.u2.dwRBitMask;
pScreenPriv->dwGreenMask = pddsdPrimary->ddpfPixelFormat.u3.dwGBitMask;
pScreenPriv->dwBlueMask = pddsdPrimary->ddpfPixelFormat.u4.dwBBitMask;
ErrorF ("winAllocateFBPrimaryDD - Returning\n");
return TRUE;
}
static Bool
winCloseScreenPrimaryDD (int nIndex, ScreenPtr pScreen)
{
winScreenPriv(pScreen);
winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
Bool fReturn;
ErrorF ("winCloseScreenPrimaryDD - Freeing screen resources\n");
pScreenPriv->fClosed = TRUE;
pScreenPriv->fActive = FALSE;
WIN_UNWRAP(CloseScreen);
fReturn = (*pScreen->CloseScreen) (nIndex, pScreen);
RemoveProp (pScreenPriv->hwndScreen, WIN_SCR_PROP);
if (pScreenPriv->pddsOffscreen)
{
IDirectDrawSurface2_Unlock (pScreenPriv->pddsOffscreen, NULL);
IDirectDrawSurface2_Release (pScreenPriv->pddsOffscreen);
pScreenPriv->pddsOffscreen = NULL;
}
if (pScreenPriv->pddsPrimary)
{
IDirectDrawSurface2_Unlock (pScreenPriv->pddsPrimary, NULL);
IDirectDrawSurface2_Release (pScreenPriv->pddsPrimary);
pScreenPriv->pddsPrimary = NULL;
}
if (pScreenPriv->pdd)
{
IDirectDraw2_RestoreDisplayMode (pScreenPriv->pdd);
IDirectDraw2_Release (pScreenPriv->pdd);
pScreenPriv->pdd = NULL;
}
if (!pScreenInfo->fNoTrayIcon)
winDeleteNotifyIcon (pScreenPriv);
if (g_hDlgExit != NULL)
{
DestroyWindow (g_hDlgExit);
g_hDlgExit = NULL;
}
if (pScreenPriv->hwndScreen)
{
DestroyWindow (pScreenPriv->hwndScreen);
pScreenPriv->hwndScreen = NULL;
}
pScreenInfo->pScreen = NULL;
pScreenInfo->pfb = NULL;
free ((pointer) pScreenPriv);
return fReturn;
}
static Bool
winInitVisualsPrimaryDD (ScreenPtr pScreen)
{
winScreenPriv(pScreen);
winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
DWORD dwRedBits, dwGreenBits, dwBlueBits;
dwRedBits = winCountBits (pScreenPriv->dwRedMask);
dwGreenBits = winCountBits (pScreenPriv->dwGreenMask);
dwBlueBits = winCountBits (pScreenPriv->dwBlueMask);
if (dwRedBits > dwGreenBits && dwRedBits > dwBlueBits)
pScreenPriv->dwBitsPerRGB = dwRedBits;
else if (dwGreenBits > dwRedBits && dwGreenBits > dwBlueBits)
pScreenPriv->dwBitsPerRGB = dwGreenBits;
else
pScreenPriv->dwBitsPerRGB = dwBlueBits;
ErrorF ("winInitVisualsPrimaryDD - Masks: %08x %08x %08x bpRGB: %d\n",
(unsigned int) pScreenPriv->dwRedMask,
(unsigned int) pScreenPriv->dwGreenMask,
(unsigned int) pScreenPriv->dwBlueMask,
(int) pScreenPriv->dwBitsPerRGB);
switch (pScreenInfo->dwDepth)
{
case 24:
case 16:
case 15:
if (!miSetVisualTypesAndMasks (pScreenInfo->dwDepth,
TrueColorMask,
pScreenPriv->dwBitsPerRGB,
TrueColor,
pScreenPriv->dwRedMask,
pScreenPriv->dwGreenMask,
pScreenPriv->dwBlueMask))
{
ErrorF ("winInitVisualsPrimaryDD - "
"miSetVisualTypesAndMasks failed\n");
return FALSE;
}
break;
case 8:
#if CYGDEBUG
winDebug ("winInitVisuals - Calling miSetVisualTypesAndMasks\n");
#endif
if (!miSetVisualTypesAndMasks (pScreenInfo->dwDepth,
PseudoColorMask,
pScreenPriv->dwBitsPerRGB,
PseudoColor,
pScreenPriv->dwRedMask,
pScreenPriv->dwGreenMask,
pScreenPriv->dwBlueMask))
{
ErrorF ("winInitVisualsPrimaryDD - "
"miSetVisualTypesAndMasks failed\n");
return FALSE;
}
#if CYGDEBUG
winDebug ("winInitVisualsPrimaryDD - Returned from "
"miSetVisualTypesAndMasks\n");
#endif
break;
default:
ErrorF ("winInitVisualsPrimaryDD - Unknown screen depth\n");
return FALSE;
}
ErrorF ("winInitVisualsPrimaryDD - Returning\n");
return TRUE;
}
static Bool
winAdjustVideoModePrimaryDD (ScreenPtr pScreen)
{
winScreenPriv(pScreen);
winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
HDC hdc = NULL;
DWORD dwBPP;
hdc = GetDC (NULL);
if (hdc == NULL)
{
ErrorF ("winAdjustVideoModePrimaryDD - GetDC failed\n");
return FALSE;
}
dwBPP = GetDeviceCaps (hdc, BITSPIXEL);
if (pScreenInfo->dwBPP == WIN_DEFAULT_BPP)
{
ErrorF ("winAdjustVideoModePrimaryDD - Using Windows display "
"depth of %d bits per pixel\n", (int) dwBPP);
pScreenInfo->dwBPP = dwBPP;
}
else if (pScreenInfo->fFullScreen
&& pScreenInfo->dwBPP != dwBPP)
{
ErrorF ("winAdjustVideoModePrimaryDD - FullScreen, using command "
"line depth: %d\n", (int) pScreenInfo->dwBPP);
}
else if (dwBPP != pScreenInfo->dwBPP)
{
ErrorF ("winAdjustVideoModePrimaryDD - Windowed, command line "
"depth: %d, using depth: %d\n",
(int) pScreenInfo->dwBPP, (int) dwBPP);
pScreenInfo->dwBPP = dwBPP;
}
ReleaseDC (NULL, hdc);
return TRUE;
}
static Bool
winActivateAppPrimaryDD (ScreenPtr pScreen)
{
winScreenPriv(pScreen);
winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
RECT rcSrc, rcClient;
HRESULT ddrval = DD_OK;
if (pScreenPriv == NULL
|| pScreenPriv->pddsPrimary == NULL
|| pScreenPriv->pddsOffscreen == NULL)
return FALSE;
if (!pScreenPriv->fActive)
return TRUE;
ddrval = IDirectDrawSurface2_IsLost (pScreenPriv->pddsOffscreen);
if (ddrval == DD_OK)
{
IDirectDrawSurface2_Unlock (pScreenPriv->pddsOffscreen,
NULL);
}
IDirectDrawSurface2_Restore (pScreenPriv->pddsOffscreen);
IDirectDrawSurface2_Restore (pScreenPriv->pddsPrimary);
GetClientRect (pScreenPriv->hwndScreen, &rcClient);
MapWindowPoints (pScreenPriv->hwndScreen,
HWND_DESKTOP,
(LPPOINT)&rcClient, 2);
rcSrc.left = 0;
rcSrc.top = 0;
rcSrc.right = pScreenInfo->dwWidth;
rcSrc.bottom = pScreenInfo->dwHeight;
ddrval = IDirectDrawSurface2_Blt (pScreenPriv->pddsPrimary,
&rcClient,
pScreenPriv->pddsOffscreen,
&rcSrc,
DDBLT_WAIT,
NULL);
if (ddrval != DD_OK)
FatalError ("winActivateAppPrimaryDD () - Failed blitting offscreen "
"surface to primary surface %08x\n", (unsigned int) ddrval);
ddrval = IDirectDrawSurface2_Lock (pScreenPriv->pddsPrimary,
&rcClient,
pScreenPriv->pddsdPrimary,
DDLOCK_WAIT,
NULL);
if (ddrval != DD_OK
|| pScreenPriv->pddsdPrimary->lpSurface == NULL)
FatalError ("winActivateAppPrimaryDD () - Could not lock "
"primary surface\n");
winUpdateFBPointer (pScreen,
pScreenPriv->pddsdPrimary->lpSurface);
RegisterHotKey (pScreenPriv->hwndScreen, 1, MOD_ALT, 9);
return TRUE;
}
static Bool
winHotKeyAltTabPrimaryDD (ScreenPtr pScreen)
{
winScreenPriv(pScreen);
winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
RECT rcClient, rcSrc;
HRESULT ddrval = DD_OK;
ErrorF ("\nwinHotKeyAltTabPrimaryDD\n\n");
pScreenPriv->fActive = FALSE;
if (pScreenPriv->pddsPrimary == NULL
|| pScreenPriv->pddsOffscreen == NULL)
return FALSE;
GetClientRect (pScreenPriv->hwndScreen, &rcClient);
MapWindowPoints (pScreenPriv->hwndScreen,
HWND_DESKTOP,
(LPPOINT)&rcClient, 2);
ddrval = IDirectDrawSurface2_IsLost (pScreenPriv->pddsPrimary);
if (ddrval == DD_OK)
{
ddrval = IDirectDrawSurface2_Unlock (pScreenPriv->pddsPrimary,
NULL);
if (FAILED (ddrval))
FatalError ("winHotKeyAltTabPrimaryDD - Failed unlocking primary "
"surface\n");
}
rcSrc.left = 0;
rcSrc.top = 0;
rcSrc.right = pScreenInfo->dwWidth;
rcSrc.bottom = pScreenInfo->dwHeight;
ddrval = IDirectDrawSurface2_Blt (pScreenPriv->pddsOffscreen,
NULL,
pScreenPriv->pddsPrimary,
NULL,
DDBLT_WAIT,
NULL);
if (ddrval == DDERR_SURFACELOST)
{
IDirectDrawSurface2_Restore (pScreenPriv->pddsOffscreen);
IDirectDrawSurface2_Restore (pScreenPriv->pddsPrimary);
ddrval = IDirectDrawSurface2_Blt (pScreenPriv->pddsOffscreen,
NULL,
pScreenPriv->pddsPrimary,
NULL,
DDBLT_WAIT,
NULL);
if (FAILED (ddrval))
FatalError ("winHotKeyAltTabPrimaryDD - Failed blitting primary "
"surface to offscreen surface: %08x\n",
(unsigned int) ddrval);
}
else
{
FatalError ("winHotKeyAltTabPrimaryDD - Unknown error from "
"Blt: %08dx\n", (unsigned int) ddrval);
}
ddrval = IDirectDrawSurface2_Lock (pScreenPriv->pddsOffscreen,
NULL,
pScreenPriv->pddsdOffscreen,
DDLOCK_WAIT,
NULL);
if (ddrval != DD_OK
|| pScreenPriv->pddsdPrimary->lpSurface == NULL)
FatalError ("winHotKeyAltTabPrimaryDD - Could not lock "
"offscreen surface\n");
winUpdateFBPointer (pScreen,
pScreenPriv->pddsdOffscreen->lpSurface);
UnregisterHotKey (pScreenPriv->hwndScreen, 1);
return TRUE;
}
Bool
winSetEngineFunctionsPrimaryDD (ScreenPtr pScreen)
{
winScreenPriv(pScreen);
winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
pScreenPriv->pwinAllocateFB = winAllocateFBPrimaryDD;
pScreenPriv->pwinShadowUpdate
= (winShadowUpdateProcPtr) (void (*)(void))NoopDDA;
pScreenPriv->pwinCloseScreen = winCloseScreenPrimaryDD;
pScreenPriv->pwinInitVisuals = winInitVisualsPrimaryDD;
pScreenPriv->pwinAdjustVideoMode = winAdjustVideoModePrimaryDD;
if (pScreenInfo->fFullScreen)
pScreenPriv->pwinCreateBoundingWindow = winCreateBoundingWindowFullScreen;
else
pScreenPriv->pwinCreateBoundingWindow = winCreateBoundingWindowWindowed;
pScreenPriv->pwinFinishScreenInit = winFinishScreenInitFB;
pScreenPriv->pwinBltExposedRegions
= (winBltExposedRegionsProcPtr) (void (*)(void))NoopDDA;
pScreenPriv->pwinActivateApp = winActivateAppPrimaryDD;
pScreenPriv->pwinHotKeyAltTab = winHotKeyAltTabPrimaryDD;
#ifdef XWIN_MULTIWINDOW
pScreenPriv->pwinFinishCreateWindowsWindow =
(winFinishCreateWindowsWindowProcPtr) (void (*)(void))NoopDDA;
#endif
return TRUE;
}