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