Do not free the not owned error string.
[wine.git] / msdos / vga.c
blobff70d0f359c2cc11b20e37dc1bcf7080db2b407d
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 int Pitch,Height,Width,Y,X;
225 char *surf;
227 if (!InterlockedExchangeAdd(&vga_polling, 1)) {
228 /* FIXME: optimize by doing this only if the data has actually changed
229 * (in a way similar to DIBSection, perhaps) */
230 if (lpddraw) {
231 /* graphics mode */
232 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
233 if (!surf) return;
234 dat = DOSMEM_MapDosToLinear(0xa0000);
235 /* copy from virtual VGA frame buffer to DirectDraw surface */
236 for (Y=0; Y<Height; Y++,surf+=Pitch,dat+=Width) {
237 memcpy(surf,dat,Width);
238 /*for (X=0; X<Width; X++) if (dat[X]) TRACE(ddraw,"data(%d) at (%d,%d)\n",dat[X],X,Y);*/
240 VGA_Unlock();
241 } else {
242 /* text mode */
243 CHAR_INFO ch[80];
244 COORD siz, off;
245 SMALL_RECT dest;
246 HANDLE con = VGA_AlphaConsole();
248 VGA_GetAlphaMode(&Width,&Height);
249 dat = VGA_AlphaBuffer();
250 siz.X = 80; siz.Y = 1;
251 off.X = 0; off.Y = 0;
252 /* copy from virtual VGA frame buffer to console */
253 for (Y=0; Y<Height; Y++) {
254 dest.Top=Y; dest.Bottom=Y;
255 for (X=0; X<Width; X++) {
256 ch[X].Char.AsciiChar = *dat++;
257 /* WriteConsoleOutputA doesn't like "dead" chars */
258 if (ch[X].Char.AsciiChar == '\0')
259 ch[X].Char.AsciiChar = ' ';
260 ch[X].Attributes = *dat++;
262 dest.Left=0; dest.Right=Width+1;
263 WriteConsoleOutputA(con, ch, siz, off, &dest);
266 vga_refresh=1;
268 InterlockedDecrement(&vga_polling);
271 static BYTE palreg,palcnt;
272 static PALETTEENTRY paldat;
274 void VGA_ioport_out( WORD port, BYTE val )
276 switch (port) {
277 case 0x3c8:
278 palreg=val; palcnt=0; break;
279 case 0x3c9:
280 ((BYTE*)&paldat)[palcnt++]=val << 2;
281 if (palcnt==3) {
282 VGA_SetPalette(&paldat,palreg++,1);
283 palcnt=0;
285 break;
289 BYTE VGA_ioport_in( WORD port )
291 BYTE ret;
293 switch (port) {
294 case 0x3da:
295 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
296 we need to fake the occurrence of the vertical refresh */
297 ret=vga_refresh?0x00:0x08;
298 vga_refresh=0;
299 break;
300 default:
301 ret=0xff;
303 return ret;
306 void VGA_Clean(void)
308 VGA_Exit();
309 VGA_DeinstallTimer();