Release 971116
[wine.git] / windows / user.c
blobf791b00cd26360402018aad84af34a34a4c3cc38
1 /*
2 * Misc. USER functions
4 * Copyright 1993 Robert J. Amstadt
5 * 1996 Alex Korobka
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "windows.h"
11 #include "resource.h"
12 #include "heap.h"
13 #include "gdi.h"
14 #include "user.h"
15 #include "task.h"
16 #include "queue.h"
17 #include "win.h"
18 #include "clipboard.h"
19 #include "hook.h"
20 #include "debug.h"
21 #include "toolhelp.h"
22 #include "message.h"
23 #include "shell.h"
25 WORD USER_HeapSel = 0;
27 extern BOOL32 MENU_PatchResidentPopup( HQUEUE16, WND* );
28 extern void QUEUE_FlushMessages(HQUEUE16);
30 /***********************************************************************
31 * GetFreeSystemResources (USER.284)
33 WORD WINAPI GetFreeSystemResources( WORD resType )
35 int userPercent, gdiPercent;
37 switch(resType)
39 case GFSR_USERRESOURCES:
40 userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
41 LOCAL_HeapSize( USER_HeapSel );
42 gdiPercent = 100;
43 break;
45 case GFSR_GDIRESOURCES:
46 gdiPercent = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
47 LOCAL_HeapSize( GDI_HeapSel );
48 userPercent = 100;
49 break;
51 case GFSR_SYSTEMRESOURCES:
52 userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
53 LOCAL_HeapSize( USER_HeapSel );
54 gdiPercent = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
55 LOCAL_HeapSize( GDI_HeapSel );
56 break;
58 default:
59 return 0;
61 return (WORD)MIN( userPercent, gdiPercent );
65 /***********************************************************************
66 * SystemHeapInfo (TOOLHELP.71)
68 BOOL16 WINAPI SystemHeapInfo( SYSHEAPINFO *pHeapInfo )
70 pHeapInfo->wUserFreePercent = GetFreeSystemResources( GFSR_USERRESOURCES );
71 pHeapInfo->wGDIFreePercent = GetFreeSystemResources( GFSR_GDIRESOURCES );
72 pHeapInfo->hUserSegment = USER_HeapSel;
73 pHeapInfo->hGDISegment = GDI_HeapSel;
74 return TRUE;
78 /***********************************************************************
79 * TimerCount (TOOLHELP.80)
81 BOOL16 WINAPI TimerCount( TIMERINFO *pTimerInfo )
83 /* FIXME
84 * In standard mode, dwmsSinceStart = dwmsThisVM
86 * I tested this, under Windows in enhanced mode, and
87 * if you never switch VM (ie start/stop DOS) these
88 * values should be the same as well.
90 * Also, Wine should adjust for the hardware timer
91 * to reduce the amount of error to ~1ms.
92 * I can't be bothered, can you?
94 pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
95 return TRUE;
98 static RESOURCEHANDLER16 __r16loader = NULL;
100 /**********************************************************************
101 * USER_CallDefaultRsrcHandler
103 * Called by the LoadDIBIcon/CursorHandler().
105 HGLOBAL16 USER_CallDefaultRsrcHandler( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
107 return __r16loader( hMemObj, hModule, hRsrc );
110 /**********************************************************************
111 * USER_InstallRsrcHandler
113 static void USER_InstallRsrcHandler( HINSTANCE16 hInstance )
115 FARPROC16 proc;
117 /* SetResourceHandler() returns previous function which is set
118 * when a module's resource table is loaded. */
120 proc = SetResourceHandler( hInstance, RT_ICON, (FARPROC32)LoadDIBIconHandler );
121 if(!__r16loader )
122 __r16loader = (RESOURCEHANDLER16)proc;
123 proc = SetResourceHandler( hInstance, RT_CURSOR, (FARPROC32)LoadDIBCursorHandler );
124 if(!__r16loader )
125 __r16loader = (RESOURCEHANDLER16)proc;
128 /**********************************************************************
129 * InitApp (USER.5)
131 INT16 WINAPI InitApp( HINSTANCE16 hInstance )
133 int queueSize;
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.
142 USER_InstallRsrcHandler( hInstance );
144 /* Create task message queue */
145 queueSize = GetProfileInt32A( "windows", "DefaultQueueSize", 8 );
146 if (!SetMessageQueue32( queueSize )) return 0;
148 return 1;
151 /**********************************************************************
152 * USER_ModuleUnload
154 static void USER_ModuleUnload( HMODULE16 hModule )
156 HOOK_FreeModuleHooks( hModule );
157 CLASS_FreeModuleClasses( hModule );
160 /**********************************************************************
161 * USER_AppExit
163 static void USER_AppExit( HTASK16 hTask, HINSTANCE16 hInstance, HQUEUE16 hQueue )
165 /* FIXME: empty clipboard if needed, maybe destroy menus (Windows
166 * only complains about them but does nothing);
169 WND* desktop = WIN_GetDesktop();
171 /* Patch desktop window */
172 if( desktop->hmemTaskQ == hQueue )
173 desktop->hmemTaskQ = GetTaskQueue(TASK_GetNextTask(hTask));
175 /* Patch resident popup menu window */
176 MENU_PatchResidentPopup( hQueue, NULL );
178 TIMER_RemoveQueueTimers( hQueue );
180 QUEUE_FlushMessages( hQueue );
181 HOOK_FreeQueueHooks( hQueue );
183 QUEUE_SetExitingQueue( hQueue );
184 WIN_ResetQueueWindows( desktop, hQueue, (HQUEUE16)0);
185 CLIPBOARD_ResetLock( hQueue, 0 );
186 QUEUE_SetExitingQueue( 0 );
188 /* Free the message queue */
190 QUEUE_DeleteMsgQueue( hQueue );
192 /* ModuleUnload() in "Internals" */
194 hInstance = GetExePtr( hInstance );
195 if( GetModuleUsage( hInstance ) <= 1 )
196 USER_ModuleUnload( hInstance );
200 /***********************************************************************
201 * USER_ExitWindows
203 * Clean-up everything and exit the Wine process.
204 * This is the back-end of ExitWindows(), called when all windows
205 * have agreed to be terminated.
207 void USER_ExitWindows(void)
209 /* Do the clean-up stuff */
211 WriteOutProfiles();
212 SHELL_SaveRegistry();
214 exit(0);
218 /***********************************************************************
219 * USER_SignalProc (USER.314)
221 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
222 UINT16 uExitFn, HINSTANCE16 hInstance,
223 HQUEUE16 hQueue )
225 switch( uCode )
227 case USIG_GPF:
228 case USIG_TERMINATION:
229 USER_AppExit( hTaskOrModule, hInstance, hQueue ); /* task */
230 break;
232 case USIG_DLL_LOAD:
233 USER_InstallRsrcHandler( hTaskOrModule ); /* module */
234 break;
236 case USIG_DLL_UNLOAD:
237 USER_ModuleUnload( hTaskOrModule ); /* module */
238 break;
240 default:
241 fprintf(stderr,"Unimplemented USER signal: %i\n", (int)uCode );
246 /***********************************************************************
247 * ExitWindows16 (USER.7)
249 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
251 return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
255 /***********************************************************************
256 * ExitWindowsExec16 (USER.246)
258 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
260 fprintf(stdnimp, "ExitWindowsExec() : Should run the following in DOS-mode :\n\t\"%s %s\"\n",
261 lpszExe, lpszParams);
262 return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
266 /***********************************************************************
267 * ExitWindowsEx (USER32.195)
269 BOOL32 WINAPI ExitWindowsEx( UINT32 flags, DWORD reserved )
271 int i;
272 BOOL32 result;
273 WND **list, **ppWnd;
275 /* We have to build a list of all windows first, as in EnumWindows */
277 if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL ))) return FALSE;
279 /* Send a WM_QUERYENDSESSION message to every window */
281 for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
283 /* Make sure that the window still exists */
284 if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
285 if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
286 break;
288 result = !(*ppWnd);
290 /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
292 for (ppWnd = list; i > 0; i--, ppWnd++)
294 if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
295 SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
297 HeapFree( SystemHeap, 0, list );
299 if (result) USER_ExitWindows();
300 return FALSE;