- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[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         HOOK_FreeQueueHooks( hQueue );
177
178         QUEUE_SetExitingQueue( hQueue );
179         WIN_ResetQueueWindows( desktop, hQueue, (HQUEUE16)0);
180         CLIPBOARD_ResetLock( hQueue, 0 );
181         QUEUE_SetExitingQueue( 0 );
182
183         /* Free the message queue */
184         QUEUE_DeleteMsgQueue( hQueue );
185     }
186 }
187
188 /**********************************************************************
189  *           USER_AppExit
190  */
191 static void USER_AppExit( HTASK16 hTask, HINSTANCE16 hInstance, HQUEUE16 hQueue )
192 {
193     /* FIXME: empty clipboard if needed, maybe destroy menus (Windows
194      *        only complains about them but does nothing);
195      */
196
197     WND* desktop = WIN_GetDesktop();
198
199     /* Patch desktop window */
200     if( desktop->hmemTaskQ == hQueue )
201         desktop->hmemTaskQ = GetTaskQueue(TASK_GetNextTask(hTask));
202                   
203     USER_QueueCleanup(hQueue);
204
205     /* ModuleUnload() in "Internals" */
206
207     hInstance = GetExePtr( hInstance );
208     if( GetModuleUsage( hInstance ) <= 1 ) 
209         USER_ModuleUnload( hInstance );
210 }
211
212
213 /***********************************************************************
214  *           USER_ExitWindows
215  *
216  * Clean-up everything and exit the Wine process.
217  * This is the back-end of ExitWindows(), called when all windows
218  * have agreed to be terminated.
219  */
220 void USER_ExitWindows(void)
221 {
222     /* Do the clean-up stuff */
223
224     WriteOutProfiles();
225     SHELL_SaveRegistry();
226
227     exit(0);
228 }
229
230
231 /***********************************************************************
232  *           USER_SignalProc (USER.314)
233  */
234 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
235                              UINT16 uExitFn, HINSTANCE16 hInstance,
236                              HQUEUE16 hQueue )
237 {
238     switch( uCode )
239     {
240         case USIG_GPF:
241         case USIG_TERMINATION:
242              USER_AppExit( hTaskOrModule, hInstance, hQueue ); /* task */
243              break;
244
245         case USIG_DLL_LOAD:
246              USER_InstallRsrcHandler( hTaskOrModule ); /* module */
247              break;
248
249         case USIG_DLL_UNLOAD:
250              USER_ModuleUnload( hTaskOrModule ); /* module */
251              break;
252
253         default:
254              FIXME(msg,"Unimplemented USER signal: %i\n", (int)uCode );
255     }
256 }
257
258
259 /***********************************************************************
260  *           ExitWindows16   (USER.7)
261  */
262 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
263 {
264     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
265 }
266
267
268 /***********************************************************************
269  *           ExitWindowsExec16   (USER.246)
270  */
271 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
272 {
273     TRACE(system, "Should run the following in DOS-mode: \"%s %s\"\n",
274         lpszExe, lpszParams);
275     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
276 }
277
278
279 /***********************************************************************
280  *           ExitWindowsEx   (USER32.196)
281  */
282 BOOL32 WINAPI ExitWindowsEx( UINT32 flags, DWORD reserved )
283 {
284     int i;
285     BOOL32 result;
286     WND **list, **ppWnd;
287         
288     /* We have to build a list of all windows first, as in EnumWindows */
289
290     if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL ))) return FALSE;
291
292     /* Send a WM_QUERYENDSESSION message to every window */
293
294     for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
295     {
296         /* Make sure that the window still exists */
297         if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
298         if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
299             break;
300     }
301     result = !(*ppWnd);
302
303     /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
304
305     for (ppWnd = list; i > 0; i--, ppWnd++)
306     {
307         if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
308         SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
309     }
310     HeapFree( SystemHeap, 0, list );
311
312     if (result) USER_ExitWindows();
313     return FALSE;
314 }
315
316
317 /***********************************************************************
318  *           ChangeDisplaySettingA    (USER32.589)
319  */
320 LONG WINAPI ChangeDisplaySettings32A( LPDEVMODE32A devmode, DWORD flags )
321 {
322   FIXME(system, ": stub\n");
323   if (devmode==NULL)
324     FIXME(system,"   devmode=NULL (return to default mode)\n");
325   else if ( (devmode->dmBitsPerPel != DESKTOP_GetScreenDepth()) 
326             || (devmode->dmPelsHeight != DESKTOP_GetScreenHeight())
327             || (devmode->dmPelsWidth != DESKTOP_GetScreenWidth()) )
328
329   {
330
331     if (devmode->dmFields & DM_BITSPERPEL)
332       FIXME(system,"   bpp=%ld\n",devmode->dmBitsPerPel);
333     if (devmode->dmFields & DM_PELSWIDTH)
334       FIXME(system,"   width=%ld\n",devmode->dmPelsWidth);
335     if (devmode->dmFields & DM_PELSHEIGHT)
336       FIXME(system,"   height=%ld\n",devmode->dmPelsHeight);
337     FIXME(system," (Putting X in this mode beforehand might help)\n"); 
338     /* we don't, but the program ... does not need to know */
339     return DISP_CHANGE_SUCCESSFUL; 
340   }
341   return DISP_CHANGE_SUCCESSFUL;
342 }
343
344 /***********************************************************************
345  *           EnumDisplaySettingsA   (USER32.592)
346  * FIXME: Currently uses static list of modes.
347  *
348  * RETURNS
349  *      TRUE if nth setting exists found (described in the LPDEVMODE32A struct)
350  *      FALSE if we do not have the nth setting
351  */
352 BOOL32 WINAPI EnumDisplaySettings32A(
353         LPCSTR name,            /* [in] huh? */
354         DWORD n,                /* [in] nth entry in display settings list*/
355         LPDEVMODE32A devmode    /* [out] devmode for that setting */
356 ) {
357 #define NRMODES 5
358 #define NRDEPTHS 4
359         struct {
360                 int w,h;
361         } modes[NRMODES]={{512,384},{640,400},{640,480},{800,600},{1024,768}};
362         int depths[4] = {8,16,24,32};
363
364         TRACE(system,"(%s,%ld,%p)\n",name,n,devmode);
365         if (n==0) {
366                 devmode->dmBitsPerPel = DESKTOP_GetScreenDepth();
367                 devmode->dmPelsHeight = DESKTOP_GetScreenHeight();
368                 devmode->dmPelsWidth = DESKTOP_GetScreenWidth();
369                 return TRUE;
370         }
371         if ((n-1)<NRMODES*NRDEPTHS) {
372                 devmode->dmBitsPerPel   = depths[(n-1)/NRMODES];
373                 devmode->dmPelsHeight   = modes[(n-1)%NRMODES].h;
374                 devmode->dmPelsWidth    = modes[(n-1)%NRMODES].w;
375                 return TRUE;
376         }
377         return FALSE;
378 }
379
380 /***********************************************************************
381  *           EnumDisplaySettingsW   (USER32.593)
382  */
383 BOOL32 WINAPI EnumDisplaySettings32W(LPCWSTR name,DWORD n,LPDEVMODE32W devmode) {
384         LPSTR nameA = HEAP_strdupWtoA(GetProcessHeap(),0,name);
385         DEVMODE32A      devmodeA; 
386         BOOL32 ret = EnumDisplaySettings32A(nameA,n,&devmodeA); 
387
388         if (ret) {
389                 devmode->dmBitsPerPel   = devmodeA.dmBitsPerPel;
390                 devmode->dmPelsHeight   = devmodeA.dmPelsHeight;
391                 devmode->dmPelsWidth    = devmodeA.dmPelsWidth;
392                 /* FIXME: convert rest too, if they are ever returned */
393         }
394         HeapFree(GetProcessHeap(),0,nameA);
395         return ret;
396 }
397
398 /***********************************************************************
399  *           SetEventHook   (USER.321)
400  *
401  *      Used by Turbo Debugger for Windows
402  */
403 FARPROC16 WINAPI SetEventHook(FARPROC16 lpfnEventHook)
404 {
405         FIXME(hook, "(lpfnEventHook=%08x): stub\n", (UINT32)lpfnEventHook);
406         return NULL;
407 }
408
409 /***********************************************************************
410  *           UserSeeUserDo   (USER.216)
411  */
412 DWORD WINAPI UserSeeUserDo(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
413 {
414     switch (wReqType)
415     {
416     case USUD_LOCALALLOC:
417         return LOCAL_Alloc(USER_HeapSel, wParam1, wParam3);
418     case USUD_LOCALFREE:
419         return LOCAL_Free(USER_HeapSel, wParam1);
420     case USUD_LOCALCOMPACT:
421         return LOCAL_Compact(USER_HeapSel, wParam3, 0);
422     case USUD_LOCALHEAP:
423         return USER_HeapSel;
424     case USUD_FIRSTCLASS:
425         FIXME(local, "return a pointer to the first window class.\n"); 
426         return (DWORD)-1;
427     default:
428         WARN(local, "wReqType %04x (unknown)", wReqType);
429         return (DWORD)-1;
430     }
431 }
432
433 /***********************************************************************
434  *           RegisterLogonProcess   (USER32.434)
435  */
436 DWORD WINAPI RegisterLogonProcess(HANDLE32 hprocess,BOOL32 x) {
437         FIXME(win32,"(%d,%d),stub!\n",hprocess,x);
438         return 1;
439 }
440
441 /***********************************************************************
442  *           CreateWindowStation32W   (USER32.86)
443  */
444 HWINSTA32 WINAPI CreateWindowStation32W(
445         LPWSTR winstation,DWORD res1,DWORD desiredaccess,
446         LPSECURITY_ATTRIBUTES lpsa
447 ) {
448         FIXME(win32,"(%s,0x%08lx,0x%08lx,%p),stub!\n",debugstr_w(winstation),
449                 res1,desiredaccess,lpsa
450         );
451         return 0xdeadcafe;
452 }
453
454 /***********************************************************************
455  *           SetProcessWindowStation   (USER32.496)
456  */
457 BOOL32 WINAPI SetProcessWindowStation(HWINSTA32 hWinSta) {
458         FIXME(win32,"(%d),stub!\n",hWinSta);
459         return TRUE;
460 }
461
462 /***********************************************************************
463  *           SetUserObjectSecurity   (USER32.514)
464  */
465 BOOL32 WINAPI SetUserObjectSecurity(
466         HANDLE32 hObj,
467         /*LPSECURITY_INFORMATION*/LPVOID pSIRequested,
468         PSECURITY_DESCRIPTOR pSID
469 ) {
470         FIXME(win32,"(0x%08x,%p,%p),stub!\n",hObj,pSIRequested,pSID);
471         return TRUE;
472 }
473
474 /***********************************************************************
475  *           CreateDesktop32W   (USER32.69)
476  */
477 HDESK32 WINAPI CreateDesktop32W(
478         LPWSTR lpszDesktop,LPWSTR lpszDevice,LPDEVMODE32W pDevmode,
479         DWORD dwFlags,DWORD dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa
480 ) {
481         FIXME(win32,"(%s,%s,%p,0x%08lx,0x%08lx,%p),stub!\n",
482                 debugstr_w(lpszDesktop),debugstr_w(lpszDevice),pDevmode,
483                 dwFlags,dwDesiredAccess,lpsa
484         );
485         return 0xcafedead;
486 }
487
488 /***********************************************************************
489  *           SetWindowStationUser   (USER32.521)
490  */
491 DWORD WINAPI SetWindowStationUser(DWORD x1,DWORD x2) {
492         FIXME(win32,"(0x%08lx,0x%08lx),stub!\n",x1,x2);
493         return 1;
494 }
495
496 /***********************************************************************
497  *           SetLogonNotifyWindow   (USER32.486)
498  */
499 DWORD WINAPI SetLogonNotifyWindow(HWINSTA32 hwinsta,HWND32 hwnd) {
500         FIXME(win32,"(0x%x,%04x),stub!\n",hwinsta,hwnd);
501         return 1;
502 }
503
504 /***********************************************************************
505  *           LoadLocalFonts   (USER32.486)
506  */
507 VOID WINAPI LoadLocalFonts(VOID) {
508         /* are loaded. */
509         return;
510 }
511 /***********************************************************************
512  *           GetUserObjectInformation32A   (USER32.299)
513  */
514 BOOL32 WINAPI GetUserObjectInformation32A( HANDLE32 hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
515 {       FIXME(win32,"(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
516         return TRUE;
517 }
518 /***********************************************************************
519  *           GetUserObjectInformation32W   (USER32.300)
520  */
521 BOOL32 WINAPI GetUserObjectInformation32W( HANDLE32 hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
522 {       FIXME(win32,"(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
523         return TRUE;
524 }
525 /***********************************************************************
526  *           GetUserObjectSecurity32   (USER32.300)
527  */
528 BOOL32 WINAPI GetUserObjectSecurity32(HANDLE32 hObj, SECURITY_INFORMATION * pSIRequested,
529         PSECURITY_DESCRIPTOR pSID, DWORD nLength, LPDWORD lpnLengthNeeded)
530 {       FIXME(win32,"(0x%x %p %p len=%ld %p),stub!\n",  hObj, pSIRequested, pSID, nLength, lpnLengthNeeded);
531         return TRUE;
532 }