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