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