If we are looking for an existing pixmap format, we do not need the
[wine.git] / msdos / vga.c
blob89aae227acf10ebce63acb8cfb2c1ba3717ef379
1 /*
2 * VGA hardware emulation
3 *
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
6 */
8 #include <string.h>
9 #include "winbase.h"
10 #include "wincon.h"
11 #include "miscemu.h"
12 #include "vga.h"
13 #include "ddraw.h"
14 #include "services.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(ddraw)
19 static IDirectDraw *lpddraw = NULL;
20 static IDirectDrawSurface *lpddsurf;
21 static IDirectDrawPalette *lpddpal;
22 static DDSURFACEDESC sdesc;
23 static LONG vga_polling,vga_refresh;
24 static HANDLE poll_timer;
26 static HRESULT WINAPI (*pDirectDrawCreate)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
28 static void VGA_DeinstallTimer(void)
30 if (poll_timer) {
31 SERVICE_Delete( poll_timer );
32 poll_timer = 0;
36 static void VGA_InstallTimer(unsigned Rate)
38 VGA_DeinstallTimer();
39 if (!poll_timer)
40 poll_timer = SERVICE_AddTimer( Rate, VGA_Poll, 0 );
43 HANDLE VGA_AlphaConsole(void)
45 /* this assumes that no Win32 redirection has taken place, but then again,
46 * only 16-bit apps are likely to use this part of Wine... */
47 return GetStdHandle(STD_OUTPUT_HANDLE);
50 char*VGA_AlphaBuffer(void)
52 return DOSMEM_MapDosToLinear(0xb8000);
55 /*** GRAPHICS MODE ***/
57 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
59 if (lpddraw) VGA_Exit();
60 if (!lpddraw) {
61 if (!pDirectDrawCreate)
63 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
64 if (hmod) pDirectDrawCreate = GetProcAddress( hmod, "DirectDrawCreate" );
66 if (pDirectDrawCreate) pDirectDrawCreate(NULL,&lpddraw,NULL);
67 if (!lpddraw) {
68 ERR("DirectDraw is not available\n");
69 return 1;
71 if (IDirectDraw_SetDisplayMode(lpddraw,Xres,Yres,Depth)) {
72 ERR("DirectDraw does not support requested display mode\n");
73 IDirectDraw_Release(lpddraw);
74 lpddraw=NULL;
75 return 1;
77 IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
78 memset(&sdesc,0,sizeof(sdesc));
79 sdesc.dwSize=sizeof(sdesc);
80 sdesc.dwFlags = DDSD_CAPS;
81 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
82 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
83 ERR("DirectDraw surface is not available\n");
84 IDirectDraw_Release(lpddraw);
85 lpddraw=NULL;
86 return 1;
88 FIXME("no default palette entries\n");
89 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
90 vga_refresh=0;
91 /* poll every 20ms (50fps should provide adequate responsiveness) */
92 VGA_InstallTimer(20);
94 return 0;
97 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
99 if (!lpddraw) return 1;
100 if (!lpddsurf) return 1;
101 if (Height) *Height=sdesc.dwHeight;
102 if (Width) *Width=sdesc.dwWidth;
103 if (Depth) *Depth=sdesc.ddpfPixelFormat.u.dwRGBBitCount;
104 return 0;
107 void VGA_Exit(void)
109 if (lpddraw) {
110 VGA_DeinstallTimer();
111 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
112 IDirectDrawSurface_Release(lpddsurf);
113 lpddsurf=NULL;
114 IDirectDrawPalette_Release(lpddpal);
115 lpddpal=NULL;
116 IDirectDraw_Release(lpddraw);
117 lpddraw=NULL;
121 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
123 if (!lpddraw) return;
124 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
127 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
129 PALETTEENTRY pal[256];
130 int c;
132 if (!lpddraw) return;
133 for (c=0; c<len; c++) {
134 pal[c].peRed =color[c].rgbRed;
135 pal[c].peGreen=color[c].rgbGreen;
136 pal[c].peBlue =color[c].rgbBlue;
137 pal[c].peFlags=0;
139 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
142 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
144 if (!lpddraw) return NULL;
145 if (!lpddsurf) return NULL;
146 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
147 ERR("could not lock surface!\n");
148 return NULL;
150 if (Pitch) *Pitch=sdesc.lPitch;
151 if (Height) *Height=sdesc.dwHeight;
152 if (Width) *Width=sdesc.dwWidth;
153 if (Depth) *Depth=sdesc.ddpfPixelFormat.u.dwRGBBitCount;
154 return sdesc.u1.lpSurface;
157 void VGA_Unlock(void)
159 IDirectDrawSurface_Unlock(lpddsurf,sdesc.u1.lpSurface);
162 /*** TEXT MODE ***/
164 int VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
166 COORD siz;
168 if (lpddraw) VGA_Exit();
170 /* the xterm is slow, so refresh only every 200ms (5fps) */
171 VGA_InstallTimer(200);
173 siz.X = Xres;
174 siz.Y = Yres;
175 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz);
176 return 0;
179 void VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
181 CONSOLE_SCREEN_BUFFER_INFO info;
182 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
183 if (Xres) *Xres=info.dwSize.X;
184 if (Yres) *Yres=info.dwSize.Y;
187 void VGA_SetCursorPos(unsigned X,unsigned Y)
189 COORD pos;
191 pos.X = X;
192 pos.Y = Y;
193 SetConsoleCursorPosition(VGA_AlphaConsole(),pos);
196 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
198 CONSOLE_SCREEN_BUFFER_INFO info;
199 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
200 if (X) *X=info.dwCursorPosition.X;
201 if (Y) *Y=info.dwCursorPosition.Y;
204 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
206 unsigned XR, YR;
207 char*dat;
209 VGA_GetAlphaMode(&XR, &YR);
210 dat = VGA_AlphaBuffer() + ((XR*Y + X) * 2);
211 /* FIXME: also call WriteConsoleOutputA, for better responsiveness */
212 while (count--) {
213 *dat++ = ch;
214 if (attr>=0) *dat = attr;
215 dat++;
219 /*** CONTROL ***/
221 void CALLBACK VGA_Poll( ULONG_PTR arg )
223 char *dat;
224 unsigned Pitch,Height,Width;
225 char *surf;
226 int Y,X;
228 if (!InterlockedExchangeAdd(&vga_polling, 1)) {
229 /* FIXME: optimize by doing this only if the data has actually changed
230 * (in a way similar to DIBSection, perhaps) */
231 if (lpddraw) {
232 /* graphics mode */
233 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
234 if (!surf) return;
235 dat = DOSMEM_MapDosToLinear(0xa0000);
236 /* copy from virtual VGA frame buffer to DirectDraw surface */
237 for (Y=0; Y<Height; Y++,surf+=Pitch,dat+=Width) {
238 memcpy(surf,dat,Width);
239 /*for (X=0; X<Width; X++) if (dat[X]) TRACE(ddraw,"data(%d) at (%d,%d)\n",dat[X],X,Y);*/
241 VGA_Unlock();
242 } else {
243 /* text mode */
244 CHAR_INFO ch[80];
245 COORD siz, off;
246 SMALL_RECT dest;
247 HANDLE con = VGA_AlphaConsole();
249 VGA_GetAlphaMode(&Width,&Height);
250 dat = VGA_AlphaBuffer();
251 siz.X = 80; siz.Y = 1;
252 off.X = 0; off.Y = 0;
253 /* copy from virtual VGA frame buffer to console */
254 for (Y=0; Y<Height; Y++) {
255 dest.Top=Y; dest.Bottom=Y;
256 for (X=0; X<Width; X++) {
257 ch[X].Char.AsciiChar = *dat++;
258 ch[X].Attributes = *dat++;
260 dest.Left=0; dest.Right=Width+1;
261 WriteConsoleOutputA(con, ch, siz, off, &dest);
264 vga_refresh=1;
266 InterlockedDecrement(&vga_polling);
269 static BYTE palreg,palcnt;
270 static PALETTEENTRY paldat;
272 void VGA_ioport_out( WORD port, BYTE val )
274 switch (port) {
275 case 0x3c8:
276 palreg=val; palcnt=0; break;
277 case 0x3c9:
278 ((BYTE*)&paldat)[palcnt++]=val << 2;
279 if (palcnt==3) {
280 VGA_SetPalette(&paldat,palreg++,1);
281 palcnt=0;
283 break;
287 BYTE VGA_ioport_in( WORD port )
289 BYTE ret;
291 switch (port) {
292 case 0x3da:
293 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
294 we need to fake the occurrence of the vertical refresh */
295 ret=vga_refresh?0x00:0x08;
296 vga_refresh=0;
297 break;
298 default:
299 ret=0xff;
301 return ret;
304 void VGA_Clean(void)
306 VGA_Exit();
307 VGA_DeinstallTimer();