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