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