Release 980601
[wine] / windows / user.c
1 /*
2  * Misc. USER functions
3  *
4  * Copyright 1993 Robert J. Amstadt
5  *           1996 Alex Korobka 
6  */
7
8 #include <stdlib.h>
9 #include "windows.h"
10 #include "resource.h"
11 #include "heap.h"
12 #include "gdi.h"
13 #include "user.h"
14 #include "task.h"
15 #include "queue.h"
16 #include "win.h"
17 #include "clipboard.h"
18 #include "menu.h"
19 #include "hook.h"
20 #include "debug.h"
21 #include "toolhelp.h"
22 #include "message.h"
23 #include "module.h"
24 #include "miscemu.h"
25 #include "queue.h"
26 #include "shell.h"
27
28 /***********************************************************************
29  *           GetFreeSystemResources   (USER.284)
30  */
31 WORD WINAPI GetFreeSystemResources( WORD resType )
32 {
33     int userPercent, gdiPercent;
34
35     switch(resType)
36     {
37     case GFSR_USERRESOURCES:
38         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
39                                LOCAL_HeapSize( USER_HeapSel );
40         gdiPercent  = 100;
41         break;
42
43     case GFSR_GDIRESOURCES:
44         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
45                                LOCAL_HeapSize( GDI_HeapSel );
46         userPercent = 100;
47         break;
48
49     case GFSR_SYSTEMRESOURCES:
50         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
51                                LOCAL_HeapSize( USER_HeapSel );
52         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
53                                LOCAL_HeapSize( GDI_HeapSel );
54         break;
55
56     default:
57         return 0;
58     }
59     return (WORD)MIN( userPercent, gdiPercent );
60 }
61
62
63 /***********************************************************************
64  *           SystemHeapInfo   (TOOLHELP.71)
65  */
66 BOOL16 WINAPI SystemHeapInfo( SYSHEAPINFO *pHeapInfo )
67 {
68     pHeapInfo->wUserFreePercent = GetFreeSystemResources( GFSR_USERRESOURCES );
69     pHeapInfo->wGDIFreePercent  = GetFreeSystemResources( GFSR_GDIRESOURCES );
70     pHeapInfo->hUserSegment = USER_HeapSel;
71     pHeapInfo->hGDISegment  = GDI_HeapSel;
72     return TRUE;
73 }
74
75
76 /***********************************************************************
77  *           TimerCount   (TOOLHELP.80)
78  */
79 BOOL16 WINAPI TimerCount( TIMERINFO *pTimerInfo )
80 {
81     /* FIXME
82      * In standard mode, dwmsSinceStart = dwmsThisVM 
83      *
84      * I tested this, under Windows in enhanced mode, and
85      * if you never switch VM (ie start/stop DOS) these
86      * values should be the same as well. 
87      *
88      * Also, Wine should adjust for the hardware timer
89      * to reduce the amount of error to ~1ms. 
90      * I can't be bothered, can you?
91      */
92     pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
93     return TRUE;
94 }
95
96 static RESOURCEHANDLER16 __r16loader = NULL;
97
98 /**********************************************************************
99  *           USER_CallDefaultRsrcHandler
100  *
101  * Called by the LoadDIBIcon/CursorHandler().
102  */
103 HGLOBAL16 USER_CallDefaultRsrcHandler( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
104 {
105     return __r16loader( hMemObj, hModule, hRsrc );
106 }
107
108 /**********************************************************************
109  *           USER_InstallRsrcHandler
110  */
111 static void USER_InstallRsrcHandler( HINSTANCE16 hInstance )
112 {
113     FARPROC16 proc;
114
115     /* SetResourceHandler() returns previous function which is set
116      * when a module's resource table is loaded. */
117
118     proc = SetResourceHandler( hInstance, RT_ICON16,
119                                (FARPROC32)LoadDIBIconHandler );
120     if(!__r16loader ) 
121         __r16loader = (RESOURCEHANDLER16)proc;
122     proc = SetResourceHandler( hInstance, RT_CURSOR16,
123                                (FARPROC32)LoadDIBCursorHandler );
124     if(!__r16loader )
125         __r16loader = (RESOURCEHANDLER16)proc;
126 }
127
128 /**********************************************************************
129  *           InitApp   (USER.5)
130  */
131 INT16 WINAPI InitApp( HINSTANCE16 hInstance )
132 {
133     int queueSize;
134
135       /* InitTask() calls LibMain()'s of implicitly loaded DLLs 
136        * prior to InitApp() so there is no clean way to do
137        * SetTaskSignalHandler() in time. So, broken Windows bypasses 
138        * a pTask->userhandler on startup and simply calls a global 
139        * function pointer to the default USER signal handler.
140        */
141
142     USER_InstallRsrcHandler( hInstance );
143
144     /* Hack: restore the divide-by-zero handler */
145     /* FIXME: should set a USER-specific handler that displays a msg box */
146     INT_SetHandler( 0, INT_GetHandler( 0xff ) );
147
148       /* Create task message queue */
149     queueSize = GetProfileInt32A( "windows", "DefaultQueueSize", 8 );
150     if (!SetMessageQueue32( queueSize )) return 0;
151
152     return 1;
153 }
154
155 /**********************************************************************
156  *           USER_ModuleUnload
157  */
158 static void USER_ModuleUnload( HMODULE16 hModule )
159 {
160     HOOK_FreeModuleHooks( hModule );
161     CLASS_FreeModuleClasses( hModule );
162 }
163
164 /**********************************************************************
165  *           USER_AppExit
166  */
167 static void USER_AppExit( HTASK16 hTask, HINSTANCE16 hInstance, HQUEUE16 hQueue )
168 {
169     /* FIXME: empty clipboard if needed, maybe destroy menus (Windows
170      *        only complains about them but does nothing);
171      */
172
173     WND* desktop = WIN_GetDesktop();
174
175     /* Patch desktop window */
176     if( desktop->hmemTaskQ == hQueue )
177         desktop->hmemTaskQ = GetTaskQueue(TASK_GetNextTask(hTask));
178
179     /* Patch resident popup menu window */
180     MENU_PatchResidentPopup( hQueue, NULL );
181
182     TIMER_RemoveQueueTimers( hQueue );
183
184     QUEUE_FlushMessages( hQueue );
185     HOOK_FreeQueueHooks( hQueue );
186
187     QUEUE_SetExitingQueue( hQueue );
188     WIN_ResetQueueWindows( desktop, hQueue, (HQUEUE16)0);
189     CLIPBOARD_ResetLock( hQueue, 0 );
190     QUEUE_SetExitingQueue( 0 );
191
192     /* Free the message queue */
193
194     QUEUE_DeleteMsgQueue( hQueue );
195
196     /* ModuleUnload() in "Internals" */
197
198     hInstance = GetExePtr( hInstance );
199     if( GetModuleUsage( hInstance ) <= 1 ) 
200         USER_ModuleUnload( hInstance );
201 }
202
203
204 /***********************************************************************
205  *           USER_ExitWindows
206  *
207  * Clean-up everything and exit the Wine process.
208  * This is the back-end of ExitWindows(), called when all windows
209  * have agreed to be terminated.
210  */
211 void USER_ExitWindows(void)
212 {
213     /* Do the clean-up stuff */
214
215     WriteOutProfiles();
216     SHELL_SaveRegistry();
217
218     exit(0);
219 }
220
221
222 /***********************************************************************
223  *           USER_SignalProc (USER.314)
224  */
225 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
226                              UINT16 uExitFn, HINSTANCE16 hInstance,
227                              HQUEUE16 hQueue )
228 {
229     switch( uCode )
230     {
231         case USIG_GPF:
232         case USIG_TERMINATION:
233              USER_AppExit( hTaskOrModule, hInstance, hQueue ); /* task */
234              break;
235
236         case USIG_DLL_LOAD:
237              USER_InstallRsrcHandler( hTaskOrModule ); /* module */
238              break;
239
240         case USIG_DLL_UNLOAD:
241              USER_ModuleUnload( hTaskOrModule ); /* module */
242              break;
243
244         default:
245              FIXME(msg,"Unimplemented USER signal: %i\n", (int)uCode );
246     }
247 }
248
249
250 /***********************************************************************
251  *           ExitWindows16   (USER.7)
252  */
253 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
254 {
255     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
256 }
257
258
259 /***********************************************************************
260  *           ExitWindowsExec16   (USER.246)
261  */
262 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
263 {
264     TRACE(system, "Should run the following in DOS-mode: \"%s %s\"\n",
265         lpszExe, lpszParams);
266     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
267 }
268
269
270 /***********************************************************************
271  *           ExitWindowsEx   (USER32.196)
272  */
273 BOOL32 WINAPI ExitWindowsEx( UINT32 flags, DWORD reserved )
274 {
275     int i;
276     BOOL32 result;
277     WND **list, **ppWnd;
278         
279     /* We have to build a list of all windows first, as in EnumWindows */
280
281     if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL ))) return FALSE;
282
283     /* Send a WM_QUERYENDSESSION message to every window */
284
285     for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
286     {
287         /* Make sure that the window still exists */
288         if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
289         if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
290             break;
291     }
292     result = !(*ppWnd);
293
294     /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
295
296     for (ppWnd = list; i > 0; i--, ppWnd++)
297     {
298         if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
299         SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
300     }
301     HeapFree( SystemHeap, 0, list );
302
303     if (result) USER_ExitWindows();
304     return FALSE;
305 }
306
307 /***********************************************************************
308  *           EnumDisplaySettingsA   (USER32.592)
309  */
310 BOOL32 WINAPI EnumDisplaySettings32A(LPCSTR name,DWORD n,LPDEVMODE32A devmode) {
311         TRACE(system,"(%s,%ld,%p)\n",name,n,devmode);
312         if (n==0) {
313                 devmode->dmBitsPerPel = DefaultDepthOfScreen(screen);
314                 devmode->dmPelsHeight = screenHeight;
315                 devmode->dmPelsWidth = screenWidth;
316                 return TRUE;
317         }
318         return FALSE;
319 }
320
321
322 /***********************************************************************
323  *           SetEventHook   (USER.321)
324  *
325  *      Used by Turbo Debugger for Windows
326  */
327 FARPROC16 SetEventHook(FARPROC16 lpfnEventHook)
328 {
329         FIXME(hook, "(lpfnEventHook=%08x): stub\n", (UINT32)lpfnEventHook);
330         return NULL;
331 }
332
333 /***********************************************************************
334  *           UserSeeUserDo   (USER.216)
335  */
336 DWORD WINAPI UserSeeUserDo(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
337 {
338     switch (wReqType)
339     {
340     case USUD_LOCALALLOC:
341         return LOCAL_Alloc(USER_HeapSel, wParam1, wParam3);
342     case USUD_LOCALFREE:
343         return LOCAL_Free(USER_HeapSel, wParam1);
344     case USUD_LOCALCOMPACT:
345         return LOCAL_Compact(USER_HeapSel, wParam3, 0);
346     case USUD_LOCALHEAP:
347         return USER_HeapSel;
348     case USUD_FIRSTCLASS:
349         FIXME(local, "return a pointer to the first window class.\n"); 
350         return (DWORD)-1;
351     default:
352         WARN(local, "wReqType %04x (unknown)", wReqType);
353         return (DWORD)-1;
354     }
355 }
356