Don't consider no sound device a failure.
[wine/wine-gecko.git] / dlls / winedos / vga.c
blob7ca39bdf2aadf096462d2a5f2c4721ca5b077cb6
1 /*
2 * VGA hardware emulation
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <string.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "wincon.h"
31 #include "miscemu.h"
32 #include "dosexe.h"
33 #include "vga.h"
34 #include "ddraw.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 static IDirectDraw *lpddraw = NULL;
40 static IDirectDrawSurface *lpddsurf;
41 static IDirectDrawPalette *lpddpal;
42 static DDSURFACEDESC sdesc;
44 static BOOL vga_retrace_vertical;
45 static BOOL vga_retrace_horizontal;
48 * Size and location of VGA controller window to framebuffer.
50 * Note: We support only single window even though some
51 * controllers support two. This should not be changed unless
52 * there are programs that depend on having two windows.
54 #define VGA_WINDOW_SIZE (64 * 1024)
55 #define VGA_WINDOW_START ((char *)0xa0000)
58 * VGA controller memory is emulated using linear framebuffer.
59 * This frambuffer also acts as an interface
60 * between VGA controller emulation and DirectDraw.
62 * vga_fb_width: Display width in pixels. Can be modified when
63 * display mode is changed.
64 * vga_fb_height: Display height in pixels. Can be modified when
65 * display mode is changed.
66 * vga_fb_depth: Number of bits used to store single pixel color information.
67 * Each pixel uses (vga_fb_depth+7)/8 bytes because
68 * 1-16 color modes are mapped to 256 color mode.
69 * Can be modified when display mode is changed.
70 * vga_fb_pitch: How many bytes to add to pointer in order to move
71 * from one row to another. This is fixed in VGA modes,
72 * but can be modified in SVGA modes.
73 * vga_fb_offset: Offset added to framebuffer start address in order
74 * to find the display origin. Programs use this to do
75 * double buffering and to scroll display. The value can
76 * be modified in VGA and SVGA modes.
77 * vga_fb_size: How many bytes are allocated to framebuffer.
78 * VGA framebuffers are always larger than display size and
79 * SVGA framebuffers may also be.
80 * vga_fb_data: Pointer to framebuffer start.
81 * vga_fb_window: Offset of 64k window 0xa0000 in bytes from framebuffer start.
82 * This value is >= 0, if mode uses linear framebuffer and
83 * -1, if mode uses color planes. This value is fixed
84 * in all modes except 0x13 (256 color VGA) where
85 * 0 means normal mode and -1 means Mode-X (unchained mode).
87 static int vga_fb_width;
88 static int vga_fb_height;
89 static int vga_fb_depth;
90 static int vga_fb_pitch;
91 static int vga_fb_offset;
92 static int vga_fb_size = 0;
93 static char *vga_fb_data = 0;
94 static int vga_fb_window = 0;
97 * VGA text mode data.
99 * vga_text_attr: Current active attribute.
100 * vga_text_old: Last data sent to console.
101 * This is used to optimize console updates.
102 * vga_text_width: Width of the text display in characters.
103 * vga_text_height: Height of the text display in characters.
104 * vga_text_x: Current cursor X-position. Starts from zero.
105 * vga_text_y: Current cursor Y-position. Starts from zero.
106 * vga_text_console: TRUE if stdout is console,
107 * FALSE if it is regular file.
109 static BYTE vga_text_attr;
110 static char *vga_text_old = NULL;
111 static BYTE vga_text_width;
112 static BYTE vga_text_height;
113 static BYTE vga_text_x;
114 static BYTE vga_text_y;
115 static BOOL vga_text_console;
118 * VGA controller ports 0x3c0, 0x3c4, 0x3ce and 0x3d4 are
119 * indexed registers. These ports are used to select VGA controller
120 * subregister that can be written to or read from using ports 0x3c1,
121 * 0x3c5, 0x3cf or 0x3d5. Selected subregister indexes are
122 * stored in variables vga_index_*.
124 * Port 0x3c0 is special because it is both index and
125 * data-write register. Flip-flop vga_address_3c0 tells whether
126 * the port acts currently as an address register. Reading from port
127 * 0x3da resets the flip-flop to address mode.
129 static BYTE vga_index_3c0;
130 static BYTE vga_index_3c4;
131 static BYTE vga_index_3ce;
132 static BYTE vga_index_3d4;
133 static BOOL vga_address_3c0 = TRUE;
136 * This mutex is used to protect VGA state during asynchronous
137 * screen updates (see VGA_Poll). It makes sure that VGA state changes
138 * are atomic and the user interface is protected from flicker and
139 * corruption.
141 * The mutex actually serializes VGA operations and the screen update.
142 * Which means that whenever VGA_Poll occurs, application stalls if it
143 * tries to modify VGA state. This is not how real VGA adapters work,
144 * but it makes timing and correctness issues much easier to deal with.
146 static CRITICAL_SECTION vga_lock;
147 static CRITICAL_SECTION_DEBUG critsect_debug =
149 0, 0, &vga_lock,
150 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
151 0, 0, { 0, (DWORD)(__FILE__ ": vga_lock") }
153 static CRITICAL_SECTION vga_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
155 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
156 static DirectDrawCreateProc pDirectDrawCreate;
158 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
160 static HWND vga_hwnd = NULL;
163 * For simplicity, I'm creating a second palette.
164 * 16 color accesses will use these pointers and insert
165 * entries from the 64-color palette into the default
166 * palette. --Robert 'Admiral' Coeyman
169 static char vga_16_palette[17]={
170 0x00, /* 0 - Black */
171 0x01, /* 1 - Blue */
172 0x02, /* 2 - Green */
173 0x03, /* 3 - Cyan */
174 0x04, /* 4 - Red */
175 0x05, /* 5 - Magenta */
176 0x14, /* 6 - Brown */
177 0x07, /* 7 - Light gray */
178 0x38, /* 8 - Dark gray */
179 0x39, /* 9 - Light blue */
180 0x3a, /* A - Light green */
181 0x3b, /* B - Light cyan */
182 0x3c, /* C - Light red */
183 0x3d, /* D - Light magenta */
184 0x3e, /* E - Yellow */
185 0x3f, /* F - White */
186 0x00 /* Border Color */
189 static PALETTEENTRY vga_def_palette[256]={
190 /* red green blue */
191 {0x00, 0x00, 0x00}, /* 0 - Black */
192 {0x00, 0x00, 0x80}, /* 1 - Blue */
193 {0x00, 0x80, 0x00}, /* 2 - Green */
194 {0x00, 0x80, 0x80}, /* 3 - Cyan */
195 {0x80, 0x00, 0x00}, /* 4 - Red */
196 {0x80, 0x00, 0x80}, /* 5 - Magenta */
197 {0x80, 0x80, 0x00}, /* 6 - Brown */
198 {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */
199 {0x80, 0x80, 0x80}, /* 8 - Dark gray */
200 {0x00, 0x00, 0xFF}, /* 9 - Light blue */
201 {0x00, 0xFF, 0x00}, /* A - Light green */
202 {0x00, 0xFF, 0xFF}, /* B - Light cyan */
203 {0xFF, 0x00, 0x00}, /* C - Light red */
204 {0xFF, 0x00, 0xFF}, /* D - Light magenta */
205 {0xFF, 0xFF, 0x00}, /* E - Yellow */
206 {0xFF, 0xFF, 0xFF}, /* F - White */
207 {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */
211 * This palette is the dos default, converted from 18 bit color to 24.
212 * It contains only 64 entries of colors--all others are zeros.
213 * --Robert 'Admiral' Coeyman
215 static PALETTEENTRY vga_def64_palette[256]={
216 /* red green blue */
217 {0x00, 0x00, 0x00}, /* 0x00 Black */
218 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
219 {0x00, 0xaa, 0x00}, /* 0x02 Green */
220 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
221 {0xaa, 0x00, 0x00}, /* 0x04 Red */
222 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
223 {0xaa, 0xaa, 0x00}, /* 0x06 */
224 {0xaa, 0xaa, 0xaa}, /* 0x07 Light Gray */
225 {0x00, 0x00, 0x55}, /* 0x08 */
226 {0x00, 0x00, 0xff}, /* 0x09 */
227 {0x00, 0xaa, 0x55}, /* 0x0a */
228 {0x00, 0xaa, 0xff}, /* 0x0b */
229 {0xaa, 0x00, 0x55}, /* 0x0c */
230 {0xaa, 0x00, 0xff}, /* 0x0d */
231 {0xaa, 0xaa, 0x55}, /* 0x0e */
232 {0xaa, 0xaa, 0xff}, /* 0x0f */
233 {0x00, 0x55, 0x00}, /* 0x10 */
234 {0x00, 0x55, 0xaa}, /* 0x11 */
235 {0x00, 0xff, 0x00}, /* 0x12 */
236 {0x00, 0xff, 0xaa}, /* 0x13 */
237 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
238 {0xaa, 0x55, 0xaa}, /* 0x15 */
239 {0xaa, 0xff, 0x00}, /* 0x16 */
240 {0xaa, 0xff, 0xaa}, /* 0x17 */
241 {0x00, 0x55, 0x55}, /* 0x18 */
242 {0x00, 0x55, 0xff}, /* 0x19 */
243 {0x00, 0xff, 0x55}, /* 0x1a */
244 {0x00, 0xff, 0xff}, /* 0x1b */
245 {0xaa, 0x55, 0x55}, /* 0x1c */
246 {0xaa, 0x55, 0xff}, /* 0x1d */
247 {0xaa, 0xff, 0x55}, /* 0x1e */
248 {0xaa, 0xff, 0xff}, /* 0x1f */
249 {0x55, 0x00, 0x00}, /* 0x20 */
250 {0x55, 0x00, 0xaa}, /* 0x21 */
251 {0x55, 0xaa, 0x00}, /* 0x22 */
252 {0x55, 0xaa, 0xaa}, /* 0x23 */
253 {0xff, 0x00, 0x00}, /* 0x24 */
254 {0xff, 0x00, 0xaa}, /* 0x25 */
255 {0xff, 0xaa, 0x00}, /* 0x26 */
256 {0xff, 0xaa, 0xaa}, /* 0x27 */
257 {0x55, 0x00, 0x55}, /* 0x28 */
258 {0x55, 0x00, 0xff}, /* 0x29 */
259 {0x55, 0xaa, 0x55}, /* 0x2a */
260 {0x55, 0xaa, 0xff}, /* 0x2b */
261 {0xff, 0x00, 0x55}, /* 0x2c */
262 {0xff, 0x00, 0xff}, /* 0x2d */
263 {0xff, 0xaa, 0x55}, /* 0x2e */
264 {0xff, 0xaa, 0xff}, /* 0x2f */
265 {0x55, 0x55, 0x00}, /* 0x30 */
266 {0x55, 0x55, 0xaa}, /* 0x31 */
267 {0x55, 0xff, 0x00}, /* 0x32 */
268 {0x55, 0xff, 0xaa}, /* 0x33 */
269 {0xff, 0x55, 0x00}, /* 0x34 */
270 {0xff, 0x55, 0xaa}, /* 0x35 */
271 {0xff, 0xff, 0x00}, /* 0x36 */
272 {0xff, 0xff, 0xaa}, /* 0x37 */
273 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
274 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
275 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
276 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
277 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
278 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
279 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
280 {0xff, 0xff, 0xff}, /* 0x3f White */
281 {0,0,0} /* The next 192 entries are all zeros */
284 static HANDLE VGA_timer;
285 static HANDLE VGA_timer_thread;
287 /* set the timer rate; called in the polling thread context */
288 static void CALLBACK set_timer_rate( ULONG_PTR arg )
290 LARGE_INTEGER when;
292 when.u.LowPart = when.u.HighPart = 0;
293 SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
296 static DWORD CALLBACK VGA_TimerThread( void *dummy )
298 for (;;) SleepEx( INFINITE, TRUE );
301 static void VGA_DeinstallTimer(void)
303 if (VGA_timer_thread)
306 * Make sure the update thread is not holding
307 * system resources when we kill it.
309 * Now, we only need to worry about update thread
310 * getting terminated while in EnterCriticalSection
311 * or WaitForMultipleObjectsEx.
313 * FIXME: Is this a problem?
315 EnterCriticalSection(&vga_lock);
317 CancelWaitableTimer( VGA_timer );
318 CloseHandle( VGA_timer );
319 TerminateThread( VGA_timer_thread, 0 );
320 CloseHandle( VGA_timer_thread );
321 VGA_timer_thread = 0;
323 LeaveCriticalSection(&vga_lock);
326 * Synchronize display. This makes sure that
327 * changes to display become visible even if program
328 * terminates before update thread had time to run.
330 VGA_Poll( 0, 0, 0 );
334 static void VGA_InstallTimer(unsigned Rate)
336 if (!VGA_timer_thread)
338 VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
339 VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
341 QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
344 static BOOL VGA_IsTimerRunning(void)
346 return VGA_timer_thread ? TRUE : FALSE;
349 HANDLE VGA_AlphaConsole(void)
351 /* this assumes that no Win32 redirection has taken place, but then again,
352 * only 16-bit apps are likely to use this part of Wine... */
353 return GetStdHandle(STD_OUTPUT_HANDLE);
356 char*VGA_AlphaBuffer(void)
358 return (char *)0xb8000;
361 /*** GRAPHICS MODE ***/
363 typedef struct {
364 unsigned Xres, Yres, Depth;
365 int ret;
366 } ModeSet;
369 /**********************************************************************
370 * VGA_SyncWindow
372 * Copy VGA window into framebuffer (if argument is TRUE) or
373 * part of framebuffer into VGA window (if argument is FALSE).
375 static void VGA_SyncWindow( BOOL target_is_fb )
377 int size = VGA_WINDOW_SIZE;
379 /* Window does not overlap framebuffer. */
380 if (vga_fb_window >= vga_fb_size)
381 return;
383 /* Check if window overlaps framebuffer only partially. */
384 if (vga_fb_size - vga_fb_window < VGA_WINDOW_SIZE)
385 size = vga_fb_size - vga_fb_window;
387 if (target_is_fb)
388 memmove( vga_fb_data + vga_fb_window, VGA_WINDOW_START, size );
389 else
390 memmove( VGA_WINDOW_START, vga_fb_data + vga_fb_window, size );
394 static void WINAPI VGA_DoExit(ULONG_PTR arg)
396 VGA_DeinstallTimer();
397 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
398 IDirectDrawSurface_Release(lpddsurf);
399 lpddsurf=NULL;
400 IDirectDrawPalette_Release(lpddpal);
401 lpddpal=NULL;
402 IDirectDraw_Release(lpddraw);
403 lpddraw=NULL;
406 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
408 LRESULT res;
409 ModeSet *par = (ModeSet *)arg;
410 par->ret=1;
412 if (lpddraw) VGA_DoExit(0);
413 if (!lpddraw) {
414 if (!pDirectDrawCreate)
416 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
417 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
418 if (!pDirectDrawCreate) {
419 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
420 return;
423 res = pDirectDrawCreate(NULL,&lpddraw,NULL);
424 if (!lpddraw) {
425 ERR("DirectDraw is not available (res = %lx)\n",res);
426 return;
428 if (!vga_hwnd) {
429 vga_hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",
430 WS_POPUP|WS_VISIBLE|SS_NOTIFY,0,0,
431 par->Xres,par->Yres,0,0,0,NULL);
432 if (!vga_hwnd) {
433 ERR("Failed to create user window.\n");
434 IDirectDraw_Release(lpddraw);
435 lpddraw=NULL;
436 return;
439 else
440 SetWindowPos(vga_hwnd,0,0,0,par->Xres,par->Yres,SWP_NOMOVE|SWP_NOZORDER);
442 if ((res=IDirectDraw_SetCooperativeLevel(lpddraw,vga_hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE))) {
443 ERR("Could not set cooperative level to exclusive (%lx)\n",res);
446 if ((res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth))) {
447 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par->Xres,par->Yres,par->Depth,res);
448 IDirectDraw_Release(lpddraw);
449 lpddraw=NULL;
450 return;
453 res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
454 if (res) {
455 ERR("Could not create palette (res = %lx)\n",res);
456 IDirectDraw_Release(lpddraw);
457 lpddraw=NULL;
458 return;
460 if ((res=IDirectDrawPalette_SetEntries(lpddpal,0,0,256,vga_def_palette))) {
461 ERR("Could not set default palette entries (res = %lx)\n", res);
464 memset(&sdesc,0,sizeof(sdesc));
465 sdesc.dwSize=sizeof(sdesc);
466 sdesc.dwFlags = DDSD_CAPS;
467 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
468 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
469 ERR("DirectDraw surface is not available\n");
470 IDirectDraw_Release(lpddraw);
471 lpddraw=NULL;
472 return;
474 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
475 vga_retrace_vertical = vga_retrace_horizontal = FALSE;
476 /* poll every 20ms (50fps should provide adequate responsiveness) */
477 VGA_InstallTimer(20);
479 par->ret=0;
480 return;
483 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
485 ModeSet par;
486 int newSize;
488 vga_fb_width = Xres;
489 vga_fb_height = Yres;
490 vga_fb_depth = Depth;
491 vga_fb_offset = 0;
492 vga_fb_pitch = Xres * ((Depth + 7) / 8);
494 newSize = Xres * Yres * ((Depth + 7) / 8);
495 if(newSize < 256 * 1024)
496 newSize = 256 * 1024;
498 if(vga_fb_size < newSize) {
499 HeapFree(GetProcessHeap(), 0, vga_fb_data);
500 vga_fb_data = HeapAlloc(GetProcessHeap(), 0, newSize);
501 vga_fb_size = newSize;
504 if(Xres >= 640 || Yres >= 480) {
505 par.Xres = Xres;
506 par.Yres = Yres;
507 } else {
508 par.Xres = 640;
509 par.Yres = 480;
512 VGA_SetWindowStart((Depth < 8) ? -1 : 0);
514 par.Depth = (Depth < 8) ? 8 : Depth;
516 MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
517 return par.ret;
520 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
522 if (!lpddraw) return 1;
523 if (!lpddsurf) return 1;
524 if (Height) *Height=sdesc.dwHeight;
525 if (Width) *Width=sdesc.dwWidth;
526 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
527 return 0;
530 void VGA_Exit(void)
532 if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
535 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
537 if (!lpddraw) return;
538 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
541 /* set a single [char wide] color in 16 color mode. */
542 void VGA_SetColor16(int reg,int color)
544 PALETTEENTRY *pal;
546 if (!lpddraw) return;
547 pal= &vga_def64_palette[color];
548 IDirectDrawPalette_SetEntries(lpddpal,0,reg,1,pal);
549 vga_16_palette[reg]=(char)color;
552 /* Get a single [char wide] color in 16 color mode. */
553 char VGA_GetColor16(int reg)
556 if (!lpddraw) return 0;
557 return (char)vga_16_palette[reg];
560 /* set all 17 [char wide] colors at once in 16 color mode. */
561 void VGA_Set16Palette(char *Table)
563 PALETTEENTRY *pal;
564 int c;
566 if (!lpddraw) return; /* return if we're in text only mode */
567 memcpy( Table, &vga_16_palette, 17 ); /* copy the entries into the table */
569 for (c=0; c<17; c++) { /* 17 entries */
570 pal= &vga_def64_palette[(int)vga_16_palette[c]]; /* get color */
571 IDirectDrawPalette_SetEntries(lpddpal,0,c,1,pal); /* set entry */
572 TRACE("Palette register %d set to %d\n",c,(int)vga_16_palette[c]);
573 } /* end of the counting loop */
576 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
577 void VGA_Get16Palette(char *Table)
580 if (!lpddraw) return; /* return if we're in text only mode */
581 memcpy( &vga_16_palette, Table, 17 ); /* copy the entries into the table */
584 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
586 PALETTEENTRY pal[256];
587 int c;
589 if (!lpddraw) return;
590 for (c=0; c<len; c++) {
591 pal[c].peRed =color[c].rgbRed;
592 pal[c].peGreen=color[c].rgbGreen;
593 pal[c].peBlue =color[c].rgbBlue;
594 pal[c].peFlags=0;
596 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
599 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
601 if (!lpddraw) return NULL;
602 if (!lpddsurf) return NULL;
603 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
604 ERR("could not lock surface!\n");
605 return NULL;
607 if (Pitch) *Pitch=sdesc.u1.lPitch;
608 if (Height) *Height=sdesc.dwHeight;
609 if (Width) *Width=sdesc.dwWidth;
610 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
611 return sdesc.lpSurface;
614 void VGA_Unlock(void)
616 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
620 * Set start of 64k window at 0xa0000 in bytes.
621 * If value is -1, initialize color plane support.
622 * If value is >= 0, window contains direct copy of framebuffer.
624 void VGA_SetWindowStart(int start)
626 if(start == vga_fb_window)
627 return;
629 EnterCriticalSection(&vga_lock);
631 if(vga_fb_window == -1)
632 FIXME("Remove VGA memory emulation.\n");
633 else
634 VGA_SyncWindow( TRUE );
636 vga_fb_window = start;
638 if(vga_fb_window == -1)
639 FIXME("Install VGA memory emulation.\n");
640 else
641 VGA_SyncWindow( FALSE );
643 LeaveCriticalSection(&vga_lock);
647 * Get start of 64k window at 0xa0000 in bytes.
648 * Value is -1 in color plane modes.
650 int VGA_GetWindowStart()
652 return vga_fb_window;
656 /**********************************************************************
657 * VGA_DoShowMouse
659 * Callback for VGA_ShowMouse.
661 static WINAPI void VGA_DoShowMouse( ULONG_PTR show )
663 INT rv;
667 rv = ShowCursor( show );
669 while( show ? (rv < 0) : (rv >= 0) );
673 /**********************************************************************
674 * VGA_ShowMouse
676 * If argument is TRUE, unconditionally show mouse cursor.
677 * If argument is FALSE, unconditionally hide mouse cursor.
678 * This only works in graphics mode.
680 void VGA_ShowMouse( BOOL show )
682 if (lpddraw)
683 MZ_RunInThread( VGA_DoShowMouse, (ULONG_PTR)show );
687 /*** TEXT MODE ***/
689 /* prepare the text mode video memory copy that is used to only
690 * update the video memory line that did get updated. */
691 void VGA_PrepareVideoMemCopy(unsigned Xres, unsigned Yres)
693 char *p, *p2;
694 unsigned int i;
697 * Allocate space for char + attr.
700 if (vga_text_old)
701 vga_text_old = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
702 vga_text_old, Xres * Yres * 2 );
703 else
704 vga_text_old = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
705 Xres * Yres * 2 );
706 p = VGA_AlphaBuffer();
707 p2 = vga_text_old;
709 /* make sure the video mem copy contains the exact opposite of our
710 * actual text mode memory area to make sure the screen
711 * does get updated fully initially */
712 for (i=0; i < Xres*Yres*2; i++)
713 *p2++ = *p++ ^ 0xff; /* XOR it */
716 /**********************************************************************
717 * VGA_SetAlphaMode
719 * Set VGA emulation to text mode.
721 void VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
723 VGA_Exit();
724 VGA_DeinstallTimer();
726 VGA_PrepareVideoMemCopy(Xres, Yres);
727 vga_text_width = Xres;
728 vga_text_height = Yres;
730 if (vga_text_x >= vga_text_width || vga_text_y >= vga_text_height)
731 VGA_SetCursorPos(0,0);
733 if(vga_text_console) {
734 COORD size;
735 size.X = Xres;
736 size.Y = Yres;
737 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size );
739 /* poll every 30ms (33fps should provide adequate responsiveness) */
740 VGA_InstallTimer(30);
744 /**********************************************************************
745 * VGA_InitAlphaMode
747 * Initialize VGA text mode handling and return default text mode.
748 * This function does not set VGA emulation to text mode.
750 void VGA_InitAlphaMode(unsigned*Xres,unsigned*Yres)
752 CONSOLE_SCREEN_BUFFER_INFO info;
754 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info ))
756 vga_text_console = TRUE;
757 vga_text_x = info.dwCursorPosition.X;
758 vga_text_y = info.dwCursorPosition.Y;
759 vga_text_attr = info.wAttributes;
760 *Xres = info.dwSize.X;
761 *Yres = info.dwSize.Y;
763 else
765 vga_text_console = FALSE;
766 vga_text_x = 0;
767 vga_text_y = 0;
768 vga_text_attr = 0x0f;
769 *Xres = 80;
770 *Yres = 25;
774 /**********************************************************************
775 * VGA_GetAlphaMode
777 * Get current text mode. Returns TRUE and sets resolution if
778 * any VGA text mode has been initialized.
780 BOOL VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
782 if (vga_text_width != 0 && vga_text_height != 0) {
783 *Xres = vga_text_width;
784 *Yres = vga_text_height;
785 return TRUE;
786 } else
787 return FALSE;
790 void VGA_SetCursorShape(unsigned char start_options, unsigned char end)
792 CONSOLE_CURSOR_INFO cci;
794 /* standard cursor settings:
795 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
797 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
798 cci.dwSize = ((end & 0x1f) - (start_options & 0x1f))/0x0e * 100;
799 if (!cci.dwSize) cci.dwSize++; /* NULL cursor would make SCCI() fail ! */
800 cci.bVisible = ((start_options & 0x60) != 0x20); /* invisible ? */
802 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci);
805 void VGA_SetCursorPos(unsigned X,unsigned Y)
807 vga_text_x = X;
808 vga_text_y = Y;
811 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
813 if (X) *X = vga_text_x;
814 if (Y) *Y = vga_text_y;
817 static void VGA_PutCharAt(unsigned x, unsigned y, BYTE ascii, int attr)
819 char *dat = VGA_AlphaBuffer() + ((vga_text_width * y + x) * 2);
820 dat[0] = ascii;
821 if (attr>=0)
822 dat[1] = attr;
825 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
827 EnterCriticalSection(&vga_lock);
829 while (count--)
830 VGA_PutCharAt(X + count, Y, ch, attr);
832 LeaveCriticalSection(&vga_lock);
835 void VGA_PutChar(BYTE ascii)
837 DWORD w;
839 EnterCriticalSection(&vga_lock);
841 switch(ascii) {
842 case '\b':
843 if (vga_text_x)
845 vga_text_x--;
846 VGA_PutCharAt(vga_text_x, vga_text_y, ' ', 0);
848 break;
850 case '\t':
851 vga_text_x += ((vga_text_x + 8) & ~7) - vga_text_x;
852 break;
854 case '\n':
855 vga_text_y++;
856 vga_text_x = 0;
857 break;
859 case '\a':
860 break;
862 case '\r':
863 vga_text_x = 0;
864 break;
866 default:
867 VGA_PutCharAt(vga_text_x, vga_text_y, ascii, vga_text_attr);
868 vga_text_x++;
871 if (vga_text_x >= vga_text_width)
873 vga_text_x = 0;
874 vga_text_y++;
877 if (vga_text_y >= vga_text_height)
879 vga_text_y = vga_text_height - 1;
880 VGA_ScrollUpText( 0, 0,
881 vga_text_height - 1, vga_text_width - 1,
882 1, vga_text_attr );
886 * If we don't have a console, write directly to standard output.
888 if(!vga_text_console)
889 WriteFile(VGA_AlphaConsole(), &ascii, 1, &w, NULL);
891 LeaveCriticalSection(&vga_lock);
894 void VGA_SetTextAttribute(BYTE attr)
896 vga_text_attr = attr;
899 void VGA_ClearText(unsigned row1, unsigned col1,
900 unsigned row2, unsigned col2,
901 BYTE attr)
903 unsigned x, y;
905 EnterCriticalSection(&vga_lock);
907 for(y=row1; y<=row2; y++)
908 for(x=col1; x<=col2; x++)
909 VGA_PutCharAt(x, y, 0x20, attr);
911 LeaveCriticalSection(&vga_lock);
914 void VGA_ScrollUpText(unsigned row1, unsigned col1,
915 unsigned row2, unsigned col2,
916 unsigned lines, BYTE attr)
918 char *buffer = VGA_AlphaBuffer();
919 unsigned y;
921 EnterCriticalSection(&vga_lock);
924 * Scroll buffer.
926 for (y = row1; y <= row2 - lines; y++)
927 memmove( buffer + col1 + y * vga_text_width * 2,
928 buffer + col1 + (y + lines) * vga_text_width * 2,
929 (col2 - col1 + 1) * 2 );
932 * Fill exposed lines.
934 for (y = max(row1, row2 - lines + 1); y <= row2; y++)
935 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
937 LeaveCriticalSection(&vga_lock);
940 void VGA_ScrollDownText(unsigned row1, unsigned col1,
941 unsigned row2, unsigned col2,
942 unsigned lines, BYTE attr)
944 char *buffer = VGA_AlphaBuffer();
945 unsigned y;
947 EnterCriticalSection(&vga_lock);
950 * Scroll buffer.
952 for (y = row2; y >= row1 + lines; y--)
953 memmove( buffer + col1 + y * vga_text_width * 2,
954 buffer + col1 + (y - lines) * vga_text_width * 2,
955 (col2 - col1 + 1) * 2 );
958 * Fill exposed lines.
960 for (y = row1; y <= min(row1 + lines - 1, row2); y++)
961 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
963 LeaveCriticalSection(&vga_lock);
966 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
968 char *dat;
970 dat = VGA_AlphaBuffer() + ((vga_text_width * vga_text_y + vga_text_x) * 2);
972 *ascii = dat[0];
973 *attr = dat[1];
977 /*** CONTROL ***/
979 /* FIXME: optimize by doing this only if the data has actually changed
980 * (in a way similar to DIBSection, perhaps) */
981 static void VGA_Poll_Graphics(void)
983 unsigned int Pitch, Height, Width, X, Y;
984 char *surf;
985 char *dat = vga_fb_data + vga_fb_offset;
986 int bpp = (vga_fb_depth + 7) / 8;
988 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
989 if (!surf) return;
992 * Synchronize framebuffer contents.
994 if (vga_fb_window != -1)
995 VGA_SyncWindow( TRUE );
998 * Double VGA framebuffer (320x200 -> 640x400), if needed.
1000 if(Height >= 2 * vga_fb_height && Width >= 2 * vga_fb_width && bpp == 1)
1001 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch*2,dat+=vga_fb_pitch)
1002 for (X=0; X<vga_fb_width; X++) {
1003 BYTE value = dat[X];
1004 surf[X*2] = value;
1005 surf[X*2+1] = value;
1006 surf[X*2+Pitch] = value;
1007 surf[X*2+Pitch+1] = value;
1009 else
1010 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch,dat+=vga_fb_pitch)
1011 memcpy(surf, dat, vga_fb_width * bpp);
1013 VGA_Unlock();
1016 static void VGA_Poll_Text(void)
1018 char *dat, *old, *p_line;
1019 unsigned int X, Y;
1020 CHAR_INFO ch[256]; /* that should suffice for the largest text width */
1021 COORD siz, off;
1022 SMALL_RECT dest;
1023 HANDLE con = VGA_AlphaConsole();
1024 BOOL linechanged = FALSE; /* video memory area differs from stored copy? */
1026 /* Synchronize cursor position. */
1027 off.X = vga_text_x;
1028 off.Y = vga_text_y;
1029 SetConsoleCursorPosition(con,off);
1031 dat = VGA_AlphaBuffer();
1032 old = vga_text_old; /* pointer to stored video mem copy */
1033 siz.X = vga_text_width; siz.Y = 1;
1034 off.X = 0; off.Y = 0;
1036 /* copy from virtual VGA frame buffer to console */
1037 for (Y=0; Y<vga_text_height; Y++) {
1038 linechanged = memcmp(dat, old, vga_text_width*2);
1039 if (linechanged)
1041 /*TRACE("line %d changed\n", Y);*/
1042 p_line = dat;
1043 for (X=0; X<vga_text_width; X++) {
1044 ch[X].Char.AsciiChar = *p_line++;
1045 /* WriteConsoleOutputA doesn't like "dead" chars */
1046 if (ch[X].Char.AsciiChar == '\0')
1047 ch[X].Char.AsciiChar = ' ';
1048 ch[X].Attributes = *p_line++;
1050 dest.Top=Y; dest.Bottom=Y;
1051 dest.Left=0; dest.Right=vga_text_width+1;
1052 WriteConsoleOutputA(con, ch, siz, off, &dest);
1053 memcpy(old, dat, vga_text_width*2);
1055 /* advance to next text line */
1056 dat += vga_text_width*2;
1057 old += vga_text_width*2;
1061 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
1063 EnterCriticalSection(&vga_lock);
1065 if (lpddraw)
1066 VGA_Poll_Graphics();
1067 else
1068 VGA_Poll_Text();
1071 * Fake start of retrace.
1073 vga_retrace_vertical = TRUE;
1075 LeaveCriticalSection(&vga_lock);
1078 static BYTE palreg,palcnt;
1079 static PALETTEENTRY paldat;
1081 void VGA_ioport_out( WORD port, BYTE val )
1083 switch (port) {
1084 case 0x3c0:
1085 if (vga_address_3c0)
1086 vga_index_3c0 = val;
1087 else
1088 FIXME("Unsupported index, register 0x3c0: 0x%02x (value 0x%02x)\n",
1089 vga_index_3c0, val);
1090 vga_address_3c0 = !vga_address_3c0;
1091 break;
1092 case 0x3c4:
1093 vga_index_3c4 = val;
1094 break;
1095 case 0x3c5:
1096 switch(vga_index_3c4) {
1097 case 0x04: /* Sequencer: Memory Mode Register */
1098 if(vga_fb_depth == 8)
1099 VGA_SetWindowStart((val & 8) ? 0 : -1);
1100 else
1101 FIXME("Memory Mode Register not supported in this mode.\n");
1102 break;
1103 default:
1104 FIXME("Unsupported index, register 0x3c4: 0x%02x (value 0x%02x)\n",
1105 vga_index_3c4, val);
1107 break;
1108 case 0x3c8:
1109 palreg=val; palcnt=0; break;
1110 case 0x3c9:
1111 ((BYTE*)&paldat)[palcnt++]=val << 2;
1112 if (palcnt==3) {
1113 VGA_SetPalette(&paldat,palreg++,1);
1114 palcnt=0;
1116 break;
1117 case 0x3ce:
1118 vga_index_3ce = val;
1119 break;
1120 case 0x3cf:
1121 FIXME("Unsupported index, register 0x3ce: 0x%02x (value 0x%02x)\n",
1122 vga_index_3ce, val);
1123 break;
1124 case 0x3d4:
1125 vga_index_3d4 = val;
1126 break;
1127 case 0x3d5:
1128 FIXME("Unsupported index, register 0x3d4: 0x%02x (value 0x%02x)\n",
1129 vga_index_3d4, val);
1130 break;
1131 default:
1132 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port, val);
1136 BYTE VGA_ioport_in( WORD port )
1138 BYTE ret;
1140 switch (port) {
1141 case 0x3c1:
1142 FIXME("Unsupported index, register 0x3c0: 0x%02x\n",
1143 vga_index_3c0);
1144 return 0xff;
1145 case 0x3c5:
1146 switch(vga_index_3c4) {
1147 case 0x04: /* Sequencer: Memory Mode Register */
1148 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1149 default:
1150 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1151 vga_index_3c4);
1152 return 0xff;
1154 case 0x3cf:
1155 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1156 vga_index_3ce);
1157 return 0xff;
1158 case 0x3d5:
1159 FIXME("Unsupported index, register 0x3d4: 0x%02x\n",
1160 vga_index_3d4);
1161 return 0xff;
1163 case 0x3da:
1165 * Read from this register resets register 0x3c0 address flip-flop.
1167 vga_address_3c0 = TRUE;
1170 * Read from this register returns following bits:
1171 * xxxx1xxx = Vertical retrace in progress if set.
1172 * xxxxx1xx = Light pen switched on.
1173 * xxxxxx1x = Light pen trigger set.
1174 * xxxxxxx1 = Either vertical or horizontal retrace
1175 * in progress if set.
1177 ret = 0;
1178 if (vga_retrace_vertical)
1179 ret |= 9;
1180 if (vga_retrace_horizontal)
1181 ret |= 3;
1184 * If VGA mode has been set, vertical retrace is
1185 * turned on once a frame and cleared after each read.
1186 * This might cause applications that synchronize with
1187 * vertical retrace to actually skip one frame but that
1188 * is probably not a problem.
1190 * If no VGA mode has been set, vertical retrace is faked
1191 * by toggling the value after every read.
1193 if (VGA_IsTimerRunning())
1194 vga_retrace_vertical = FALSE;
1195 else
1196 vga_retrace_vertical = !vga_retrace_vertical;
1199 * Toggle horizontal retrace.
1201 vga_retrace_horizontal = !vga_retrace_horizontal;
1202 break;
1204 default:
1205 ret=0xff;
1206 FIXME("Unsupported VGA register: 0x%04x\n", port);
1208 return ret;
1211 void VGA_Clean(void)
1213 VGA_Exit();
1214 VGA_DeinstallTimer();