SetCursorPos() function was called with a window coordinates
[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 "desktop.h"
29 #include "process.h"
30 #include "debugtools.h"
31
32 DECLARE_DEBUG_CHANNEL(hook)
33 DECLARE_DEBUG_CHANNEL(local)
34 DECLARE_DEBUG_CHANNEL(system)
35 DECLARE_DEBUG_CHANNEL(win)
36 DECLARE_DEBUG_CHANNEL(win32)
37
38 /***********************************************************************
39  *           GetFreeSystemResources   (USER.284)
40  */
41 WORD WINAPI GetFreeSystemResources16( WORD resType )
42 {
43     int userPercent, gdiPercent;
44
45     switch(resType)
46     {
47     case GFSR_USERRESOURCES:
48         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
49                                LOCAL_HeapSize( USER_HeapSel );
50         gdiPercent  = 100;
51         break;
52
53     case GFSR_GDIRESOURCES:
54         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
55                                LOCAL_HeapSize( GDI_HeapSel );
56         userPercent = 100;
57         break;
58
59     case GFSR_SYSTEMRESOURCES:
60         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
61                                LOCAL_HeapSize( USER_HeapSel );
62         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
63                                LOCAL_HeapSize( GDI_HeapSel );
64         break;
65
66     default:
67         return 0;
68     }
69     return (WORD)MIN( userPercent, gdiPercent );
70 }
71
72
73 /***********************************************************************
74  *           SystemHeapInfo   (TOOLHELP.71)
75  */
76 BOOL16 WINAPI SystemHeapInfo16( SYSHEAPINFO *pHeapInfo )
77 {
78     pHeapInfo->wUserFreePercent = GetFreeSystemResources16( GFSR_USERRESOURCES );
79     pHeapInfo->wGDIFreePercent  = GetFreeSystemResources16( GFSR_GDIRESOURCES );
80     pHeapInfo->hUserSegment = USER_HeapSel;
81     pHeapInfo->hGDISegment  = GDI_HeapSel;
82     return TRUE;
83 }
84
85
86 /***********************************************************************
87  *           TimerCount   (TOOLHELP.80)
88  */
89 BOOL16 WINAPI TimerCount16( TIMERINFO *pTimerInfo )
90 {
91     /* FIXME
92      * In standard mode, dwmsSinceStart = dwmsThisVM 
93      *
94      * I tested this, under Windows in enhanced mode, and
95      * if you never switch VM (ie start/stop DOS) these
96      * values should be the same as well. 
97      *
98      * Also, Wine should adjust for the hardware timer
99      * to reduce the amount of error to ~1ms. 
100      * I can't be bothered, can you?
101      */
102     pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
103     return TRUE;
104 }
105
106 /**********************************************************************
107  *           InitApp   (USER.5)
108  */
109 INT16 WINAPI InitApp16( HINSTANCE16 hInstance )
110 {
111     /* Hack: restore the divide-by-zero handler */
112     /* FIXME: should set a USER-specific handler that displays a msg box */
113     INT_SetPMHandler( 0, INT_GetPMHandler( 0xff ) );
114
115     /* Create task message queue */
116     if ( !GetFastQueue16() ) return 0;
117
118     return 1;
119 }
120
121 /**********************************************************************
122  *           USER_ModuleUnload
123  */
124 static void USER_ModuleUnload( HMODULE16 hModule )
125 {
126     HOOK_FreeModuleHooks( hModule );
127     CLASS_FreeModuleClasses( hModule );
128     CURSORICON_FreeModuleIcons( hModule );
129 }
130
131 /**********************************************************************
132  *           USER_QueueCleanup
133  */
134 static void USER_QueueCleanup( HQUEUE16 hQueue )
135 {
136     if ( hQueue )
137     {
138         WND* desktop = WIN_GetDesktop();
139
140         /* Patch desktop window */
141         if ( desktop->hmemTaskQ == hQueue )
142         {
143             HTASK16 nextTask = TASK_GetNextTask( GetCurrentTask() );
144             desktop->hmemTaskQ = GetTaskQueue16( nextTask );
145         }
146
147         /* Patch resident popup menu window */
148         MENU_PatchResidentPopup( hQueue, NULL );
149
150         TIMER_RemoveQueueTimers( hQueue );
151
152         HOOK_FreeQueueHooks( hQueue );
153
154         QUEUE_SetExitingQueue( hQueue );
155         WIN_ResetQueueWindows( desktop, hQueue, (HQUEUE16)0);
156         CLIPBOARD_ResetLock( hQueue, 0 );
157         QUEUE_SetExitingQueue( 0 );
158
159         /* Free the message queue */
160         QUEUE_DeleteMsgQueue( hQueue );
161
162         WIN_ReleaseDesktop();
163     }
164 }
165
166 /**********************************************************************
167  *           USER_AppExit
168  */
169 static void USER_AppExit( HINSTANCE16 hInstance )
170 {
171     /* FIXME: empty clipboard if needed, maybe destroy menus (Windows
172      *        only complains about them but does nothing);
173      */
174
175     /* ModuleUnload() in "Internals" */
176
177     hInstance = GetExePtr( hInstance );
178     if( GetModuleUsage16( hInstance ) <= 1 ) 
179         USER_ModuleUnload( hInstance );
180 }
181
182
183 /***********************************************************************
184  *           USER_SignalProc (USER.314)
185  */
186 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
187                              UINT16 uExitFn, HINSTANCE16 hInstance,
188                              HQUEUE16 hQueue )
189 {
190     FIXME_(win)("Win 3.1 USER signal %04x\n", uCode );
191 }
192
193 /***********************************************************************
194  *           UserSignalProc     (USER.610) (USER32.559)
195  *
196  * For comments about the meaning of uCode and dwFlags 
197  * see PROCESS_CallUserSignalProc.
198  *
199  */
200 WORD WINAPI UserSignalProc( UINT uCode, DWORD dwThreadOrProcessID,
201                             DWORD dwFlags, HMODULE16 hModule )
202 {
203     HINSTANCE16 hInst;
204
205     /* FIXME: Proper reaction to most signals still missing. */
206
207     switch ( uCode )
208     {
209     case USIG_DLL_UNLOAD_WIN16:
210     case USIG_DLL_UNLOAD_WIN32:
211         USER_ModuleUnload( hModule );
212         break;
213
214     case USIG_DLL_UNLOAD_ORPHANS:
215         break;
216
217     case USIG_FAULT_DIALOG_PUSH:
218     case USIG_FAULT_DIALOG_POP:
219         break;
220
221     case USIG_THREAD_INIT:
222         break;
223
224     case USIG_THREAD_EXIT:
225         USER_QueueCleanup( GetThreadQueue16( dwThreadOrProcessID ) );
226         SetThreadQueue16( dwThreadOrProcessID, 0 );
227         break;
228
229     case USIG_PROCESS_CREATE:
230     case USIG_PROCESS_INIT:
231     case USIG_PROCESS_LOADED:
232     case USIG_PROCESS_RUNNING:
233         break;
234
235     case USIG_PROCESS_EXIT:
236         break;
237
238     case USIG_PROCESS_DESTROY:
239         hInst = GetProcessDword( dwThreadOrProcessID, GPD_HINSTANCE16 );
240         USER_AppExit( hInst );
241         break;
242
243     default:
244         FIXME_(win)("(%04x, %08lx, %04lx, %04x)\n",
245                     uCode, dwThreadOrProcessID, dwFlags, hModule );
246         break;
247     }
248
249     /* FIXME: Should chain to GdiSignalProc now. */
250
251     return 0;
252 }
253
254
255
256 /***********************************************************************
257  *           ExitWindows16   (USER.7)
258  */
259 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
260 {
261     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
262 }
263
264
265 /***********************************************************************
266  *           ExitWindowsExec16   (USER.246)
267  */
268 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
269 {
270     TRACE_(system)("Should run the following in DOS-mode: \"%s %s\"\n",
271         lpszExe, lpszParams);
272     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
273 }
274
275
276 /***********************************************************************
277  *           ExitWindowsEx   (USER32.196)
278  */
279 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reserved )
280 {
281     int i;
282     BOOL result;
283     WND **list, **ppWnd;
284         
285     /* We have to build a list of all windows first, as in EnumWindows */
286
287     if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL )))
288     {
289         WIN_ReleaseDesktop();
290         return FALSE;
291     }
292
293     /* Send a WM_QUERYENDSESSION message to every window */
294
295     for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
296     {
297         /* Make sure that the window still exists */
298         if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
299         if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
300             break;
301     }
302     result = !(*ppWnd);
303
304     /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
305
306     for (ppWnd = list; i > 0; i--, ppWnd++)
307     {
308         if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
309         SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
310     }
311     WIN_ReleaseWinArray(list);
312
313     if (result) ExitKernel16();
314     WIN_ReleaseDesktop();
315     return FALSE;
316 }
317
318
319 /***********************************************************************
320  *           ChangeDisplaySettingA    (USER32.589)
321  */
322 LONG WINAPI ChangeDisplaySettingsA( LPDEVMODEA 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 BOOL WINAPI EnumDisplaySettingsA(
355         LPCSTR name,            /* [in] huh? */
356         DWORD n,                /* [in] nth entry in display settings list*/
357         LPDEVMODEA 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 BOOL WINAPI EnumDisplaySettingsW(LPCWSTR name,DWORD n,LPDEVMODEW devmode) {
386         LPSTR nameA = HEAP_strdupWtoA(GetProcessHeap(),0,name);
387         DEVMODEA        devmodeA; 
388         BOOL ret = EnumDisplaySettingsA(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 SetEventHook16(FARPROC16 lpfnEventHook)
406 {
407         FIXME_(hook)("(lpfnEventHook=%08x): stub\n", (UINT)lpfnEventHook);
408         return NULL;
409 }
410
411 /***********************************************************************
412  *           UserSeeUserDo   (USER.216)
413  */
414 DWORD WINAPI UserSeeUserDo16(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(HANDLE hprocess,BOOL x) {
439         FIXME_(win32)("(%d,%d),stub!\n",hprocess,x);
440         return 1;
441 }
442
443 /***********************************************************************
444  *           CreateWindowStation32W   (USER32.86)
445  */
446 HWINSTA WINAPI CreateWindowStationW(
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 BOOL WINAPI SetProcessWindowStation(HWINSTA hWinSta) {
460         FIXME_(win32)("(%d),stub!\n",hWinSta);
461         return TRUE;
462 }
463
464 /***********************************************************************
465  *           SetUserObjectSecurity   (USER32.514)
466  */
467 BOOL WINAPI SetUserObjectSecurity(
468         HANDLE 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 HDESK WINAPI CreateDesktopW(
480         LPWSTR lpszDesktop,LPWSTR lpszDevice,LPDEVMODEW 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 BOOL WINAPI CloseWindowStation(HWINSTA hWinSta)
491 {
492     FIXME_(win32)("(0x%08x)\n", hWinSta);
493     return TRUE;
494 }
495 BOOL WINAPI CloseDesktop(HDESK hDesk)
496 {
497     FIXME_(win32)("(0x%08x)\n", hDesk);
498     return TRUE;
499 }
500
501 /***********************************************************************
502  *           SetWindowStationUser   (USER32.521)
503  */
504 DWORD WINAPI SetWindowStationUser(DWORD x1,DWORD x2) {
505         FIXME_(win32)("(0x%08lx,0x%08lx),stub!\n",x1,x2);
506         return 1;
507 }
508
509 /***********************************************************************
510  *           SetLogonNotifyWindow   (USER32.486)
511  */
512 DWORD WINAPI SetLogonNotifyWindow(HWINSTA hwinsta,HWND hwnd) {
513         FIXME_(win32)("(0x%x,%04x),stub!\n",hwinsta,hwnd);
514         return 1;
515 }
516
517 /***********************************************************************
518  *           LoadLocalFonts   (USER32.486)
519  */
520 VOID WINAPI LoadLocalFonts(VOID) {
521         /* are loaded. */
522         return;
523 }
524 /***********************************************************************
525  *           GetUserObjectInformation32A   (USER32.299)
526  */
527 BOOL WINAPI GetUserObjectInformationA( HANDLE hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
528 {       FIXME_(win32)("(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
529         return TRUE;
530 }
531 /***********************************************************************
532  *           GetUserObjectInformation32W   (USER32.300)
533  */
534 BOOL WINAPI GetUserObjectInformationW( HANDLE hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
535 {       FIXME_(win32)("(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
536         return TRUE;
537 }
538 /***********************************************************************
539  *           GetUserObjectSecurity32   (USER32.300)
540  */
541 BOOL WINAPI GetUserObjectSecurity(HANDLE hObj, SECURITY_INFORMATION * pSIRequested,
542         PSECURITY_DESCRIPTOR pSID, DWORD nLength, LPDWORD lpnLengthNeeded)
543 {       FIXME_(win32)("(0x%x %p %p len=%ld %p),stub!\n",  hObj, pSIRequested, pSID, nLength, lpnLengthNeeded);
544         return TRUE;
545 }
546
547 /***********************************************************************
548  *           SetSystemCursor   (USER32.507)
549  */
550 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
551 {       FIXME_(win32)("(%08x,%08lx),stub!\n",  hcur, id);
552         return TRUE;
553 }
554
555 /***********************************************************************
556  *      RegisterSystemThread    (USER32.435)
557  */
558 void WINAPI RegisterSystemThread(DWORD flags, DWORD reserved)
559 {
560         FIXME_(win32)("(%08lx, %08lx)\n", flags, reserved);
561 }