Prepare background while drawing radio buttons and checkboxes.
[wine.git] / msdos / vga.c
blob339618527e88fb190a81a96b2a95b609264f8c35
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 typedef HRESULT WINAPI (*DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
27 static DirectDrawCreateProc pDirectDrawCreate;
29 static void VGA_DeinstallTimer(void)
31 if (poll_timer) {
32 SERVICE_Delete( poll_timer );
33 poll_timer = 0;
37 static void VGA_InstallTimer(unsigned Rate)
39 VGA_DeinstallTimer();
40 if (!poll_timer)
41 poll_timer = SERVICE_AddTimer( Rate, VGA_Poll, 0 );
44 HANDLE VGA_AlphaConsole(void)
46 /* this assumes that no Win32 redirection has taken place, but then again,
47 * only 16-bit apps are likely to use this part of Wine... */
48 return GetStdHandle(STD_OUTPUT_HANDLE);
51 char*VGA_AlphaBuffer(void)
53 return DOSMEM_MapDosToLinear(0xb8000);
56 /*** GRAPHICS MODE ***/
58 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
60 if (lpddraw) VGA_Exit();
61 if (!lpddraw) {
62 if (!pDirectDrawCreate)
64 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
65 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
67 if (pDirectDrawCreate) pDirectDrawCreate(NULL,&lpddraw,NULL);
68 if (!lpddraw) {
69 ERR("DirectDraw is not available\n");
70 return 1;
72 if (IDirectDraw_SetDisplayMode(lpddraw,Xres,Yres,Depth)) {
73 ERR("DirectDraw does not support requested display mode\n");
74 IDirectDraw_Release(lpddraw);
75 lpddraw=NULL;
76 return 1;
78 IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
79 memset(&sdesc,0,sizeof(sdesc));
80 sdesc.dwSize=sizeof(sdesc);
81 sdesc.dwFlags = DDSD_CAPS;
82 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
83 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
84 ERR("DirectDraw surface is not available\n");
85 IDirectDraw_Release(lpddraw);
86 lpddraw=NULL;
87 return 1;
89 FIXME("no default palette entries\n");
90 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
91 vga_refresh=0;
92 /* poll every 20ms (50fps should provide adequate responsiveness) */
93 VGA_InstallTimer(20);
95 return 0;
98 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
100 if (!lpddraw) return 1;
101 if (!lpddsurf) return 1;
102 if (Height) *Height=sdesc.dwHeight;
103 if (Width) *Width=sdesc.dwWidth;
104 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
105 return 0;
108 void VGA_Exit(void)
110 if (lpddraw) {
111 VGA_DeinstallTimer();
112 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
113 IDirectDrawSurface_Release(lpddsurf);
114 lpddsurf=NULL;
115 IDirectDrawPalette_Release(lpddpal);
116 lpddpal=NULL;
117 IDirectDraw_Release(lpddraw);
118 lpddraw=NULL;
122 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
124 if (!lpddraw) return;
125 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
128 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
130 PALETTEENTRY pal[256];
131 int c;
133 if (!lpddraw) return;
134 for (c=0; c<len; c++) {
135 pal[c].peRed =color[c].rgbRed;
136 pal[c].peGreen=color[c].rgbGreen;
137 pal[c].peBlue =color[c].rgbBlue;
138 pal[c].peFlags=0;
140 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
143 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
145 if (!lpddraw) return NULL;
146 if (!lpddsurf) return NULL;
147 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
148 ERR("could not lock surface!\n");
149 return NULL;
151 if (Pitch) *Pitch=sdesc.u1.lPitch;
152 if (Height) *Height=sdesc.dwHeight;
153 if (Width) *Width=sdesc.dwWidth;
154 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
155 return sdesc.lpSurface;
158 void VGA_Unlock(void)
160 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
163 /*** TEXT MODE ***/
165 int VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
167 COORD siz;
169 if (lpddraw) VGA_Exit();
171 /* the xterm is slow, so refresh only every 200ms (5fps) */
172 VGA_InstallTimer(200);
174 siz.X = Xres;
175 siz.Y = Yres;
176 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz);
177 return 0;
180 void VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
182 CONSOLE_SCREEN_BUFFER_INFO info;
183 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
184 if (Xres) *Xres=info.dwSize.X;
185 if (Yres) *Yres=info.dwSize.Y;
188 void VGA_SetCursorPos(unsigned X,unsigned Y)
190 COORD pos;
192 pos.X = X;
193 pos.Y = Y;
194 SetConsoleCursorPosition(VGA_AlphaConsole(),pos);
197 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
199 CONSOLE_SCREEN_BUFFER_INFO info;
200 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
201 if (X) *X=info.dwCursorPosition.X;
202 if (Y) *Y=info.dwCursorPosition.Y;
205 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
207 unsigned XR, YR;
208 char*dat;
210 VGA_GetAlphaMode(&XR, &YR);
211 dat = VGA_AlphaBuffer() + ((XR*Y + X) * 2);
212 /* FIXME: also call WriteConsoleOutputA, for better responsiveness */
213 while (count--) {
214 *dat++ = ch;
215 if (attr>=0) *dat = attr;
216 dat++;
220 /*** CONTROL ***/
222 void CALLBACK VGA_Poll( ULONG_PTR arg )
224 char *dat;
225 unsigned int Pitch,Height,Width,Y,X;
226 char *surf;
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 /* WriteConsoleOutputA doesn't like "dead" chars */
259 if (ch[X].Char.AsciiChar == '\0')
260 ch[X].Char.AsciiChar = ' ';
261 ch[X].Attributes = *dat++;
263 dest.Left=0; dest.Right=Width+1;
264 WriteConsoleOutputA(con, ch, siz, off, &dest);
267 vga_refresh=1;
269 InterlockedDecrement(&vga_polling);
272 static BYTE palreg,palcnt;
273 static PALETTEENTRY paldat;
275 void VGA_ioport_out( WORD port, BYTE val )
277 switch (port) {
278 case 0x3c8:
279 palreg=val; palcnt=0; break;
280 case 0x3c9:
281 ((BYTE*)&paldat)[palcnt++]=val << 2;
282 if (palcnt==3) {
283 VGA_SetPalette(&paldat,palreg++,1);
284 palcnt=0;
286 break;
290 BYTE VGA_ioport_in( WORD port )
292 BYTE ret;
294 switch (port) {
295 case 0x3da:
296 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
297 we need to fake the occurrence of the vertical refresh */
298 ret=vga_refresh?0x00:0x08;
299 vga_refresh=0;
300 break;
301 default:
302 ret=0xff;
304 return ret;
307 void VGA_Clean(void)
309 VGA_Exit();
310 VGA_DeinstallTimer();