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