vga.c 9.29 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * VGA hardware emulation
 * 
 * Copyright 1998 Ove Kven (with some help from Marcus Meissner)
 *
 */

#include <string.h>
#include "winbase.h"
10 11
#include "wingdi.h"
#include "winuser.h"
12
#include "wincon.h"
13 14 15
#include "miscemu.h"
#include "vga.h"
#include "ddraw.h"
16
#include "services.h"
17
#include "debugtools.h"
18

19
DEFAULT_DEBUG_CHANNEL(ddraw);
20

21 22 23 24
static IDirectDraw *lpddraw = NULL;
static IDirectDrawSurface *lpddsurf;
static IDirectDrawPalette *lpddpal;
static DDSURFACEDESC sdesc;
25 26 27
static LONG vga_polling,vga_refresh;
static HANDLE poll_timer;

28 29
typedef HRESULT WINAPI (*DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
static DirectDrawCreateProc pDirectDrawCreate;
30

31 32 33
typedef HWND WINAPI (*CreateWindowExAProc)(DWORD,LPCSTR,LPCSTR,DWORD,INT,INT, INT,INT,HWND,HMENU,HINSTANCE,LPVOID);
static CreateWindowExAProc pCreateWindowExA;

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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);
}

56 57 58 59 60
char*VGA_AlphaBuffer(void)
{
    return DOSMEM_MapDosToLinear(0xb8000);
}

61
/*** GRAPHICS MODE ***/
62 63 64

int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
{
65 66 67
    LRESULT	res;
    HWND	hwnd;

68 69
    if (lpddraw) VGA_Exit();
    if (!lpddraw) {
70 71 72
        if (!pDirectDrawCreate)
        {
            HMODULE hmod = LoadLibraryA( "ddraw.dll" );
73
            if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
74 75 76 77
	    if (!pDirectDrawCreate) {
		ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
		return 1;
	    }
78
        }
79 80 81 82 83 84 85 86 87 88 89 90 91 92
        if (!pCreateWindowExA)
	{
            HMODULE hmod = LoadLibraryA( "user32.dll" );
	    if (!hmod) {
		ERR("Can't load user32.dll.\n");
		return 1;
	    }
            if (hmod) pCreateWindowExA = (CreateWindowExAProc)GetProcAddress( hmod, "CreateWindowExA" );
	    if (!pCreateWindowExA) {
		ERR("Can't lookup CreateWindowExA from user32.dll.\n");
		return 1;
	    }
	}
        res = pDirectDrawCreate(NULL,&lpddraw,NULL);
93
        if (!lpddraw) {
94
            ERR("DirectDraw is not available (res = %lx)\n",res);
95 96
            return 1;
        }
97 98 99 100 101 102 103 104 105 106
	hwnd = pCreateWindowExA(0,"STATIC","WINEDOS VGA",WS_POPUP|WS_BORDER|WS_CAPTION|WS_SYSMENU,0,0,Xres,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,Xres,Yres,Depth))) {
            ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",Xres,Yres,Depth,res);
107
            IDirectDraw_Release(lpddraw);
108 109 110
            lpddraw=NULL;
            return 1;
        }
111 112 113 114
        res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
	if (res) {
	    ERR("Could not create palette (res = %lx)\n",res);
	}
115 116
        memset(&sdesc,0,sizeof(sdesc));
        sdesc.dwSize=sizeof(sdesc);
117 118
	sdesc.dwFlags = DDSD_CAPS;
	sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
119
        if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
120
            ERR("DirectDraw surface is not available\n");
121
            IDirectDraw_Release(lpddraw);
122 123 124
            lpddraw=NULL;
            return 1;
        }
125 126
        FIXME("no default palette entries\n");
        IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
127
        vga_refresh=0;
128
        /* poll every 20ms (50fps should provide adequate responsiveness) */
129
        VGA_InstallTimer(20);
130 131 132 133 134 135 136 137 138 139
    }
    return 0;
}

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;
140
    if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
141 142 143 144 145 146
    return 0;
}

void VGA_Exit(void)
{
    if (lpddraw) {
147
        VGA_DeinstallTimer();
148
        IDirectDrawSurface_SetPalette(lpddsurf,NULL);
149
        IDirectDrawSurface_Release(lpddsurf);
150
        lpddsurf=NULL;
151 152
        IDirectDrawPalette_Release(lpddpal);
        lpddpal=NULL;
153
        IDirectDraw_Release(lpddraw);
154 155 156 157 158 159 160
        lpddraw=NULL;
    }
}

void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
{
    if (!lpddraw) return;
161
    IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
162 163 164 165 166 167 168 169 170 171 172 173 174 175
}

void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
{
    PALETTEENTRY pal[256];
    int c;

    if (!lpddraw) return;
    for (c=0; c<len; c++) {
        pal[c].peRed  =color[c].rgbRed;
        pal[c].peGreen=color[c].rgbGreen;
        pal[c].peBlue =color[c].rgbBlue;
        pal[c].peFlags=0;
    }
176
    IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
177 178 179 180 181 182
}

LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
{
    if (!lpddraw) return NULL;
    if (!lpddsurf) return NULL;
183
    if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
184
        ERR("could not lock surface!\n");
185 186
        return NULL;
    }
187
    if (Pitch) *Pitch=sdesc.u1.lPitch;
188 189
    if (Height) *Height=sdesc.dwHeight;
    if (Width) *Width=sdesc.dwWidth;
190 191
    if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
    return sdesc.lpSurface;
192 193 194 195
}

void VGA_Unlock(void)
{
196
    IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
197 198
}

199 200 201 202 203 204 205 206 207
/*** TEXT MODE ***/

int VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
{
    COORD siz;

    if (lpddraw) VGA_Exit();

    /* the xterm is slow, so refresh only every 200ms (5fps) */
208
    VGA_InstallTimer(200);
209

210 211
    siz.X = Xres;
    siz.Y = Yres;
212 213 214 215 216 217 218 219
    SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz);
    return 0;
}

void VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
{
    CONSOLE_SCREEN_BUFFER_INFO info;
    GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
220 221
    if (Xres) *Xres=info.dwSize.X;
    if (Yres) *Yres=info.dwSize.Y;
222 223 224 225 226 227
}

void VGA_SetCursorPos(unsigned X,unsigned Y)
{
    COORD pos;
    
228 229
    pos.X = X;
    pos.Y = Y;
230 231 232 233 234 235 236
    SetConsoleCursorPosition(VGA_AlphaConsole(),pos);
}

void VGA_GetCursorPos(unsigned*X,unsigned*Y)
{
    CONSOLE_SCREEN_BUFFER_INFO info;
    GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
237 238
    if (X) *X=info.dwCursorPosition.X;
    if (Y) *Y=info.dwCursorPosition.Y;
239 240
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
{
    unsigned XR, YR;
    char*dat;

    VGA_GetAlphaMode(&XR, &YR);
    dat = VGA_AlphaBuffer() + ((XR*Y + X) * 2);
    /* FIXME: also call WriteConsoleOutputA, for better responsiveness */
    while (count--) {
        *dat++ = ch;
        if (attr>=0) *dat = attr;
        dat++;
    }
}

256 257 258
/*** CONTROL ***/

void CALLBACK VGA_Poll( ULONG_PTR arg )
259 260
{
    char *dat;
261
    unsigned int Pitch,Height,Width,Y,X;
262 263
    char *surf;

264
    if (!InterlockedExchangeAdd(&vga_polling, 1)) {
265 266
        /* FIXME: optimize by doing this only if the data has actually changed
         *        (in a way similar to DIBSection, perhaps) */
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
        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<Height; Y++,surf+=Pitch,dat+=Width) {
              memcpy(surf,dat,Width);
              /*for (X=0; X<Width; X++) if (dat[X]) TRACE(ddraw,"data(%d) at (%d,%d)\n",dat[X],X,Y);*/
          }
          VGA_Unlock();
        } else {
          /* text mode */
          CHAR_INFO ch[80];
          COORD siz, off;
          SMALL_RECT dest;
          HANDLE con = VGA_AlphaConsole();

          VGA_GetAlphaMode(&Width,&Height);
286
          dat = VGA_AlphaBuffer();
287 288
          siz.X = 80; siz.Y = 1;
          off.X = 0; off.Y = 0;
289 290 291 292 293
          /* copy from virtual VGA frame buffer to console */
          for (Y=0; Y<Height; Y++) {
              dest.Top=Y; dest.Bottom=Y;
              for (X=0; X<Width; X++) {
                  ch[X].Char.AsciiChar = *dat++;
294 295 296
		  /* WriteConsoleOutputA doesn't like "dead" chars */
		  if (ch[X].Char.AsciiChar == '\0')
		      ch[X].Char.AsciiChar = ' ';
297 298 299 300 301
                  ch[X].Attributes = *dat++;
              }
              dest.Left=0; dest.Right=Width+1;
              WriteConsoleOutputA(con, ch, siz, off, &dest);
          }
302
        }
303
        vga_refresh=1;
304
    }
305
    InterlockedDecrement(&vga_polling);
306 307 308 309 310 311 312 313 314 315 316 317 318
}

static BYTE palreg,palcnt;
static PALETTEENTRY paldat;

void VGA_ioport_out( WORD port, BYTE val )
{
    switch (port) {
        case 0x3c8:
            palreg=val; palcnt=0; break;
        case 0x3c9:
            ((BYTE*)&paldat)[palcnt++]=val << 2;
            if (palcnt==3) {
319 320
                VGA_SetPalette(&paldat,palreg++,1);
                palcnt=0;
321
            }
322
            break;
323 324
    }
}
325 326 327 328 329 330 331 332 333

BYTE VGA_ioport_in( WORD port )
{
    BYTE ret;

    switch (port) {
        case 0x3da:
            /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
               we need to fake the occurrence of the vertical refresh */
334 335
            ret=vga_refresh?0x00:0x08;
            vga_refresh=0;
336 337 338 339 340 341
            break;
        default:
            ret=0xff;
    }
    return ret;
}
342 343 344 345 346 347

void VGA_Clean(void)
{
    VGA_Exit();
    VGA_DeinstallTimer();
}