Fixed some of the issues reported by winapi-check.
[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         QUEUE_SetExitingQueue( 0 );
157
158         /* Free the message queue */
159         QUEUE_DeleteMsgQueue( hQueue );
160
161         WIN_ReleaseDesktop();
162     }
163 }
164
165 /**********************************************************************
166  *           USER_AppExit
167  */
168 static void USER_AppExit( HINSTANCE16 hInstance )
169 {
170     /* FIXME: maybe destroy menus (Windows only complains about them
171      * but does nothing);
172      */
173
174     /*  TODO: Start up persistant WINE X clipboard server process which will
175      *  take ownership of the X selection and continue to service selection
176      *  requests from other apps.
177      */
178
179     /* ModuleUnload() in "Internals" */
180
181     hInstance = GetExePtr( hInstance );
182     if( GetModuleUsage16( hInstance ) <= 1 ) 
183         USER_ModuleUnload( hInstance );
184 }
185
186
187 /***********************************************************************
188  *           USER_SignalProc (USER.314)
189  */
190 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
191                              UINT16 uExitFn, HINSTANCE16 hInstance,
192                              HQUEUE16 hQueue )
193 {
194     FIXME_(win)("Win 3.1 USER signal %04x\n", uCode );
195 }
196
197 /***********************************************************************
198  *           FinalUserInit (USER.400)
199  */
200 void WINAPI FinalUserInit16( void )
201 {
202     /* FIXME: Should chain to FinalGdiInit now. */
203 }
204
205 /***********************************************************************
206  *           UserSignalProc     (USER.610) (USER32.559)
207  *
208  * For comments about the meaning of uCode and dwFlags 
209  * see PROCESS_CallUserSignalProc.
210  *
211  */
212 WORD WINAPI UserSignalProc( UINT uCode, DWORD dwThreadOrProcessID,
213                             DWORD dwFlags, HMODULE16 hModule )
214 {
215     HINSTANCE16 hInst;
216
217     /* FIXME: Proper reaction to most signals still missing. */
218
219     switch ( uCode )
220     {
221     case USIG_DLL_UNLOAD_WIN16:
222     case USIG_DLL_UNLOAD_WIN32:
223         USER_ModuleUnload( hModule );
224         break;
225
226     case USIG_DLL_UNLOAD_ORPHANS:
227         break;
228
229     case USIG_FAULT_DIALOG_PUSH:
230     case USIG_FAULT_DIALOG_POP:
231         break;
232
233     case USIG_THREAD_INIT:
234         break;
235
236     case USIG_THREAD_EXIT:
237         USER_QueueCleanup( GetThreadQueue16( dwThreadOrProcessID ) );
238         SetThreadQueue16( dwThreadOrProcessID, 0 );
239         break;
240
241     case USIG_PROCESS_CREATE:
242     case USIG_PROCESS_INIT:
243     case USIG_PROCESS_LOADED:
244     case USIG_PROCESS_RUNNING:
245         break;
246
247     case USIG_PROCESS_EXIT:
248         break;
249
250     case USIG_PROCESS_DESTROY:
251         hInst = GetProcessDword( dwThreadOrProcessID, GPD_HINSTANCE16 );
252         USER_AppExit( hInst );
253         break;
254
255     default:
256         FIXME_(win)("(%04x, %08lx, %04lx, %04x)\n",
257                     uCode, dwThreadOrProcessID, dwFlags, hModule );
258         break;
259     }
260
261     /* FIXME: Should chain to GdiSignalProc now. */
262
263     return 0;
264 }
265
266
267
268 /***********************************************************************
269  *           ExitWindows16   (USER.7)
270  */
271 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
272 {
273     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
274 }
275
276
277 /***********************************************************************
278  *           ExitWindowsExec16   (USER.246)
279  */
280 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
281 {
282     TRACE_(system)("Should run the following in DOS-mode: \"%s %s\"\n",
283         lpszExe, lpszParams);
284     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
285 }
286
287
288 /***********************************************************************
289  *           ExitWindowsEx   (USER32.196)
290  */
291 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reserved )
292 {
293     int i;
294     BOOL result;
295     WND **list, **ppWnd;
296         
297     /* We have to build a list of all windows first, as in EnumWindows */
298
299     if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL )))
300     {
301         WIN_ReleaseDesktop();
302         return FALSE;
303     }
304
305     /* Send a WM_QUERYENDSESSION message to every window */
306
307     for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
308     {
309         /* Make sure that the window still exists */
310         if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
311         if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
312             break;
313     }
314     result = !(*ppWnd);
315
316     /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
317
318     for (ppWnd = list; i > 0; i--, ppWnd++)
319     {
320         if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
321         SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
322     }
323     WIN_ReleaseWinArray(list);
324
325     if (result) ExitKernel16();
326     WIN_ReleaseDesktop();
327     return FALSE;
328 }
329
330
331 /***********************************************************************
332  *           ChangeDisplaySettingA    (USER32.589)
333  */
334 LONG WINAPI ChangeDisplaySettingsA( LPDEVMODEA devmode, DWORD flags )
335 {
336   FIXME_(system)(": stub\n");
337   if (devmode==NULL)
338     FIXME_(system)("   devmode=NULL (return to default mode)\n");
339   else if ( (devmode->dmBitsPerPel != DESKTOP_GetScreenDepth()) 
340             || (devmode->dmPelsHeight != DESKTOP_GetScreenHeight())
341             || (devmode->dmPelsWidth != DESKTOP_GetScreenWidth()) )
342
343   {
344
345     if (devmode->dmFields & DM_BITSPERPEL)
346       FIXME_(system)("   bpp=%ld\n",devmode->dmBitsPerPel);
347     if (devmode->dmFields & DM_PELSWIDTH)
348       FIXME_(system)("   width=%ld\n",devmode->dmPelsWidth);
349     if (devmode->dmFields & DM_PELSHEIGHT)
350       FIXME_(system)("   height=%ld\n",devmode->dmPelsHeight);
351     FIXME_(system)(" (Putting X in this mode beforehand might help)\n"); 
352     /* we don't, but the program ... does not need to know */
353     return DISP_CHANGE_SUCCESSFUL; 
354   }
355   return DISP_CHANGE_SUCCESSFUL;
356 }
357
358 /***********************************************************************
359  *           EnumDisplaySettingsA   (USER32.592)
360  * FIXME: Currently uses static list of modes.
361  *
362  * RETURNS
363  *      TRUE if nth setting exists found (described in the LPDEVMODE32A struct)
364  *      FALSE if we do not have the nth setting
365  */
366 BOOL WINAPI EnumDisplaySettingsA(
367         LPCSTR name,            /* [in] huh? */
368         DWORD n,                /* [in] nth entry in display settings list*/
369         LPDEVMODEA devmode      /* [out] devmode for that setting */
370 ) {
371 #define NRMODES 5
372 #define NRDEPTHS 4
373         struct {
374                 int w,h;
375         } modes[NRMODES]={{512,384},{640,400},{640,480},{800,600},{1024,768}};
376         int depths[4] = {8,16,24,32};
377
378         TRACE_(system)("(%s,%ld,%p)\n",name,n,devmode);
379         if (n==0) {
380                 devmode->dmBitsPerPel = DESKTOP_GetScreenDepth();
381                 devmode->dmPelsHeight = DESKTOP_GetScreenHeight();
382                 devmode->dmPelsWidth = DESKTOP_GetScreenWidth();
383                 return TRUE;
384         }
385         if ((n-1)<NRMODES*NRDEPTHS) {
386                 devmode->dmBitsPerPel   = depths[(n-1)/NRMODES];
387                 devmode->dmPelsHeight   = modes[(n-1)%NRMODES].h;
388                 devmode->dmPelsWidth    = modes[(n-1)%NRMODES].w;
389                 return TRUE;
390         }
391         return FALSE;
392 }
393
394 /***********************************************************************
395  *           EnumDisplaySettingsW   (USER32.593)
396  */
397 BOOL WINAPI EnumDisplaySettingsW(LPCWSTR name,DWORD n,LPDEVMODEW devmode) {
398         LPSTR nameA = HEAP_strdupWtoA(GetProcessHeap(),0,name);
399         DEVMODEA        devmodeA; 
400         BOOL ret = EnumDisplaySettingsA(nameA,n,&devmodeA); 
401
402         if (ret) {
403                 devmode->dmBitsPerPel   = devmodeA.dmBitsPerPel;
404                 devmode->dmPelsHeight   = devmodeA.dmPelsHeight;
405                 devmode->dmPelsWidth    = devmodeA.dmPelsWidth;
406                 /* FIXME: convert rest too, if they are ever returned */
407         }
408         HeapFree(GetProcessHeap(),0,nameA);
409         return ret;
410 }
411
412 /***********************************************************************
413  *           EnumDisplayDevicesA   (USER32.184)
414  */
415 BOOL WINAPI EnumDisplayDevicesA(
416         LPVOID unused,DWORD i,LPDISPLAY_DEVICEA lpDisplayDevice,DWORD dwFlags
417 ) {
418         if (i)
419                 return FALSE;
420         FIXME_(system)("(%p,%ld,%p,0x%08lx), stub!\n",unused,i,lpDisplayDevice,dwFlags);
421         strcpy(lpDisplayDevice->DeviceName,"X11");
422         strcpy(lpDisplayDevice->DeviceString,"X 11 Windowing System");
423         lpDisplayDevice->StateFlags =
424                         DISPLAY_DEVICE_ATTACHED_TO_DESKTOP      |
425                         DISPLAY_DEVICE_PRIMARY_DEVICE           |
426                         DISPLAY_DEVICE_VGA_COMPATIBLE;
427         return TRUE;
428 }
429
430 /***********************************************************************
431  *           EnumDisplayDevicesW   (USER32.185)
432  */
433 BOOL WINAPI EnumDisplayDevicesW(
434         LPVOID unused,DWORD i,LPDISPLAY_DEVICEW lpDisplayDevice,DWORD dwFlags
435 ) {
436         if (i)
437                 return FALSE;
438         FIXME_(system)("(%p,%ld,%p,0x%08lx), stub!\n",unused,i,lpDisplayDevice,dwFlags);
439         lstrcpyAtoW(lpDisplayDevice->DeviceName,"X11");
440         lstrcpyAtoW(lpDisplayDevice->DeviceString,"X 11 Windowing System");
441         lpDisplayDevice->StateFlags =
442                         DISPLAY_DEVICE_ATTACHED_TO_DESKTOP      |
443                         DISPLAY_DEVICE_PRIMARY_DEVICE           |
444                         DISPLAY_DEVICE_VGA_COMPATIBLE;
445         return TRUE;
446 }
447
448 /***********************************************************************
449  *           SetEventHook   (USER.321)
450  *
451  *      Used by Turbo Debugger for Windows
452  */
453 FARPROC16 WINAPI SetEventHook16(FARPROC16 lpfnEventHook)
454 {
455         FIXME_(hook)("(lpfnEventHook=%08x): stub\n", (UINT)lpfnEventHook);
456         return NULL;
457 }
458
459 /***********************************************************************
460  *           UserSeeUserDo   (USER.216)
461  */
462 DWORD WINAPI UserSeeUserDo16(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
463 {
464     switch (wReqType)
465     {
466     case USUD_LOCALALLOC:
467         return LOCAL_Alloc(USER_HeapSel, wParam1, wParam3);
468     case USUD_LOCALFREE:
469         return LOCAL_Free(USER_HeapSel, wParam1);
470     case USUD_LOCALCOMPACT:
471         return LOCAL_Compact(USER_HeapSel, wParam3, 0);
472     case USUD_LOCALHEAP:
473         return USER_HeapSel;
474     case USUD_FIRSTCLASS:
475         FIXME_(local)("return a pointer to the first window class.\n"); 
476         return (DWORD)-1;
477     default:
478         WARN_(local)("wReqType %04x (unknown)", wReqType);
479         return (DWORD)-1;
480     }
481 }
482
483 /***********************************************************************
484  *         GetSystemDebugState16   (USER.231)
485  */
486 WORD WINAPI GetSystemDebugState16(void)
487 {
488     return 0;  /* FIXME */
489 }
490
491 /***********************************************************************
492  *           RegisterLogonProcess   (USER32.434)
493  */
494 DWORD WINAPI RegisterLogonProcess(HANDLE hprocess,BOOL x) {
495         FIXME_(win32)("(%d,%d),stub!\n",hprocess,x);
496         return 1;
497 }
498
499 /***********************************************************************
500  *           CreateWindowStation32W   (USER32.86)
501  */
502 HWINSTA WINAPI CreateWindowStationW(
503         LPWSTR winstation,DWORD res1,DWORD desiredaccess,
504         LPSECURITY_ATTRIBUTES lpsa
505 ) {
506         FIXME_(win32)("(%s,0x%08lx,0x%08lx,%p),stub!\n",debugstr_w(winstation),
507                 res1,desiredaccess,lpsa
508         );
509         return 0xdeadcafe;
510 }
511
512 /***********************************************************************
513  *           SetProcessWindowStation   (USER32.496)
514  */
515 BOOL WINAPI SetProcessWindowStation(HWINSTA hWinSta) {
516         FIXME_(win32)("(%d),stub!\n",hWinSta);
517         return TRUE;
518 }
519
520 /***********************************************************************
521  *           SetUserObjectSecurity   (USER32.514)
522  */
523 BOOL WINAPI SetUserObjectSecurity(
524         HANDLE hObj,
525         /*LPSECURITY_INFORMATION*/LPVOID pSIRequested,
526         PSECURITY_DESCRIPTOR pSID
527 ) {
528         FIXME_(win32)("(0x%08x,%p,%p),stub!\n",hObj,pSIRequested,pSID);
529         return TRUE;
530 }
531
532 /***********************************************************************
533  *           CreateDesktop32W   (USER32.69)
534  */
535 HDESK WINAPI CreateDesktopW(
536         LPWSTR lpszDesktop,LPWSTR lpszDevice,LPDEVMODEW pDevmode,
537         DWORD dwFlags,DWORD dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa
538 ) {
539         FIXME_(win32)("(%s,%s,%p,0x%08lx,0x%08lx,%p),stub!\n",
540                 debugstr_w(lpszDesktop),debugstr_w(lpszDevice),pDevmode,
541                 dwFlags,dwDesiredAccess,lpsa
542         );
543         return 0xcafedead;
544 }
545
546 BOOL WINAPI CloseWindowStation(HWINSTA hWinSta)
547 {
548     FIXME_(win32)("(0x%08x)\n", hWinSta);
549     return TRUE;
550 }
551 BOOL WINAPI CloseDesktop(HDESK hDesk)
552 {
553     FIXME_(win32)("(0x%08x)\n", hDesk);
554     return TRUE;
555 }
556
557 /***********************************************************************
558  *           SetWindowStationUser   (USER32.521)
559  */
560 DWORD WINAPI SetWindowStationUser(DWORD x1,DWORD x2) {
561         FIXME_(win32)("(0x%08lx,0x%08lx),stub!\n",x1,x2);
562         return 1;
563 }
564
565 /***********************************************************************
566  *           SetLogonNotifyWindow   (USER32.486)
567  */
568 DWORD WINAPI SetLogonNotifyWindow(HWINSTA hwinsta,HWND hwnd) {
569         FIXME_(win32)("(0x%x,%04x),stub!\n",hwinsta,hwnd);
570         return 1;
571 }
572
573 /***********************************************************************
574  *           LoadLocalFonts   (USER32.486)
575  */
576 VOID WINAPI LoadLocalFonts(VOID) {
577         /* are loaded. */
578         return;
579 }
580 /***********************************************************************
581  *           GetUserObjectInformation32A   (USER32.299)
582  */
583 BOOL WINAPI GetUserObjectInformationA( HANDLE hObj, INT nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
584 {       FIXME_(win32)("(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
585         return TRUE;
586 }
587 /***********************************************************************
588  *           GetUserObjectInformation32W   (USER32.300)
589  */
590 BOOL WINAPI GetUserObjectInformationW( HANDLE hObj, INT nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
591 {       FIXME_(win32)("(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
592         return TRUE;
593 }
594 /***********************************************************************
595  *           GetUserObjectSecurity32   (USER32.300)
596  */
597 BOOL WINAPI GetUserObjectSecurity(HANDLE hObj, SECURITY_INFORMATION * pSIRequested,
598         PSECURITY_DESCRIPTOR pSID, DWORD nLength, LPDWORD lpnLengthNeeded)
599 {       FIXME_(win32)("(0x%x %p %p len=%ld %p),stub!\n",  hObj, pSIRequested, pSID, nLength, lpnLengthNeeded);
600         return TRUE;
601 }
602
603 /***********************************************************************
604  *           SetSystemCursor   (USER32.507)
605  */
606 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
607 {       FIXME_(win32)("(%08x,%08lx),stub!\n",  hcur, id);
608         return TRUE;
609 }
610
611 /***********************************************************************
612  *      RegisterSystemThread    (USER32.435)
613  */
614 void WINAPI RegisterSystemThread(DWORD flags, DWORD reserved)
615 {
616         FIXME_(win32)("(%08lx, %08lx)\n", flags, reserved);
617 }