/* * VGA hardware emulation * * Copyright 1998 Ove Kåven (with some help from Marcus Meissner) * */ #include #include "winbase.h" #include "wingdi.h" #include "winuser.h" #include "wincon.h" #include "miscemu.h" #include "dosexe.h" #include "vga.h" #include "ddraw.h" #include "services.h" #include "debugtools.h" DEFAULT_DEBUG_CHANNEL(ddraw); static IDirectDraw *lpddraw = NULL; static IDirectDrawSurface *lpddsurf; static IDirectDrawPalette *lpddpal; static DDSURFACEDESC sdesc; static LONG vga_polling,vga_refresh; static HANDLE poll_timer; typedef HRESULT WINAPI (*DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN); static DirectDrawCreateProc pDirectDrawCreate; static PALETTEENTRY vga_def_palette[256]={ /* red green blue */ {0x00, 0x00, 0x00}, /* 0 - Black */ {0x00, 0x00, 0x80}, /* 1 - Blue */ {0x00, 0x80, 0x00}, /* 2 - Green */ {0x00, 0x80, 0x80}, /* 3 - Cyan */ {0x80, 0x00, 0x00}, /* 4 - Red */ {0x80, 0x00, 0x80}, /* 5 - Magenta */ {0x80, 0x80, 0x00}, /* 6 - Brown */ {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */ {0x80, 0x80, 0x80}, /* 8 - Dark gray */ {0x00, 0x00, 0xFF}, /* 9 - Light blue */ {0x00, 0xFF, 0x00}, /* A - Light green */ {0x00, 0xFF, 0xFF}, /* B - Light cyan */ {0xFF, 0x00, 0x00}, /* C - Light red */ {0xFF, 0x00, 0xFF}, /* D - Light magenta */ {0xFF, 0xFF, 0x00}, /* E - Yellow */ {0xFF, 0xFF, 0xFF}, /* F - White */ {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */ }; static void VGA_DeinstallTimer(void) { if (poll_timer) { SERVICE_Delete( poll_timer ); poll_timer = 0; } } static void VGA_InstallTimer(unsigned Rate) { VGA_DeinstallTimer(); if (!poll_timer) poll_timer = SERVICE_AddTimer( Rate, VGA_Poll, 0 ); } HANDLE VGA_AlphaConsole(void) { /* this assumes that no Win32 redirection has taken place, but then again, * only 16-bit apps are likely to use this part of Wine... */ return GetStdHandle(STD_OUTPUT_HANDLE); } char*VGA_AlphaBuffer(void) { return DOSMEM_MapDosToLinear(0xb8000); } /*** GRAPHICS MODE ***/ typedef struct { unsigned Xres, Yres, Depth; int ret; } ModeSet; static void WINAPI VGA_DoSetMode(ULONG_PTR arg) { LRESULT res; HWND hwnd; ModeSet *par = (ModeSet *)arg; par->ret=1; if (lpddraw) VGA_Exit(); if (!lpddraw) { if (!pDirectDrawCreate) { HMODULE hmod = LoadLibraryA( "ddraw.dll" ); if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" ); if (!pDirectDrawCreate) { ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n"); return; } } res = pDirectDrawCreate(NULL,&lpddraw,NULL); if (!lpddraw) { ERR("DirectDraw is not available (res = %lx)\n",res); return; } hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",WS_POPUP|WS_BORDER|WS_CAPTION|WS_SYSMENU,0,0,par->Xres,par->Yres,0,0,0,NULL); if (!hwnd) { ERR("Failed to create user window.\n"); } if ((res=IDirectDraw_SetCooperativeLevel(lpddraw,hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE))) { ERR("Could not set cooperative level to exclusive (%lx)\n",res); } if ((res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth))) { ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par->Xres,par->Yres,par->Depth,res); IDirectDraw_Release(lpddraw); lpddraw=NULL; return; } res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL); if (res) { ERR("Could not create palette (res = %lx)\n",res); IDirectDraw_Release(lpddraw); lpddraw=NULL; return; } if ((res=IDirectDrawPalette_SetEntries(lpddpal,0,0,256,vga_def_palette))) { ERR("Could not set default palette entries (res = %lx)\n", res); } memset(&sdesc,0,sizeof(sdesc)); sdesc.dwSize=sizeof(sdesc); sdesc.dwFlags = DDSD_CAPS; sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) { ERR("DirectDraw surface is not available\n"); IDirectDraw_Release(lpddraw); lpddraw=NULL; return; } IDirectDrawSurface_SetPalette(lpddsurf,lpddpal); vga_refresh=0; /* poll every 20ms (50fps should provide adequate responsiveness) */ VGA_InstallTimer(20); } par->ret=0; return; } int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth) { ModeSet par; par.Xres = Xres; par.Yres = Yres; par.Depth = Depth; MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par); return par.ret; } int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth) { if (!lpddraw) return 1; if (!lpddsurf) return 1; if (Height) *Height=sdesc.dwHeight; if (Width) *Width=sdesc.dwWidth; if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount; return 0; } static void WINAPI VGA_DoExit(ULONG_PTR arg) { VGA_DeinstallTimer(); IDirectDrawSurface_SetPalette(lpddsurf,NULL); IDirectDrawSurface_Release(lpddsurf); lpddsurf=NULL; IDirectDrawPalette_Release(lpddpal); lpddpal=NULL; IDirectDraw_Release(lpddraw); lpddraw=NULL; } void VGA_Exit(void) { if (lpddraw) MZ_RunInThread(VGA_DoExit, 0); } void VGA_SetPalette(PALETTEENTRY*pal,int start,int len) { if (!lpddraw) return; IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal); } void VGA_SetQuadPalette(RGBQUAD*color,int start,int len) { PALETTEENTRY pal[256]; int c; if (!lpddraw) return; for (c=0; c=0) *dat = attr; dat++; } } /*** CONTROL ***/ void CALLBACK VGA_Poll( ULONG_PTR arg ) { char *dat; unsigned int Pitch,Height,Width,Y,X; char *surf; if (!InterlockedExchangeAdd(&vga_polling, 1)) { /* FIXME: optimize by doing this only if the data has actually changed * (in a way similar to DIBSection, perhaps) */ if (lpddraw) { /* graphics mode */ surf = VGA_Lock(&Pitch,&Height,&Width,NULL); if (!surf) return; dat = DOSMEM_MapDosToLinear(0xa0000); /* copy from virtual VGA frame buffer to DirectDraw surface */ for (Y=0; Y