Large-scale renaming of all Win32 functions and types to use the
[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 "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"
30
31 /***********************************************************************
32  *           GetFreeSystemResources   (USER.284)
33  */
34 WORD WINAPI GetFreeSystemResources16( WORD resType )
35 {
36     int userPercent, gdiPercent;
37
38     switch(resType)
39     {
40     case GFSR_USERRESOURCES:
41         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
42                                LOCAL_HeapSize( USER_HeapSel );
43         gdiPercent  = 100;
44         break;
45
46     case GFSR_GDIRESOURCES:
47         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
48                                LOCAL_HeapSize( GDI_HeapSel );
49         userPercent = 100;
50         break;
51
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;
58
59     default:
60         return 0;
61     }
62     return (WORD)MIN( userPercent, gdiPercent );
63 }
64
65
66 /***********************************************************************
67  *           SystemHeapInfo   (TOOLHELP.71)
68  */
69 BOOL16 WINAPI SystemHeapInfo16( SYSHEAPINFO *pHeapInfo )
70 {
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;
76 }
77
78
79 /***********************************************************************
80  *           TimerCount   (TOOLHELP.80)
81  */
82 BOOL16 WINAPI TimerCount16( TIMERINFO *pTimerInfo )
83 {
84     /* FIXME
85      * In standard mode, dwmsSinceStart = dwmsThisVM 
86      *
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. 
90      *
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?
94      */
95     pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
96     return TRUE;
97 }
98
99 static FARPROC16 __r16loader = NULL;
100
101 /**********************************************************************
102  *           USER_CallDefaultRsrcHandler
103  *
104  * Called by the LoadDIBIcon/CursorHandler().
105  */
106 HGLOBAL16 USER_CallDefaultRsrcHandler( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
107 {
108     return Callbacks->CallResourceHandlerProc( __r16loader, hMemObj, hModule, hRsrc );
109 }
110
111 /**********************************************************************
112  *           USER_InstallRsrcHandler
113  */
114 static void USER_InstallRsrcHandler( HINSTANCE16 hInstance )
115 {
116     FARPROC16 proc;
117
118     /* SetResourceHandler() returns previous function which is set
119      * when a module's resource table is loaded. */
120
121     proc = SetResourceHandler16( hInstance, RT_ICON16,
122                                MODULE_GetWndProcEntry16("LoadDIBIconHandler") );
123     if (!__r16loader) __r16loader = proc;
124
125     proc = SetResourceHandler16( hInstance, RT_CURSOR16,
126                                MODULE_GetWndProcEntry16("LoadDIBCursorHandler") );
127     if (!__r16loader) __r16loader = proc;
128 }
129
130 /**********************************************************************
131  *           InitApp   (USER.5)
132  */
133 INT16 WINAPI InitApp16( HINSTANCE16 hInstance )
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_SetPMHandler( 0, INT_GetPMHandler( 0xff ) );
147
148     /* Create task message queue */
149     if ( !GetFastQueue16() ) return 0;
150
151     return 1;
152 }
153
154 /**********************************************************************
155  *           USER_ModuleUnload
156  */
157 static void USER_ModuleUnload( HMODULE16 hModule )
158 {
159     HOOK_FreeModuleHooks( hModule );
160     CLASS_FreeModuleClasses( hModule );
161 }
162
163 /**********************************************************************
164  *           USER_QueueCleanup
165  */
166 void USER_QueueCleanup( HQUEUE16 hQueue )
167 {
168     if ( hQueue )
169     {
170         WND* desktop = WIN_GetDesktop();
171
172         /* Patch resident popup menu window */
173         MENU_PatchResidentPopup( hQueue, NULL );
174
175         TIMER_RemoveQueueTimers( hQueue );
176
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 = GetTaskQueue16(TASK_GetNextTask(hTask));
203                   
204     USER_QueueCleanup(hQueue);
205
206     /* ModuleUnload() in "Internals" */
207
208     hInstance = GetExePtr( hInstance );
209     if( GetModuleUsage16( 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     WriteOutProfiles16();
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 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reserved )
284 {
285     int i;
286     BOOL 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 (!IsWindow( (*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 (!IsWindow( (*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 ChangeDisplaySettingsA( LPDEVMODEA 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 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 */
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 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); 
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 SetEventHook16(FARPROC16 lpfnEventHook)
405 {
406         FIXME(hook, "(lpfnEventHook=%08x): stub\n", (UINT)lpfnEventHook);
407         return NULL;
408 }
409
410 /***********************************************************************
411  *           UserSeeUserDo   (USER.216)
412  */
413 DWORD WINAPI UserSeeUserDo16(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(HANDLE hprocess,BOOL x) {
438         FIXME(win32,"(%d,%d),stub!\n",hprocess,x);
439         return 1;
440 }
441
442 /***********************************************************************
443  *           CreateWindowStation32W   (USER32.86)
444  */
445 HWINSTA WINAPI CreateWindowStationW(
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 BOOL WINAPI SetProcessWindowStation(HWINSTA hWinSta) {
459         FIXME(win32,"(%d),stub!\n",hWinSta);
460         return TRUE;
461 }
462
463 /***********************************************************************
464  *           SetUserObjectSecurity   (USER32.514)
465  */
466 BOOL WINAPI SetUserObjectSecurity(
467         HANDLE 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 HDESK WINAPI CreateDesktopW(
479         LPWSTR lpszDesktop,LPWSTR lpszDevice,LPDEVMODEW 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(HWINSTA hwinsta,HWND 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 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;
518 }
519 /***********************************************************************
520  *           GetUserObjectInformation32W   (USER32.300)
521  */
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;
525 }
526 /***********************************************************************
527  *           GetUserObjectSecurity32   (USER32.300)
528  */
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;
533 }