Release 960928

Fri Sep 27 14:18:42 1996  Alexandre Julliard  <julliard@lrc.epfl.ch>

	* [controls/button.c]
	Fixed focus rectangle size and clipping.

	* [controls/scroll.c]
	Converted to Win32 and added support for scroll page.
	Completed SetScrollInfo() and implemented other Win32 functions.

	* [files/file.c]
	Removed FILE_Read() (use _lread32 instead).

	* [objects/dce.c] [include/dce.h]
	Allocate DCE on the Win32 heap, and use pointers instead of
	handles.
	Implemented Win32 version of DC functions.

	* [windows/painting.c]
	Attempt to make CS_PARENTDC style work again.

Wed Sep 25 23:40:52 1996 Alex Korobka <alex@trantor.pharm.sunysb.edu>

	* [windows/dce.c] [windows/winpos.c]
	Override SaveUnder attribute when painting took place
	in a window below. Force X to raise activated window 
	in seamless mode.

	* [misc/clipboard.c] [windows/event.c]
	Translation between DOS and Unix text formats and several
	other fixes for the sudden selection loss.

	* [windows/message.c]
	Apply "first" and "last" when checking for WM_QUIT in 
        MSG_PeekMessage().

	* [windows/win.c]
	Rearranged DestroyWindow() to fit "Windows Internals"
	description.

	* [windows/win.c] [windows/winpos.c] [windows/nonclient.c]
	Misc. fixes to CBT hook calls.

	* [controls/menu.c] [misc/user.c]
	Fixup resident popup menu window so that it doesn't get
	destroyed by USER_AppExit().

	* [loader/module.c] [loader/task.c] [windows/event.c]
	Process "unsafe" X events outside the scheduler to prevent
	deadlocks.

	* [windows/message.c] [windows/queue.c] [windows/winpos.c]
	Lots of fixes for better Win16 multitasking.

Wed Sep 25 20:36:30 1996  Marcus Meissner <msmeissn@cip.informatik.uni-erlangen.de>

	* [include/windows.h]
	Added some missing HOOK defines.

	* [misc/shell.c][if1632/shell32.spec][include/shell.h]
	SHGetFileInfoA stub added (win95 mplayer.exe /play bla.avi).

	* [win32/console.c][include/wincon.h]
	GetConsoleScreenBufferInfo, GetLargestConsoleWindowSize added.

	* [misc/registry.c]
	Some null ptr fixes.

	* [loader/pe_image.c]
	Fixed exported function lookup. (msvcrt20.dll)
	Add debugsyms for entrypoint, exported functions and sections.

	* [multimedia/mmsystem.c]
	MCIOpen: support for element opens (mplayer.exe /play bla.avi).

	* [several]
	Added several missing things/stubs/simple thunks from win32
	to win16 code.

Sat Sep 21 17:27:44 1996  O.Flebbe  <flebbe@science-computing.uni-tuebingen.de>

	* [windows/property.c]
	Fixed debugging of 16 Bit RemoveProp().

	* [debugger/memory.c]
	Added DEBUG_checkmap_bad() for linux.

Thu Sep 19 20:48:31 1996  Albrecht Kleine  <kleine@ak.sax.de>

	* [windows/event.c] [windows/hook.c]
	Use EnableHardwareInput() for JournalPlayback hook.

	* [controls/listbox.c]
	Changed handling of LB_GETITEMRECT in empty listboxes.

Thu Sep 19 13:34:35 1996  Slaven Rezic  <eserte@cs.tu-berlin.de>

	* [misc/main.c]
	Fixes to X resources handling.	

Wed Sep 18 00:31:15 1996  Huw D. M. Davies <h.davies1@physics.oxford.ac.uk>

	* [objects/metafile.c] [include/gdi.h] [objects/dc.c]
	Individual handle table created for each metafile. Fixed
 	GlobalReAlloc() bug in MF_AddHandleDC() (was MF_AddHandleInternal).

	* [windows/graphics.c] [objects/dc.c]
	Rectangle() changed to work better with wide pens and PS_NULL.
	Use JoinMiter.

	* [windows/winpos.c]
	Make the whole (non X) window invalid on resize if CS_[VH]REDRAW
 	is set.

	* [windows/nonclient.c]
	AdjustWindowRectEx() should perform calculations even if the
 	window is minimized.

	* [windows/mdi.c]
	Better handling of system button painting. Maximized windows can
 	contain scroll bars. Icons now maximize properly.

	* [windows/defwnd.c] [windows/nonclient.c] [controls/menu.c]
	Improved greying of items in system menu. WM_INITMEMUPOPUP no
 	longer caught in DefWndProc, DEFWND_InitSysMenuPopup moved to
 	menu.c.

Mon Sep 16 21:30:00 1996  Uwe Bonnes <bon@elektron.ikp.physik.th-darmstadt.de>

	* [several files]
	Fix missing includes and wrong printing arguments.

	* [controls/listbox.c]
	Don't sort drives in ListBoxDirectory().
	
Sat Sep 14 09:05:47 1996  Petri Tuomola <ptuomola@xs4all.nl>

	* [windows/dialog.c]
	Fixed handling of Shift-TAB in dialogs.

Thu Sep 12 18:31:00 1996  Thomas Sandford <t.d.g.sandford@prds-grn.demon.co.uk>

	* [if1632/gdi32.spec]
	Added SelectClipRgn - call win16 version.

	* [if1632/user32.spec]
	Added GetAsyncKeyState, GetMenuItemID and GetMenuStringA.

	* [include/wincon.h]
	Added COORD and SMALL_RECT typedefs, moved CONSOLE_SCREEN_BUFFER_INFO
	out of #if 0 protected portion of file.

	* [loader/pe_image.c]
	PE_InitTEB() - Tidy up, bug fix to stack pointer value (Borland
	programs now work better)

	* [win32/console.c]
	Added stub functions for GetConsoleScreenBufferInfo and 
	GetLargestConsoleWindowSize

	* [win32/findfile.c]
	FindFirstFile32A() - removed erroneous strcpy

	* [windows/keyboard.c]
	GetAsyncKeyState() - bug fix - now returns value as per Microsoft
	specification. NB - I still have doubts about some other functions
	in this file.
diff --git a/loader/builtin.c b/loader/builtin.c
index 00571c5..0b67254 100644
--- a/loader/builtin.c
+++ b/loader/builtin.c
@@ -145,7 +145,7 @@
     { &W32SYS_Descriptor,   0 },
     /* Win32 DLLs */
     { &ADVAPI32_Descriptor, 0 },
-    { &COMCTL32_Descriptor, 0 },
+    { &COMCTL32_Descriptor, DLL_FLAG_NOT_USED },
     { &COMDLG32_Descriptor, 0 },
     { &CRTDLL_Descriptor,   0 },
     { &OLE32_Descriptor,    0 },
diff --git a/loader/module.c b/loader/module.c
index f8074df..c5ec62b 100644
--- a/loader/module.c
+++ b/loader/module.c
@@ -459,15 +459,15 @@
          ((offset)+(size) <= fastload_offset+fastload_length)) ? \
         (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
         (_llseek( hFile, mz_header.ne_offset+(offset), SEEK_SET), \
-         FILE_Read( hFile, (buffer), (size) ) == (size)))
+         _lread32( hFile, (buffer), (size) ) == (size)))
 
     _llseek( hFile, 0, SEEK_SET );
-    if ((FILE_Read(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
+    if ((_lread32(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
         (mz_header.mz_magic != MZ_SIGNATURE))
         return (HMODULE16)11;  /* invalid exe */
 
     _llseek( hFile, mz_header.ne_offset, SEEK_SET );
-    if (FILE_Read( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
+    if (_lread32( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
         return (HMODULE16)11;  /* invalid exe */
 
     if (ne_header.ne_magic == PE_SIGNATURE) return (HMODULE16)21;  /* win32 exe */
@@ -517,7 +517,7 @@
         if ((fastload = (char *)malloc( fastload_length )) != NULL)
         {
             _llseek( hFile, mz_header.ne_offset + fastload_offset, SEEK_SET );
-            if (FILE_Read( hFile, fastload, fastload_length ) != fastload_length)
+            if (_lread32(hFile, fastload, fastload_length) != fastload_length)
             {
                 free( fastload );
                 fastload = NULL;
@@ -632,7 +632,7 @@
         }
         buffer = GlobalLock16( pModule->nrname_handle );
         _llseek( hFile, ne_header.nrname_tab_offset, SEEK_SET );
-        if (FILE_Read( hFile, buffer, ne_header.nrname_tab_length )
+        if (_lread32( hFile, buffer, ne_header.nrname_tab_length )
               != ne_header.nrname_tab_length)
         {
             GlobalFree16( pModule->nrname_handle );
@@ -1165,7 +1165,6 @@
 		  IF1632_Stack32_base = WIN16_GlobalLock16(hInitialStack32);
 
 		}
-                /* FIXME: we probably need a DOS handle here */
                 hf = FILE_DupUnixHandle( MODULE_OpenFile( hModule ) );
 		CallTo16_word_ww( selfloadheader->BootApp, hModule, hf );
                 _lclose(hf);
@@ -1215,6 +1214,7 @@
 
     if (!(pModule->flags & NE_FFLAGS_LIBMODULE) && (paramBlock != (LPVOID)-1))
     {
+	HTASK16 hTask;
         WORD	showcmd;
 
 	/* PowerPoint passes NULL as showCmd */
@@ -1222,10 +1222,13 @@
 		showcmd = *((WORD *)PTR_SEG_TO_LIN(params->showCmd)+1);
 	else
 		showcmd = 0; /* FIXME: correct */
-        TASK_CreateTask( hModule, hInstance, hPrevInstance,
-                         params->hEnvironment,
-                         (LPSTR)PTR_SEG_TO_LIN( params->cmdLine ),
-                         showcmd );
+
+        hTask = TASK_CreateTask( hModule, hInstance, hPrevInstance,
+                                 params->hEnvironment,
+                                (LPSTR)PTR_SEG_TO_LIN( params->cmdLine ),
+                                 showcmd );
+
+	if( hTask && TASK_GetNextTask(hTask)) Yield();
     }
 
     return hInstance;
@@ -1479,14 +1482,8 @@
 #if 0
     if (handle < (HANDLE)32)	/* Error? */
 	return handle;
-    
-/* FIXME: Yield never returns! 
-   We may want to run more applications or start the debugger
-   before calling Yield. If we don't Yield will be called immdiately
-   after returning.  Why is it needed for Word anyway? */
-    Yield();	/* program is executed immediately ....needed for word */
-
 #endif
+
     return handle;
 }
 
diff --git a/loader/pe_image.c b/loader/pe_image.c
index 6514c18..8ab35e9 100644
--- a/loader/pe_image.c
+++ b/loader/pe_image.c
@@ -2,6 +2,7 @@
 /* 
  *  Copyright	1994	Eric Youndale & Erik Bos
  *  Copyright	1995	Martin von Löwis
+ *  Copyright   1996    Marcus Meissner
  *
  *	based on Eric Youndale's pe-test and:
  *
@@ -30,6 +31,7 @@
 #include "registers.h"
 #include "stddebug.h"
 #include "debug.h"
+#include "debugger.h"
 #include "xmalloc.h"
 
 void my_wcstombs(char * result, u_short * source, int len)
@@ -77,34 +79,53 @@
 
 void dump_exports(struct PE_Export_Directory * pe_exports, unsigned int load_addr)
 { 
-  char * Module;
-  int i;
-  u_short * ordinal;
-  u_long * function;
-  u_char ** name, *ename;
+  char		*Module;
+  int		i;
+  u_short	*ordinal;
+  u_long	*function,*functions;
+  u_char	**name,*ename;
+  char		buffer[1000];
+  DBG_ADDR	daddr;
 
-  Module = ((char *) load_addr) + pe_exports->Name;
+  daddr.seg = 0;
+  Module = ((char*)load_addr)+pe_exports->Name;
   dprintf_win32(stddeb,"\n*******EXPORT DATA*******\nModule name is %s, %ld functions, %ld names\n", 
 	 Module,
 	 pe_exports->Number_Of_Functions,
 	 pe_exports->Number_Of_Names);
 
-  ordinal = (u_short *) (((char *) load_addr) + (int) pe_exports->Address_Of_Name_Ordinals);
-  function = (u_long *)  (((char *) load_addr) + (int) pe_exports->AddressOfFunctions);
-  name = (u_char **)  (((char *) load_addr) + (int) pe_exports->AddressOfNames);
+  ordinal=(u_short*)(((char*)load_addr)+(int)pe_exports->Address_Of_Name_Ordinals);
+  functions=function=(u_long*)(((char*)load_addr)+(int)pe_exports->AddressOfFunctions);
+  name=(u_char**)(((char*)load_addr)+(int)pe_exports->AddressOfNames);
 
   dprintf_win32(stddeb,"%-32s Ordinal Virt Addr\n", "Function Name");
-  for(i=0; i< pe_exports->Number_Of_Functions; i++) {
+  for (i=0;i<pe_exports->Number_Of_Functions;i++) {
       if (i<pe_exports->Number_Of_Names) {
-	  ename =  (char *) (((char *) load_addr) + (int) *name++);
-	  dprintf_win32(stddeb,"%-32s %4d    %8.8lx\n", ename, *ordinal++, *function++);
+	  ename=(char*)(((char*)load_addr)+(int)*name++);
+	  dprintf_win32(stddeb,"%-32s %4d    %8.8lx (%8.8lx)\n",ename,*ordinal,functions[*ordinal],*function);
+	  sprintf(buffer,"%s.%s",Module,ename);
+	  daddr.off=load_addr+functions[*ordinal];
+	  ordinal++;
+	  function++;
       } else {
       	  /* ordinals/names no longer valid, but we still got functions */
-	  dprintf_win32(stddeb,"%-32s %4s    %8.8lx\n","","",*function++);
+	  dprintf_win32(stddeb,"%-32s %4s    %8s %8.8lx\n","","","",*function);
+	  sprintf(buffer,"%s.%d",Module,i);
+	  daddr.off=load_addr+*functions;
+	  function++;
       }
+      DEBUG_AddSymbol(buffer,&daddr);
   }
 }
 
+/* Look up the specified function or ordinal in the exportlist:
+ * If it is a string:
+ * 	- look up the name in the Name list. 
+ *	- look up the ordinal with that index.
+ *	- use the ordinal as offset into the functionlist
+ * If it is a ordinal:
+ *	- use ordinal-pe_export->Base as offset into the functionlist
+ */
 FARPROC32 PE_FindExportedFunction(struct pe_data *pe, LPCSTR funcName)
 {
 	struct PE_Export_Directory * exports = pe->pe_export;
@@ -114,32 +135,29 @@
 	u_char ** name, *ename;
 	int i;
 
+	if (HIWORD(funcName))
+		dprintf_win32(stddeb,"PE_FindExportedFunction(%s)\n",funcName);
+	else
+		dprintf_win32(stddeb,"PE_FindExportedFunction(%d)\n",(int)funcName);
 	if (!exports)
 		return NULL;
 	ordinal=(u_short*)(((char*)load_addr)+(int)exports->Address_Of_Name_Ordinals);
 	function=(u_long*)(((char*)load_addr)+(int)exports->AddressOfFunctions);
 	name=(u_char **)(((char*)load_addr)+(int)exports->AddressOfNames);
 	if (HIWORD(funcName)) {
-		for(i=0; i<exports->Number_Of_Names; i++)
-		{
-			ename =  (char *) (((char *) load_addr) + (int) *name);
-			if(strcmp(ename,funcName)==0)
-				return (FARPROC32)(load_addr + *function);
-			function++;
+		for(i=0; i<exports->Number_Of_Names; i++) {
+			ename=(char*)(((char*)load_addr)+(int)*name);
+			if(!strcmp(ename,funcName))
+				return (FARPROC32)(load_addr+function[*ordinal]);
+			ordinal++;
 			name++;
 		}
 	} else {
-		/* if we got no name directory, we use the ordinal as offset */
-		if (!exports->Number_Of_Names) {
-			i = function[(int)funcName-exports->Base];
-			return (FARPROC32)(load_addr+i);
+		if (funcName-exports->Base > exports->Number_Of_Functions) {
+			dprintf_win32(stddeb,"	ordinal %d out of range!\n",funcName);
+			return NULL;
 		}
-		for(i=0; i<exports->Number_Of_Names; i++) {
-			if((int)funcName == (int)*ordinal + exports->Base)
-				return (FARPROC32)(load_addr + *function);
-			function++;
-			ordinal++;
-		}
+		return (FARPROC32)(load_addr+function[(int)funcName-exports->Base]);
 	}
 	return NULL;
 }
@@ -357,6 +375,10 @@
     int i, result;
     unsigned int load_addr;
     struct Directory dir;
+    char	buffer[200];
+    DBG_ADDR	daddr;
+
+	daddr.seg=0;
 
 	pe = xmalloc(sizeof(struct pe_data));
 	memset(pe,0,sizeof(struct pe_data));
@@ -511,9 +533,28 @@
 		dprintf_win32(stdnimp,"Callback directory ignored\n");
 
 
+	if(pe->pe_reloc) do_relocations(pe);
 	if(pe->pe_import) fixup_imports(pe, hModule);
 	if(pe->pe_export) dump_exports(pe->pe_export,load_addr);
-	if(pe->pe_reloc) do_relocations(pe);
+  		
+	if (pe->pe_export) {
+		/* add start of sections as debugsymbols */
+		for(i=0;i<pe->pe_header->coff.NumberOfSections;i++) {
+			sprintf(buffer,"%s.%s",
+				((char*)load_addr)+pe->pe_export->Name,
+				pe->pe_seg[i].Name
+			);
+			daddr.off=load_addr+pe->pe_seg[i].Virtual_Address;
+			DEBUG_AddSymbol(buffer,&daddr);
+		}
+		/* add entry point */
+		sprintf(buffer,"%s.EntryPoint",((char*)load_addr)+pe->pe_export->Name);
+		daddr.off=load_addr+pe->pe_header->opt_coff.AddressOfEntryPoint;
+		DEBUG_AddSymbol(buffer,&daddr);
+		/* add start of DLL */
+		daddr.off=load_addr;
+		DEBUG_AddSymbol(((char*)load_addr)+pe->pe_export->Name,&daddr);
+	}
         return pe;
 }
 
@@ -607,7 +648,7 @@
         printf("InitPEDLL() called!\n");
         CallDLLEntryProc32( (FARPROC32)(pe->load_addr + 
                                   pe->pe_header->opt_coff.AddressOfEntryPoint),
-                            hModule, DLL_PROCESS_ATTACH, 0 );
+                            hModule, DLL_PROCESS_ATTACH, (void *)-1 );
     }
 }
 
@@ -631,8 +672,8 @@
     TEB *pTEB;
 
     pTask = (TDB *)(GlobalLock16(GetCurrentTask() & 0xffff));
-    pTEB = (TEB *)(PTR_SEG_OFF_TO_LIN(hTEB, 0));
-    pTEB->stack = (void *)(GlobalLock16(pTask->hStack32));
+    pTEB  = (TEB *)(GlobalLock16(hTEB));
+    pTEB->stack = pTask->esp;
     pTEB->Except = (void *)(-1); 
     pTEB->TEBDSAlias = pTEB;
     pTEB->taskid = getpid();
diff --git a/loader/task.c b/loader/task.c
index cd58f36..0983041 100644
--- a/loader/task.c
+++ b/loader/task.c
@@ -671,18 +671,42 @@
     Yield();
     /* We should never return from this Yield() */
 
-    fprintf(stderr,"It's alive! Alive!!!\n");
+    fprintf(stderr,"Return of the living dead %04x!!!\n", hCurrentTask);
     exit(1);
 }
 
+/***********************************************************************
+ *           TASK_YieldToSystem
+ *
+ * Scheduler interface, this way we ensure that all "unsafe" events are
+ * processed outside the scheduler.
+ */
+void TASK_YieldToSystem(TDB* pTask)
+{
+  MESSAGEQUEUE*		pQ;
+
+  TASK_SCHEDULE();
+
+  if( pTask )
+  {
+    pQ = (MESSAGEQUEUE*)GlobalLock16(pTask->hQueue);
+    if( pQ && pQ->flags & QUEUE_FLAG_XEVENT )
+    {
+      pQ->flags &= ~QUEUE_FLAG_XEVENT;
+      EVENT_WaitXEvent( FALSE, FALSE );
+    }
+  }
+}
 
 /***********************************************************************
  *           TASK_Reschedule
  *
  * This is where all the magic of task-switching happens!
  *
- * This function should only be called via the TASK_SCHEDULE() macro, to make
- * sure that all the context is saved correctly.
+ * Note: This function should only be called via the TASK_YieldToSystem()
+ *       wrapper, to make sure that all the context is saved correctly.
+ *   
+ *       It must not call functions that may yield control.
  */
 void TASK_Reschedule(void)
 {
@@ -700,22 +724,22 @@
         hTaskToKill = 0;
     }
 
-    /* Flush any X events that happened in the meantime */
-
-    EVENT_WaitXEvent( FALSE );
-
     /* Find a task to yield to */
 
     pOldTask = (TDB *)GlobalLock16( hCurrentTask );
     if (pOldTask && pOldTask->hYieldTo)
     {
-        /* If a task is stored in hYieldTo of the current task (put there */
-        /* by DirectedYield), yield to it only if it has events pending.  */
+        /* check for DirectedYield() */
+
         hTask = pOldTask->hYieldTo;
         if (!(pNewTask = (TDB *)GlobalLock16( hTask )) || !pNewTask->nEvents)
             hTask = 0;
     }
 
+    /* extract hardware events only! */
+
+    EVENT_WaitXEvent( FALSE, TRUE );
+
     while (!hTask)
     {
         /* Find a task that has an event pending */
@@ -724,6 +748,9 @@
         while (hTask)
         {
             pNewTask = (TDB *)GlobalLock16( hTask );
+
+	    dprintf_task( stddeb, "\ttask = %04x, events = %i\n", hTask, pNewTask->nEvents);
+
             if (pNewTask->nEvents) break;
             hTask = pNewTask->hNext;
         }
@@ -732,11 +759,14 @@
 
         /* No task found, wait for some events to come in */
 
-        EVENT_WaitXEvent( TRUE );
+        EVENT_WaitXEvent( TRUE, TRUE );
     }
 
-    if (hTask == hCurrentTask) return;  /* Nothing to do */
-
+    if (hTask == hCurrentTask) 
+    {
+       dprintf_task( stddeb, "returning to the current task(%04x)\n", hTask );
+       return;  /* Nothing to do */
+    }
     pNewTask = (TDB *)GlobalLock16( hTask );
     dprintf_task( stddeb, "Switching to task %04x (%.8s)\n",
                   hTask, pNewTask->module_name );
@@ -843,10 +873,11 @@
         pTask->nEvents--;
         return FALSE;
     }
-    TASK_SCHEDULE();
+    TASK_YieldToSystem(pTask);
+
     /* When we get back here, we have an event */
+
     if (pTask->nEvents > 0) pTask->nEvents--;
-    else fprintf( stderr, "WaitEvent: reschedule returned without event\n" );
     return TRUE;
 }
 
@@ -914,7 +945,7 @@
 
     pCurTask = (TDB *)GlobalLock16( hCurrentTask );
     if (pCurTask) pCurTask->nEvents++;  /* Make sure we get back here */
-    TASK_SCHEDULE();
+    TASK_YieldToSystem(pCurTask);
     if (pCurTask) pCurTask->nEvents--;
 }