Fixed unconditional use of JOYDEV.
[wine/wine64.git] / graphics / vga.c
blob1d5889c574f455d6720fc03dec5c7da3f9e93cac
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 void VGA_DeinstallTimer(void)
28 if (poll_timer) {
29 SERVICE_Delete( poll_timer );
30 poll_timer = 0;
34 static void VGA_InstallTimer(unsigned Rate)
36 VGA_DeinstallTimer();
37 if (!poll_timer)
38 poll_timer = SERVICE_AddTimer( Rate, VGA_Poll, 0 );
41 HANDLE VGA_AlphaConsole(void)
43 /* this assumes that no Win32 redirection has taken place, but then again,
44 * only 16-bit apps are likely to use this part of Wine... */
45 return GetStdHandle(STD_OUTPUT_HANDLE);
48 /*** GRAPHICS MODE ***/
50 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
52 if (lpddraw) VGA_Exit();
53 if (!lpddraw) {
54 DirectDrawCreate(NULL,&lpddraw,NULL);
55 if (!lpddraw) {
56 ERR("DirectDraw is not available\n");
57 return 1;
59 if (IDirectDraw_SetDisplayMode(lpddraw,Xres,Yres,Depth)) {
60 ERR("DirectDraw does not support requested display mode\n");
61 IDirectDraw_Release(lpddraw);
62 lpddraw=NULL;
63 return 1;
65 IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
66 memset(&sdesc,0,sizeof(sdesc));
67 sdesc.dwSize=sizeof(sdesc);
68 sdesc.dwFlags = DDSD_CAPS;
69 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
70 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
71 ERR("DirectDraw surface is not available\n");
72 IDirectDraw_Release(lpddraw);
73 lpddraw=NULL;
74 return 1;
76 vga_refresh=0;
77 /* poll every 20ms (50fps should provide adequate responsiveness) */
78 VGA_InstallTimer(20000);
80 return 0;
83 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
85 if (!lpddraw) return 1;
86 if (!lpddsurf) return 1;
87 if (Height) *Height=sdesc.dwHeight;
88 if (Width) *Width=sdesc.dwWidth;
89 if (Depth) *Depth=sdesc.ddpfPixelFormat.u.dwRGBBitCount;
90 return 0;
93 void VGA_Exit(void)
95 if (lpddraw) {
96 VGA_DeinstallTimer();
97 IDirectDrawSurface_Release(lpddsurf);
98 lpddsurf=NULL;
99 IDirectDraw_Release(lpddraw);
100 lpddraw=NULL;
104 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
106 if (!lpddraw) return;
107 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
108 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
111 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
113 PALETTEENTRY pal[256];
114 int c;
116 if (!lpddraw) return;
117 for (c=0; c<len; c++) {
118 pal[c].peRed =color[c].rgbRed;
119 pal[c].peGreen=color[c].rgbGreen;
120 pal[c].peBlue =color[c].rgbBlue;
121 pal[c].peFlags=0;
123 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
124 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
127 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
129 if (!lpddraw) return NULL;
130 if (!lpddsurf) return NULL;
131 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
132 ERR("could not lock surface!\n");
133 return NULL;
135 if (Pitch) *Pitch=sdesc.lPitch;
136 if (Height) *Height=sdesc.dwHeight;
137 if (Width) *Width=sdesc.dwWidth;
138 if (Depth) *Depth=sdesc.ddpfPixelFormat.u.dwRGBBitCount;
139 return sdesc.u1.lpSurface;
142 void VGA_Unlock(void)
144 IDirectDrawSurface_Unlock(lpddsurf,sdesc.u1.lpSurface);
147 /*** TEXT MODE ***/
149 int VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
151 COORD siz;
153 if (lpddraw) VGA_Exit();
155 /* the xterm is slow, so refresh only every 200ms (5fps) */
156 VGA_InstallTimer(200000);
158 siz.x = Xres;
159 siz.y = Yres;
160 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz);
161 return 0;
164 void VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
166 CONSOLE_SCREEN_BUFFER_INFO info;
167 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
168 if (Xres) *Xres=info.dwSize.x;
169 if (Yres) *Yres=info.dwSize.y;
172 void VGA_SetCursorPos(unsigned X,unsigned Y)
174 COORD pos;
176 pos.x = X;
177 pos.y = Y;
178 SetConsoleCursorPosition(VGA_AlphaConsole(),pos);
181 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
183 CONSOLE_SCREEN_BUFFER_INFO info;
184 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
185 if (X) *X=info.dwCursorPosition.x;
186 if (Y) *Y=info.dwCursorPosition.y;
189 /*** CONTROL ***/
191 void CALLBACK VGA_Poll( ULONG_PTR arg )
193 char *dat;
194 unsigned Pitch,Height,Width;
195 char *surf;
196 int Y,X;
198 if (!InterlockedExchangeAdd(&vga_polling, 1)) {
199 /* FIXME: optimize by doing this only if the data has actually changed
200 * (in a way similar to DIBSection, perhaps) */
201 if (lpddraw) {
202 /* graphics mode */
203 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
204 if (!surf) return;
205 dat = DOSMEM_MapDosToLinear(0xa0000);
206 /* copy from virtual VGA frame buffer to DirectDraw surface */
207 for (Y=0; Y<Height; Y++,surf+=Pitch,dat+=Width) {
208 memcpy(surf,dat,Width);
209 /*for (X=0; X<Width; X++) if (dat[X]) TRACE(ddraw,"data(%d) at (%d,%d)\n",dat[X],X,Y);*/
211 VGA_Unlock();
212 } else {
213 /* text mode */
214 CHAR_INFO ch[80];
215 COORD siz, off;
216 SMALL_RECT dest;
217 HANDLE con = VGA_AlphaConsole();
219 VGA_GetAlphaMode(&Width,&Height);
220 dat = DOSMEM_MapDosToLinear(0xb8000);
221 siz.x = 80; siz.y = 1;
222 off.x = 0; off.y = 0;
223 /* copy from virtual VGA frame buffer to console */
224 for (Y=0; Y<Height; Y++) {
225 dest.Top=Y; dest.Bottom=Y;
226 for (X=0; X<Width; X++) {
227 ch[X].Char.AsciiChar = *dat++;
228 ch[X].Attributes = *dat++;
230 dest.Left=0; dest.Right=Width+1;
231 WriteConsoleOutputA(con, ch, siz, off, &dest);
234 vga_refresh=1;
236 InterlockedDecrement(&vga_polling);
239 static BYTE palreg,palcnt;
240 static PALETTEENTRY paldat;
242 void VGA_ioport_out( WORD port, BYTE val )
244 switch (port) {
245 case 0x3c8:
246 palreg=val; palcnt=0; break;
247 case 0x3c9:
248 ((BYTE*)&paldat)[palcnt++]=val << 2;
249 if (palcnt==3) {
250 VGA_SetPalette(&paldat,palreg++,1);
251 palcnt=0;
253 break;
257 BYTE VGA_ioport_in( WORD port )
259 BYTE ret;
261 switch (port) {
262 case 0x3da:
263 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
264 we need to fake the occurrence of the vertical refresh */
265 ret=vga_refresh?0x00:0x08;
266 vga_refresh=0;
267 break;
268 default:
269 ret=0xff;
271 return ret;
274 void VGA_Clean(void)
276 VGA_Exit();
277 VGA_DeinstallTimer();