Release 990226.
[wine/gsoc-2012-control.git] / windows / user.c
blob4c5d320f6355fceef301eed0b27b430452cf864a
1 /*
2 * Misc. USER functions
4 * Copyright 1993 Robert J. Amstadt
5 * 1996 Alex Korobka
6 */
8 #include <stdlib.h>
9 #include "wine/winbase16.h"
10 #include "winuser.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 "shell.h"
26 #include "callback.h"
27 #include "local.h"
28 #include "class.h"
29 #include "desktop.h"
31 /***********************************************************************
32 * GetFreeSystemResources (USER.284)
34 WORD WINAPI GetFreeSystemResources16( WORD resType )
36 int userPercent, gdiPercent;
38 switch(resType)
40 case GFSR_USERRESOURCES:
41 userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
42 LOCAL_HeapSize( USER_HeapSel );
43 gdiPercent = 100;
44 break;
46 case GFSR_GDIRESOURCES:
47 gdiPercent = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
48 LOCAL_HeapSize( GDI_HeapSel );
49 userPercent = 100;
50 break;
52 case GFSR_SYSTEMRESOURCES:
53 userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
54 LOCAL_HeapSize( USER_HeapSel );
55 gdiPercent = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
56 LOCAL_HeapSize( GDI_HeapSel );
57 break;
59 default:
60 return 0;
62 return (WORD)MIN( userPercent, gdiPercent );
66 /***********************************************************************
67 * SystemHeapInfo (TOOLHELP.71)
69 BOOL16 WINAPI SystemHeapInfo16( SYSHEAPINFO *pHeapInfo )
71 pHeapInfo->wUserFreePercent = GetFreeSystemResources16( GFSR_USERRESOURCES );
72 pHeapInfo->wGDIFreePercent = GetFreeSystemResources16( GFSR_GDIRESOURCES );
73 pHeapInfo->hUserSegment = USER_HeapSel;
74 pHeapInfo->hGDISegment = GDI_HeapSel;
75 return TRUE;
79 /***********************************************************************
80 * TimerCount (TOOLHELP.80)
82 BOOL16 WINAPI TimerCount16( TIMERINFO *pTimerInfo )
84 /* FIXME
85 * In standard mode, dwmsSinceStart = dwmsThisVM
87 * I tested this, under Windows in enhanced mode, and
88 * if you never switch VM (ie start/stop DOS) these
89 * values should be the same as well.
91 * Also, Wine should adjust for the hardware timer
92 * to reduce the amount of error to ~1ms.
93 * I can't be bothered, can you?
95 pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
96 return TRUE;
99 static FARPROC16 __r16loader = NULL;
101 /**********************************************************************
102 * USER_CallDefaultRsrcHandler
104 * Called by the LoadDIBIcon/CursorHandler().
106 HGLOBAL16 USER_CallDefaultRsrcHandler( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
108 return Callbacks->CallResourceHandlerProc( __r16loader, hMemObj, hModule, hRsrc );
111 /**********************************************************************
112 * USER_InstallRsrcHandler
114 static void USER_InstallRsrcHandler( HINSTANCE16 hInstance )
116 FARPROC16 proc;
118 /* SetResourceHandler() returns previous function which is set
119 * when a module's resource table is loaded. */
121 proc = SetResourceHandler16( hInstance, RT_ICON16,
122 MODULE_GetWndProcEntry16("LoadDIBIconHandler") );
123 if (!__r16loader) __r16loader = proc;
125 proc = SetResourceHandler16( hInstance, RT_CURSOR16,
126 MODULE_GetWndProcEntry16("LoadDIBCursorHandler") );
127 if (!__r16loader) __r16loader = proc;
130 /**********************************************************************
131 * InitApp (USER.5)
133 INT16 WINAPI InitApp16( HINSTANCE16 hInstance )
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 /* Hack: restore the divide-by-zero handler */
145 /* FIXME: should set a USER-specific handler that displays a msg box */
146 INT_SetPMHandler( 0, INT_GetPMHandler( 0xff ) );
148 /* Create task message queue */
149 if ( !GetFastQueue16() ) return 0;
151 return 1;
154 /**********************************************************************
155 * USER_ModuleUnload
157 static void USER_ModuleUnload( HMODULE16 hModule )
159 HOOK_FreeModuleHooks( hModule );
160 CLASS_FreeModuleClasses( hModule );
163 /**********************************************************************
164 * USER_QueueCleanup
166 void USER_QueueCleanup( HQUEUE16 hQueue )
168 if ( hQueue )
170 WND* desktop = WIN_GetDesktop();
172 /* Patch resident popup menu window */
173 MENU_PatchResidentPopup( hQueue, NULL );
175 TIMER_RemoveQueueTimers( hQueue );
177 HOOK_FreeQueueHooks( hQueue );
179 QUEUE_SetExitingQueue( hQueue );
180 WIN_ResetQueueWindows( desktop, hQueue, (HQUEUE16)0);
181 CLIPBOARD_ResetLock( hQueue, 0 );
182 QUEUE_SetExitingQueue( 0 );
184 /* Free the message queue */
185 QUEUE_DeleteMsgQueue( hQueue );
189 /**********************************************************************
190 * USER_AppExit
192 static void USER_AppExit( HTASK16 hTask, HINSTANCE16 hInstance, HQUEUE16 hQueue )
194 /* FIXME: empty clipboard if needed, maybe destroy menus (Windows
195 * only complains about them but does nothing);
198 WND* desktop = WIN_GetDesktop();
200 /* Patch desktop window */
201 if( desktop->hmemTaskQ == hQueue )
202 desktop->hmemTaskQ = GetTaskQueue16(TASK_GetNextTask(hTask));
204 USER_QueueCleanup(hQueue);
206 /* ModuleUnload() in "Internals" */
208 hInstance = GetExePtr( hInstance );
209 if( GetModuleUsage16( hInstance ) <= 1 )
210 USER_ModuleUnload( hInstance );
214 /***********************************************************************
215 * USER_ExitWindows
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.
221 void USER_ExitWindows(void)
223 /* Do the clean-up stuff */
225 WriteOutProfiles16();
226 SHELL_SaveRegistry();
228 exit(0);
232 /***********************************************************************
233 * USER_SignalProc (USER.314)
235 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
236 UINT16 uExitFn, HINSTANCE16 hInstance,
237 HQUEUE16 hQueue )
239 switch( uCode )
241 case USIG_GPF:
242 case USIG_TERMINATION:
243 USER_AppExit( hTaskOrModule, hInstance, hQueue ); /* task */
244 break;
246 case USIG_DLL_LOAD:
247 USER_InstallRsrcHandler( hTaskOrModule ); /* module */
248 break;
250 case USIG_DLL_UNLOAD:
251 USER_ModuleUnload( hTaskOrModule ); /* module */
252 break;
254 default:
255 FIXME(msg,"Unimplemented USER signal: %i\n", (int)uCode );
260 /***********************************************************************
261 * ExitWindows16 (USER.7)
263 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
265 return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
269 /***********************************************************************
270 * ExitWindowsExec16 (USER.246)
272 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
274 TRACE(system, "Should run the following in DOS-mode: \"%s %s\"\n",
275 lpszExe, lpszParams);
276 return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
280 /***********************************************************************
281 * ExitWindowsEx (USER32.196)
283 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reserved )
285 int i;
286 BOOL result;
287 WND **list, **ppWnd;
289 /* We have to build a list of all windows first, as in EnumWindows */
291 if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL ))) return FALSE;
293 /* Send a WM_QUERYENDSESSION message to every window */
295 for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
297 /* Make sure that the window still exists */
298 if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
299 if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
300 break;
302 result = !(*ppWnd);
304 /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
306 for (ppWnd = list; i > 0; i--, ppWnd++)
308 if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
309 SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
311 HeapFree( SystemHeap, 0, list );
313 if (result) USER_ExitWindows();
314 return FALSE;
318 /***********************************************************************
319 * ChangeDisplaySettingA (USER32.589)
321 LONG WINAPI ChangeDisplaySettingsA( LPDEVMODEA devmode, DWORD flags )
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()) )
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;
342 return DISP_CHANGE_SUCCESSFUL;
345 /***********************************************************************
346 * EnumDisplaySettingsA (USER32.592)
347 * FIXME: Currently uses static list of modes.
349 * RETURNS
350 * TRUE if nth setting exists found (described in the LPDEVMODE32A struct)
351 * FALSE if we do not have the nth setting
353 BOOL WINAPI EnumDisplaySettingsA(
354 LPCSTR name, /* [in] huh? */
355 DWORD n, /* [in] nth entry in display settings list*/
356 LPDEVMODEA devmode /* [out] devmode for that setting */
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};
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;
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;
378 return FALSE;
381 /***********************************************************************
382 * EnumDisplaySettingsW (USER32.593)
384 BOOL WINAPI EnumDisplaySettingsW(LPCWSTR name,DWORD n,LPDEVMODEW devmode) {
385 LPSTR nameA = HEAP_strdupWtoA(GetProcessHeap(),0,name);
386 DEVMODEA devmodeA;
387 BOOL ret = EnumDisplaySettingsA(nameA,n,&devmodeA);
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 */
395 HeapFree(GetProcessHeap(),0,nameA);
396 return ret;
399 /***********************************************************************
400 * SetEventHook (USER.321)
402 * Used by Turbo Debugger for Windows
404 FARPROC16 WINAPI SetEventHook16(FARPROC16 lpfnEventHook)
406 FIXME(hook, "(lpfnEventHook=%08x): stub\n", (UINT)lpfnEventHook);
407 return NULL;
410 /***********************************************************************
411 * UserSeeUserDo (USER.216)
413 DWORD WINAPI UserSeeUserDo16(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
415 switch (wReqType)
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;
434 /***********************************************************************
435 * RegisterLogonProcess (USER32.434)
437 DWORD WINAPI RegisterLogonProcess(HANDLE hprocess,BOOL x) {
438 FIXME(win32,"(%d,%d),stub!\n",hprocess,x);
439 return 1;
442 /***********************************************************************
443 * CreateWindowStation32W (USER32.86)
445 HWINSTA WINAPI CreateWindowStationW(
446 LPWSTR winstation,DWORD res1,DWORD desiredaccess,
447 LPSECURITY_ATTRIBUTES lpsa
449 FIXME(win32,"(%s,0x%08lx,0x%08lx,%p),stub!\n",debugstr_w(winstation),
450 res1,desiredaccess,lpsa
452 return 0xdeadcafe;
455 /***********************************************************************
456 * SetProcessWindowStation (USER32.496)
458 BOOL WINAPI SetProcessWindowStation(HWINSTA hWinSta) {
459 FIXME(win32,"(%d),stub!\n",hWinSta);
460 return TRUE;
463 /***********************************************************************
464 * SetUserObjectSecurity (USER32.514)
466 BOOL WINAPI SetUserObjectSecurity(
467 HANDLE hObj,
468 /*LPSECURITY_INFORMATION*/LPVOID pSIRequested,
469 PSECURITY_DESCRIPTOR pSID
471 FIXME(win32,"(0x%08x,%p,%p),stub!\n",hObj,pSIRequested,pSID);
472 return TRUE;
475 /***********************************************************************
476 * CreateDesktop32W (USER32.69)
478 HDESK WINAPI CreateDesktopW(
479 LPWSTR lpszDesktop,LPWSTR lpszDevice,LPDEVMODEW pDevmode,
480 DWORD dwFlags,DWORD dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa
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
486 return 0xcafedead;
489 /***********************************************************************
490 * SetWindowStationUser (USER32.521)
492 DWORD WINAPI SetWindowStationUser(DWORD x1,DWORD x2) {
493 FIXME(win32,"(0x%08lx,0x%08lx),stub!\n",x1,x2);
494 return 1;
497 /***********************************************************************
498 * SetLogonNotifyWindow (USER32.486)
500 DWORD WINAPI SetLogonNotifyWindow(HWINSTA hwinsta,HWND hwnd) {
501 FIXME(win32,"(0x%x,%04x),stub!\n",hwinsta,hwnd);
502 return 1;
505 /***********************************************************************
506 * LoadLocalFonts (USER32.486)
508 VOID WINAPI LoadLocalFonts(VOID) {
509 /* are loaded. */
510 return;
512 /***********************************************************************
513 * GetUserObjectInformation32A (USER32.299)
515 BOOL WINAPI GetUserObjectInformationA( HANDLE 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;
519 /***********************************************************************
520 * GetUserObjectInformation32W (USER32.300)
522 BOOL WINAPI GetUserObjectInformationW( HANDLE 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;
526 /***********************************************************************
527 * GetUserObjectSecurity32 (USER32.300)
529 BOOL WINAPI GetUserObjectSecurity(HANDLE 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;