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