Optimized include/*.h: (recursively) include all headers needed by
[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 "heap.h"
11 #include "gdi.h"
12 #include "user.h"
13 #include "task.h"
14 #include "queue.h"
15 #include "win.h"
16 #include "clipboard.h"
17 #include "menu.h"
18 #include "hook.h"
19 #include "debug.h"
20 #include "toolhelp.h"
21 #include "message.h"
22 #include "module.h"
23 #include "miscemu.h"
24 #include "shell.h"
25 #include "callback.h"
26 #include "local.h"
27 #include "class.h"
28 #include "desktop.h"
29
30 /***********************************************************************
31  *           GetFreeSystemResources   (USER.284)
32  */
33 WORD WINAPI GetFreeSystemResources( WORD resType )
34 {
35     int userPercent, gdiPercent;
36
37     switch(resType)
38     {
39     case GFSR_USERRESOURCES:
40         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
41                                LOCAL_HeapSize( USER_HeapSel );
42         gdiPercent  = 100;
43         break;
44
45     case GFSR_GDIRESOURCES:
46         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
47                                LOCAL_HeapSize( GDI_HeapSel );
48         userPercent = 100;
49         break;
50
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;
57
58     default:
59         return 0;
60     }
61     return (WORD)MIN( userPercent, gdiPercent );
62 }
63
64
65 /***********************************************************************
66  *           SystemHeapInfo   (TOOLHELP.71)
67  */
68 BOOL16 WINAPI SystemHeapInfo( SYSHEAPINFO *pHeapInfo )
69 {
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;
75 }
76
77
78 /***********************************************************************
79  *           TimerCount   (TOOLHELP.80)
80  */
81 BOOL16 WINAPI TimerCount( TIMERINFO *pTimerInfo )
82 {
83     /* FIXME
84      * In standard mode, dwmsSinceStart = dwmsThisVM 
85      *
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. 
89      *
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?
93      */
94     pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
95     return TRUE;
96 }
97
98 static FARPROC16 __r16loader = NULL;
99
100 /**********************************************************************
101  *           USER_CallDefaultRsrcHandler
102  *
103  * Called by the LoadDIBIcon/CursorHandler().
104  */
105 HGLOBAL16 USER_CallDefaultRsrcHandler( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
106 {
107     return Callbacks->CallResourceHandlerProc( __r16loader, hMemObj, hModule, hRsrc );
108 }
109
110 /**********************************************************************
111  *           USER_InstallRsrcHandler
112  */
113 static void USER_InstallRsrcHandler( HINSTANCE16 hInstance )
114 {
115     FARPROC16 proc;
116
117     /* SetResourceHandler() returns previous function which is set
118      * when a module's resource table is loaded. */
119
120     proc = SetResourceHandler( hInstance, RT_ICON16,
121                                MODULE_GetWndProcEntry16("LoadDIBIconHandler") );
122     if (!__r16loader) __r16loader = proc;
123
124     proc = SetResourceHandler( hInstance, RT_CURSOR16,
125                                MODULE_GetWndProcEntry16("LoadDIBCursorHandler") );
126     if (!__r16loader) __r16loader = proc;
127 }
128
129 /**********************************************************************
130  *           InitApp   (USER.5)
131  */
132 INT16 WINAPI InitApp( HINSTANCE16 hInstance )
133 {
134       /* InitTask() calls LibMain()'s of implicitly loaded DLLs 
135        * prior to InitApp() so there is no clean way to do
136        * SetTaskSignalHandler() in time. So, broken Windows bypasses 
137        * a pTask->userhandler on startup and simply calls a global 
138        * function pointer to the default USER signal handler.
139        */
140
141     USER_InstallRsrcHandler( hInstance );
142
143     /* Hack: restore the divide-by-zero handler */
144     /* FIXME: should set a USER-specific handler that displays a msg box */
145     INT_SetPMHandler( 0, INT_GetPMHandler( 0xff ) );
146
147     /* Create task message queue */
148     if ( !GetFastQueue() ) return 0;
149
150     return 1;
151 }
152
153 /**********************************************************************
154  *           USER_ModuleUnload
155  */
156 static void USER_ModuleUnload( HMODULE16 hModule )
157 {
158     HOOK_FreeModuleHooks( hModule );
159     CLASS_FreeModuleClasses( hModule );
160 }
161
162 /**********************************************************************
163  *           USER_QueueCleanup
164  */
165 void USER_QueueCleanup( HQUEUE16 hQueue )
166 {
167     if ( hQueue )
168     {
169         WND* desktop = WIN_GetDesktop();
170
171         /* Patch resident popup menu window */
172         MENU_PatchResidentPopup( hQueue, NULL );
173
174         TIMER_RemoveQueueTimers( hQueue );
175
176         QUEUE_FlushMessages( hQueue );
177         HOOK_FreeQueueHooks( hQueue );
178
179         QUEUE_SetExitingQueue( hQueue );
180         WIN_ResetQueueWindows( desktop, hQueue, (HQUEUE16)0);
181         CLIPBOARD_ResetLock( hQueue, 0 );
182         QUEUE_SetExitingQueue( 0 );
183
184         /* Free the message queue */
185         QUEUE_DeleteMsgQueue( hQueue );
186     }
187 }
188
189 /**********************************************************************
190  *           USER_AppExit
191  */
192 static void USER_AppExit( HTASK16 hTask, HINSTANCE16 hInstance, HQUEUE16 hQueue )
193 {
194     /* FIXME: empty clipboard if needed, maybe destroy menus (Windows
195      *        only complains about them but does nothing);
196      */
197
198     WND* desktop = WIN_GetDesktop();
199
200     /* Patch desktop window */
201     if( desktop->hmemTaskQ == hQueue )
202         desktop->hmemTaskQ = GetTaskQueue(TASK_GetNextTask(hTask));
203                   
204     USER_QueueCleanup(hQueue);
205
206     /* ModuleUnload() in "Internals" */
207
208     hInstance = GetExePtr( hInstance );
209     if( GetModuleUsage( hInstance ) <= 1 ) 
210         USER_ModuleUnload( hInstance );
211 }
212
213
214 /***********************************************************************
215  *           USER_ExitWindows
216  *
217  * Clean-up everything and exit the Wine process.
218  * This is the back-end of ExitWindows(), called when all windows
219  * have agreed to be terminated.
220  */
221 void USER_ExitWindows(void)
222 {
223     /* Do the clean-up stuff */
224
225     WriteOutProfiles();
226     SHELL_SaveRegistry();
227
228     exit(0);
229 }
230
231
232 /***********************************************************************
233  *           USER_SignalProc (USER.314)
234  */
235 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
236                              UINT16 uExitFn, HINSTANCE16 hInstance,
237                              HQUEUE16 hQueue )
238 {
239     switch( uCode )
240     {
241         case USIG_GPF:
242         case USIG_TERMINATION:
243              USER_AppExit( hTaskOrModule, hInstance, hQueue ); /* task */
244              break;
245
246         case USIG_DLL_LOAD:
247              USER_InstallRsrcHandler( hTaskOrModule ); /* module */
248              break;
249
250         case USIG_DLL_UNLOAD:
251              USER_ModuleUnload( hTaskOrModule ); /* module */
252              break;
253
254         default:
255              FIXME(msg,"Unimplemented USER signal: %i\n", (int)uCode );
256     }
257 }
258
259
260 /***********************************************************************
261  *           ExitWindows16   (USER.7)
262  */
263 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
264 {
265     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
266 }
267
268
269 /***********************************************************************
270  *           ExitWindowsExec16   (USER.246)
271  */
272 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
273 {
274     TRACE(system, "Should run the following in DOS-mode: \"%s %s\"\n",
275         lpszExe, lpszParams);
276     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
277 }
278
279
280 /***********************************************************************
281  *           ExitWindowsEx   (USER32.196)
282  */
283 BOOL32 WINAPI ExitWindowsEx( UINT32 flags, DWORD reserved )
284 {
285     int i;
286     BOOL32 result;
287     WND **list, **ppWnd;
288         
289     /* We have to build a list of all windows first, as in EnumWindows */
290
291     if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL ))) return FALSE;
292
293     /* Send a WM_QUERYENDSESSION message to every window */
294
295     for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
296     {
297         /* Make sure that the window still exists */
298         if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
299         if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
300             break;
301     }
302     result = !(*ppWnd);
303
304     /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
305
306     for (ppWnd = list; i > 0; i--, ppWnd++)
307     {
308         if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
309         SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
310     }
311     HeapFree( SystemHeap, 0, list );
312
313     if (result) USER_ExitWindows();
314     return FALSE;
315 }
316
317
318 /***********************************************************************
319  *           ChangeDisplaySettingA    (USER32.589)
320  */
321 LONG WINAPI ChangeDisplaySettings32A( LPDEVMODE32A devmode, DWORD flags )
322 {
323   FIXME(system, ": stub\n");
324   if (devmode==NULL)
325     FIXME(system,"   devmode=NULL (return to default mode)\n");
326   else if ( (devmode->dmBitsPerPel != DESKTOP_GetScreenDepth()) 
327             || (devmode->dmPelsHeight != DESKTOP_GetScreenHeight())
328             || (devmode->dmPelsWidth != DESKTOP_GetScreenWidth()) )
329
330   {
331
332     if (devmode->dmFields & DM_BITSPERPEL)
333       FIXME(system,"   bpp=%ld\n",devmode->dmBitsPerPel);
334     if (devmode->dmFields & DM_PELSWIDTH)
335       FIXME(system,"   width=%ld\n",devmode->dmPelsWidth);
336     if (devmode->dmFields & DM_PELSHEIGHT)
337       FIXME(system,"   height=%ld\n",devmode->dmPelsHeight);
338     FIXME(system," (Putting X in this mode beforehand might help)\n"); 
339     /* we don't, but the program ... does not need to know */
340     return DISP_CHANGE_SUCCESSFUL; 
341   }
342   return DISP_CHANGE_SUCCESSFUL;
343 }
344
345 /***********************************************************************
346  *           EnumDisplaySettingsA   (USER32.592)
347  * FIXME: Currently uses static list of modes.
348  *
349  * RETURNS
350  *      TRUE if nth setting exists found (described in the LPDEVMODE32A struct)
351  *      FALSE if we do not have the nth setting
352  */
353 BOOL32 WINAPI EnumDisplaySettings32A(
354         LPCSTR name,            /* [in] huh? */
355         DWORD n,                /* [in] nth entry in display settings list*/
356         LPDEVMODE32A devmode    /* [out] devmode for that setting */
357 ) {
358 #define NRMODES 5
359 #define NRDEPTHS 4
360         struct {
361                 int w,h;
362         } modes[NRMODES]={{512,384},{640,400},{640,480},{800,600},{1024,768}};
363         int depths[4] = {8,16,24,32};
364
365         TRACE(system,"(%s,%ld,%p)\n",name,n,devmode);
366         if (n==0) {
367                 devmode->dmBitsPerPel = DESKTOP_GetScreenDepth();
368                 devmode->dmPelsHeight = DESKTOP_GetScreenHeight();
369                 devmode->dmPelsWidth = DESKTOP_GetScreenWidth();
370                 return TRUE;
371         }
372         if ((n-1)<NRMODES*NRDEPTHS) {
373                 devmode->dmBitsPerPel   = depths[(n-1)/NRMODES];
374                 devmode->dmPelsHeight   = modes[(n-1)%NRMODES].h;
375                 devmode->dmPelsWidth    = modes[(n-1)%NRMODES].w;
376                 return TRUE;
377         }
378         return FALSE;
379 }
380
381 /***********************************************************************
382  *           EnumDisplaySettingsW   (USER32.593)
383  */
384 BOOL32 WINAPI EnumDisplaySettings32W(LPCWSTR name,DWORD n,LPDEVMODE32W devmode) {
385         LPSTR nameA = HEAP_strdupWtoA(GetProcessHeap(),0,name);
386         DEVMODE32A      devmodeA; 
387         BOOL32 ret = EnumDisplaySettings32A(nameA,n,&devmodeA); 
388
389         if (ret) {
390                 devmode->dmBitsPerPel   = devmodeA.dmBitsPerPel;
391                 devmode->dmPelsHeight   = devmodeA.dmPelsHeight;
392                 devmode->dmPelsWidth    = devmodeA.dmPelsWidth;
393                 /* FIXME: convert rest too, if they are ever returned */
394         }
395         HeapFree(GetProcessHeap(),0,nameA);
396         return ret;
397 }
398
399 /***********************************************************************
400  *           SetEventHook   (USER.321)
401  *
402  *      Used by Turbo Debugger for Windows
403  */
404 FARPROC16 WINAPI SetEventHook(FARPROC16 lpfnEventHook)
405 {
406         FIXME(hook, "(lpfnEventHook=%08x): stub\n", (UINT32)lpfnEventHook);
407         return NULL;
408 }
409
410 /***********************************************************************
411  *           UserSeeUserDo   (USER.216)
412  */
413 DWORD WINAPI UserSeeUserDo(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
414 {
415     switch (wReqType)
416     {
417     case USUD_LOCALALLOC:
418         return LOCAL_Alloc(USER_HeapSel, wParam1, wParam3);
419     case USUD_LOCALFREE:
420         return LOCAL_Free(USER_HeapSel, wParam1);
421     case USUD_LOCALCOMPACT:
422         return LOCAL_Compact(USER_HeapSel, wParam3, 0);
423     case USUD_LOCALHEAP:
424         return USER_HeapSel;
425     case USUD_FIRSTCLASS:
426         FIXME(local, "return a pointer to the first window class.\n"); 
427         return (DWORD)-1;
428     default:
429         WARN(local, "wReqType %04x (unknown)", wReqType);
430         return (DWORD)-1;
431     }
432 }
433
434 /***********************************************************************
435  *           RegisterLogonProcess   (USER32.434)
436  */
437 DWORD WINAPI RegisterLogonProcess(HANDLE32 hprocess,BOOL32 x) {
438         FIXME(win32,"(%d,%d),stub!\n",hprocess,x);
439         return 1;
440 }
441
442 /***********************************************************************
443  *           CreateWindowStation32W   (USER32.86)
444  */
445 HWINSTA32 WINAPI CreateWindowStation32W(
446         LPWSTR winstation,DWORD res1,DWORD desiredaccess,
447         LPSECURITY_ATTRIBUTES lpsa
448 ) {
449         FIXME(win32,"(%s,0x%08lx,0x%08lx,%p),stub!\n",debugstr_w(winstation),
450                 res1,desiredaccess,lpsa
451         );
452         return 0xdeadcafe;
453 }
454
455 /***********************************************************************
456  *           SetProcessWindowStation   (USER32.496)
457  */
458 BOOL32 WINAPI SetProcessWindowStation(HWINSTA32 hWinSta) {
459         FIXME(win32,"(%d),stub!\n",hWinSta);
460         return TRUE;
461 }
462
463 /***********************************************************************
464  *           SetUserObjectSecurity   (USER32.514)
465  */
466 BOOL32 WINAPI SetUserObjectSecurity(
467         HANDLE32 hObj,
468         /*LPSECURITY_INFORMATION*/LPVOID pSIRequested,
469         PSECURITY_DESCRIPTOR pSID
470 ) {
471         FIXME(win32,"(0x%08x,%p,%p),stub!\n",hObj,pSIRequested,pSID);
472         return TRUE;
473 }
474
475 /***********************************************************************
476  *           CreateDesktop32W   (USER32.69)
477  */
478 HDESK32 WINAPI CreateDesktop32W(
479         LPWSTR lpszDesktop,LPWSTR lpszDevice,LPDEVMODE32W pDevmode,
480         DWORD dwFlags,DWORD dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa
481 ) {
482         FIXME(win32,"(%s,%s,%p,0x%08lx,0x%08lx,%p),stub!\n",
483                 debugstr_w(lpszDesktop),debugstr_w(lpszDevice),pDevmode,
484                 dwFlags,dwDesiredAccess,lpsa
485         );
486         return 0xcafedead;
487 }
488
489 /***********************************************************************
490  *           SetWindowStationUser   (USER32.521)
491  */
492 DWORD WINAPI SetWindowStationUser(DWORD x1,DWORD x2) {
493         FIXME(win32,"(0x%08lx,0x%08lx),stub!\n",x1,x2);
494         return 1;
495 }
496
497 /***********************************************************************
498  *           SetLogonNotifyWindow   (USER32.486)
499  */
500 DWORD WINAPI SetLogonNotifyWindow(HWINSTA32 hwinsta,HWND32 hwnd) {
501         FIXME(win32,"(0x%x,%04x),stub!\n",hwinsta,hwnd);
502         return 1;
503 }
504
505 /***********************************************************************
506  *           LoadLocalFonts   (USER32.486)
507  */
508 VOID WINAPI LoadLocalFonts(VOID) {
509         /* are loaded. */
510         return;
511 }
512 /***********************************************************************
513  *           GetUserObjectInformation32A   (USER32.299)
514  */
515 BOOL32 WINAPI GetUserObjectInformation32A( HANDLE32 hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
516 {       FIXME(win32,"(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
517         return TRUE;
518 }
519 /***********************************************************************
520  *           GetUserObjectInformation32W   (USER32.300)
521  */
522 BOOL32 WINAPI GetUserObjectInformation32W( HANDLE32 hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
523 {       FIXME(win32,"(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
524         return TRUE;
525 }
526 /***********************************************************************
527  *           GetUserObjectSecurity32   (USER32.300)
528  */
529 BOOL32 WINAPI GetUserObjectSecurity32(HANDLE32 hObj, SECURITY_INFORMATION * pSIRequested,
530         PSECURITY_DESCRIPTOR pSID, DWORD nLength, LPDWORD lpnLengthNeeded)
531 {       FIXME(win32,"(0x%x %p %p len=%ld %p),stub!\n",  hObj, pSIRequested, pSID, nLength, lpnLengthNeeded);
532         return TRUE;
533 }