Remove X_DC_INFO from DC and make it behave like any other PDEVICE.
[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 "windows.h"
10 #include "resource.h"
11 #include "heap.h"
12 #include "gdi.h"
13 #include "user.h"
14 #include "task.h"
15 #include "queue.h"
16 #include "win.h"
17 #include "clipboard.h"
18 #include "menu.h"
19 #include "hook.h"
20 #include "debug.h"
21 #include "toolhelp.h"
22 #include "message.h"
23 #include "module.h"
24 #include "miscemu.h"
25 #include "queue.h"
26 #include "shell.h"
27 #include "callback.h"
28 #include "x11drv.h"
29
30 /***********************************************************************
31  *           GetFreeSystemResources   (USER.284)
32  */
33 WORD WINAPI GetFreeSystemResources( WORD resType )
34 {
35     int userPercent, gdiPercent;
36
37     switch(resType)
38     {
39     case GFSR_USERRESOURCES:
40         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
41                                LOCAL_HeapSize( USER_HeapSel );
42         gdiPercent  = 100;
43         break;
44
45     case GFSR_GDIRESOURCES:
46         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
47                                LOCAL_HeapSize( GDI_HeapSel );
48         userPercent = 100;
49         break;
50
51     case GFSR_SYSTEMRESOURCES:
52         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
53                                LOCAL_HeapSize( USER_HeapSel );
54         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
55                                LOCAL_HeapSize( GDI_HeapSel );
56         break;
57
58     default:
59         return 0;
60     }
61     return (WORD)MIN( userPercent, gdiPercent );
62 }
63
64
65 /***********************************************************************
66  *           SystemHeapInfo   (TOOLHELP.71)
67  */
68 BOOL16 WINAPI SystemHeapInfo( SYSHEAPINFO *pHeapInfo )
69 {
70     pHeapInfo->wUserFreePercent = GetFreeSystemResources( GFSR_USERRESOURCES );
71     pHeapInfo->wGDIFreePercent  = GetFreeSystemResources( GFSR_GDIRESOURCES );
72     pHeapInfo->hUserSegment = USER_HeapSel;
73     pHeapInfo->hGDISegment  = GDI_HeapSel;
74     return TRUE;
75 }
76
77
78 /***********************************************************************
79  *           TimerCount   (TOOLHELP.80)
80  */
81 BOOL16 WINAPI TimerCount( TIMERINFO *pTimerInfo )
82 {
83     /* FIXME
84      * In standard mode, dwmsSinceStart = dwmsThisVM 
85      *
86      * I tested this, under Windows in enhanced mode, and
87      * if you never switch VM (ie start/stop DOS) these
88      * values should be the same as well. 
89      *
90      * Also, Wine should adjust for the hardware timer
91      * to reduce the amount of error to ~1ms. 
92      * I can't be bothered, can you?
93      */
94     pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
95     return TRUE;
96 }
97
98 static FARPROC16 __r16loader = NULL;
99
100 /**********************************************************************
101  *           USER_CallDefaultRsrcHandler
102  *
103  * Called by the LoadDIBIcon/CursorHandler().
104  */
105 HGLOBAL16 USER_CallDefaultRsrcHandler( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
106 {
107     return Callbacks->CallResourceHandlerProc( __r16loader, hMemObj, hModule, hRsrc );
108 }
109
110 /**********************************************************************
111  *           USER_InstallRsrcHandler
112  */
113 static void USER_InstallRsrcHandler( HINSTANCE16 hInstance )
114 {
115     FARPROC16 proc;
116
117     /* SetResourceHandler() returns previous function which is set
118      * when a module's resource table is loaded. */
119
120     proc = SetResourceHandler( hInstance, RT_ICON16,
121                                MODULE_GetWndProcEntry16("LoadDIBIconHandler") );
122     if (!__r16loader) __r16loader = proc;
123
124     proc = SetResourceHandler( hInstance, RT_CURSOR16,
125                                MODULE_GetWndProcEntry16("LoadDIBCursorHandler") );
126     if (!__r16loader) __r16loader = proc;
127 }
128
129 /**********************************************************************
130  *           InitApp   (USER.5)
131  */
132 INT16 WINAPI InitApp( HINSTANCE16 hInstance )
133 {
134     int queueSize;
135
136       /* InitTask() calls LibMain()'s of implicitly loaded DLLs 
137        * prior to InitApp() so there is no clean way to do
138        * SetTaskSignalHandler() in time. So, broken Windows bypasses 
139        * a pTask->userhandler on startup and simply calls a global 
140        * function pointer to the default USER signal handler.
141        */
142
143     USER_InstallRsrcHandler( hInstance );
144
145     /* Hack: restore the divide-by-zero handler */
146     /* FIXME: should set a USER-specific handler that displays a msg box */
147     INT_SetPMHandler( 0, INT_GetPMHandler( 0xff ) );
148
149       /* Create task message queue */
150     queueSize = GetProfileInt32A( "windows", "DefaultQueueSize", 8 );
151     if (!SetMessageQueue32( queueSize )) return 0;
152
153     return 1;
154 }
155
156 /**********************************************************************
157  *           USER_ModuleUnload
158  */
159 static void USER_ModuleUnload( HMODULE16 hModule )
160 {
161     HOOK_FreeModuleHooks( hModule );
162     CLASS_FreeModuleClasses( hModule );
163 }
164
165 /**********************************************************************
166  *           USER_AppExit
167  */
168 static void USER_AppExit( HTASK16 hTask, HINSTANCE16 hInstance, HQUEUE16 hQueue )
169 {
170     /* FIXME: empty clipboard if needed, maybe destroy menus (Windows
171      *        only complains about them but does nothing);
172      */
173
174     WND* desktop = WIN_GetDesktop();
175
176     /* Patch desktop window */
177     if( desktop->hmemTaskQ == hQueue )
178         desktop->hmemTaskQ = GetTaskQueue(TASK_GetNextTask(hTask));
179
180     /* Patch resident popup menu window */
181     MENU_PatchResidentPopup( hQueue, NULL );
182
183     TIMER_RemoveQueueTimers( hQueue );
184
185     QUEUE_FlushMessages( hQueue );
186     HOOK_FreeQueueHooks( hQueue );
187
188     QUEUE_SetExitingQueue( hQueue );
189     WIN_ResetQueueWindows( desktop, hQueue, (HQUEUE16)0);
190     CLIPBOARD_ResetLock( hQueue, 0 );
191     QUEUE_SetExitingQueue( 0 );
192
193     /* Free the message queue */
194
195     QUEUE_DeleteMsgQueue( hQueue );
196
197     /* ModuleUnload() in "Internals" */
198
199     hInstance = GetExePtr( hInstance );
200     if( GetModuleUsage( hInstance ) <= 1 ) 
201         USER_ModuleUnload( hInstance );
202 }
203
204
205 /***********************************************************************
206  *           USER_ExitWindows
207  *
208  * Clean-up everything and exit the Wine process.
209  * This is the back-end of ExitWindows(), called when all windows
210  * have agreed to be terminated.
211  */
212 void USER_ExitWindows(void)
213 {
214     /* Do the clean-up stuff */
215
216     WriteOutProfiles();
217     SHELL_SaveRegistry();
218
219     exit(0);
220 }
221
222
223 /***********************************************************************
224  *           USER_SignalProc (USER.314)
225  */
226 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
227                              UINT16 uExitFn, HINSTANCE16 hInstance,
228                              HQUEUE16 hQueue )
229 {
230     switch( uCode )
231     {
232         case USIG_GPF:
233         case USIG_TERMINATION:
234              USER_AppExit( hTaskOrModule, hInstance, hQueue ); /* task */
235              break;
236
237         case USIG_DLL_LOAD:
238              USER_InstallRsrcHandler( hTaskOrModule ); /* module */
239              break;
240
241         case USIG_DLL_UNLOAD:
242              USER_ModuleUnload( hTaskOrModule ); /* module */
243              break;
244
245         default:
246              FIXME(msg,"Unimplemented USER signal: %i\n", (int)uCode );
247     }
248 }
249
250
251 /***********************************************************************
252  *           ExitWindows16   (USER.7)
253  */
254 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
255 {
256     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
257 }
258
259
260 /***********************************************************************
261  *           ExitWindowsExec16   (USER.246)
262  */
263 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
264 {
265     TRACE(system, "Should run the following in DOS-mode: \"%s %s\"\n",
266         lpszExe, lpszParams);
267     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
268 }
269
270
271 /***********************************************************************
272  *           ExitWindowsEx   (USER32.196)
273  */
274 BOOL32 WINAPI ExitWindowsEx( UINT32 flags, DWORD reserved )
275 {
276     int i;
277     BOOL32 result;
278     WND **list, **ppWnd;
279         
280     /* We have to build a list of all windows first, as in EnumWindows */
281
282     if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL ))) return FALSE;
283
284     /* Send a WM_QUERYENDSESSION message to every window */
285
286     for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
287     {
288         /* Make sure that the window still exists */
289         if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
290         if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
291             break;
292     }
293     result = !(*ppWnd);
294
295     /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
296
297     for (ppWnd = list; i > 0; i--, ppWnd++)
298     {
299         if (!IsWindow32( (*ppWnd)->hwndSelf )) continue;
300         SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
301     }
302     HeapFree( SystemHeap, 0, list );
303
304     if (result) USER_ExitWindows();
305     return FALSE;
306 }
307
308
309 /***********************************************************************
310  *           ChangeDisplaySettingA    (USER32.589)
311  */
312 LONG WINAPI ChangeDisplaySettings32A( LPDEVMODE32A devmode, DWORD flags )
313 {
314   FIXME(system, ": stub\n");
315   if (devmode==NULL)
316     FIXME(system,"   devmode=NULL (return to default mode)\n");
317   else if ( (devmode->dmBitsPerPel != DefaultDepthOfScreen(screen)) 
318             || (devmode->dmPelsHeight != screenHeight)
319             || (devmode->dmPelsWidth != screenWidth) )
320
321   {
322
323     if (devmode->dmFields & DM_BITSPERPEL)
324       FIXME(system,"   bpp=%ld\n",devmode->dmBitsPerPel);
325     if (devmode->dmFields & DM_PELSWIDTH)
326       FIXME(system,"   width=%ld\n",devmode->dmPelsWidth);
327     if (devmode->dmFields & DM_PELSHEIGHT)
328       FIXME(system,"   height=%ld\n",devmode->dmPelsHeight);
329     FIXME(system," (Putting X in this mode beforehand might help)\n"); 
330     /* we don't, but the program ... does not need to know */
331     return DISP_CHANGE_SUCCESSFUL; 
332   }
333   return DISP_CHANGE_SUCCESSFUL;
334 }
335
336 /***********************************************************************
337  *           EnumDisplaySettingsA   (USER32.592)
338  * FIXME: Currently uses static list of modes.
339  *
340  * RETURNS
341  *      TRUE if nth setting exists found (described in the LPDEVMODE32A struct)
342  *      FALSE if we do not have the nth setting
343  */
344 BOOL32 WINAPI EnumDisplaySettings32A(
345         LPCSTR name,            /* [in] huh? */
346         DWORD n,                /* [in] nth entry in display settings list*/
347         LPDEVMODE32A devmode    /* [out] devmode for that setting */
348 ) {
349 #define NRMODES 5
350 #define NRDEPTHS 4
351         struct {
352                 int w,h;
353         } modes[NRMODES]={{512,384},{640,400},{640,480},{800,600},{1024,768}};
354         int depths[4] = {8,16,24,32};
355
356         TRACE(system,"(%s,%ld,%p)\n",name,n,devmode);
357         if (n==0) {
358                 devmode->dmBitsPerPel = DefaultDepthOfScreen(screen);
359                 devmode->dmPelsHeight = screenHeight;
360                 devmode->dmPelsWidth = screenWidth;
361                 return TRUE;
362         }
363         if ((n-1)<NRMODES*NRDEPTHS) {
364                 devmode->dmBitsPerPel   = depths[(n-1)/NRMODES];
365                 devmode->dmPelsHeight   = modes[(n-1)%NRMODES].h;
366                 devmode->dmPelsWidth    = modes[(n-1)%NRMODES].w;
367                 return TRUE;
368         }
369         return FALSE;
370 }
371
372 /***********************************************************************
373  *           EnumDisplaySettingsW   (USER32.593)
374  */
375 BOOL32 WINAPI EnumDisplaySettings32W(LPCWSTR name,DWORD n,LPDEVMODE32W devmode) {
376         LPSTR nameA = HEAP_strdupWtoA(GetProcessHeap(),0,name);
377         DEVMODE32A      devmodeA; 
378         BOOL32 ret = EnumDisplaySettings32A(nameA,n,&devmodeA); 
379
380         if (ret) {
381                 devmode->dmBitsPerPel   = devmodeA.dmBitsPerPel;
382                 devmode->dmPelsHeight   = devmodeA.dmPelsHeight;
383                 devmode->dmPelsWidth    = devmodeA.dmPelsWidth;
384                 /* FIXME: convert rest too, if they are ever returned */
385         }
386         HeapFree(GetProcessHeap(),0,nameA);
387         return ret;
388 }
389
390 /***********************************************************************
391  *           SetEventHook   (USER.321)
392  *
393  *      Used by Turbo Debugger for Windows
394  */
395 FARPROC16 WINAPI SetEventHook(FARPROC16 lpfnEventHook)
396 {
397         FIXME(hook, "(lpfnEventHook=%08x): stub\n", (UINT32)lpfnEventHook);
398         return NULL;
399 }
400
401 /***********************************************************************
402  *           UserSeeUserDo   (USER.216)
403  */
404 DWORD WINAPI UserSeeUserDo(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
405 {
406     switch (wReqType)
407     {
408     case USUD_LOCALALLOC:
409         return LOCAL_Alloc(USER_HeapSel, wParam1, wParam3);
410     case USUD_LOCALFREE:
411         return LOCAL_Free(USER_HeapSel, wParam1);
412     case USUD_LOCALCOMPACT:
413         return LOCAL_Compact(USER_HeapSel, wParam3, 0);
414     case USUD_LOCALHEAP:
415         return USER_HeapSel;
416     case USUD_FIRSTCLASS:
417         FIXME(local, "return a pointer to the first window class.\n"); 
418         return (DWORD)-1;
419     default:
420         WARN(local, "wReqType %04x (unknown)", wReqType);
421         return (DWORD)-1;
422     }
423 }
424
425 /***********************************************************************
426  *           RegisterLogonProcess   (USER32.434)
427  */
428 DWORD WINAPI RegisterLogonProcess(HANDLE32 hprocess,BOOL32 x) {
429         FIXME(win32,"(%d,%d),stub!\n",hprocess,x);
430         return 1;
431 }
432
433 /***********************************************************************
434  *           CreateWindowStation32W   (USER32.86)
435  */
436 HWINSTA32 WINAPI CreateWindowStation32W(
437         LPWSTR winstation,DWORD res1,DWORD desiredaccess,
438         LPSECURITY_ATTRIBUTES lpsa
439 ) {
440         FIXME(win32,"(%s,0x%08lx,0x%08lx,%p),stub!\n",debugstr_w(winstation),
441                 res1,desiredaccess,lpsa
442         );
443         return 0xdeadcafe;
444 }
445
446 /***********************************************************************
447  *           SetProcessWindowStation   (USER32.496)
448  */
449 BOOL32 WINAPI SetProcessWindowStation(HWINSTA32 hWinSta) {
450         FIXME(win32,"(%d),stub!\n",hWinSta);
451         return TRUE;
452 }
453
454 /***********************************************************************
455  *           SetUserObjectSecurity   (USER32.514)
456  */
457 BOOL32 WINAPI SetUserObjectSecurity(
458         HANDLE32 hObj,
459         /*LPSECURITY_INFORMATION*/LPVOID pSIRequested,
460         /*LPSECURITY_DESCRIPTOR*/LPVOID pSID
461 ) {
462         FIXME(win32,"(%d,%p,%p),stub!\n",hObj,pSIRequested,pSID);
463         return TRUE;
464 }
465
466 /***********************************************************************
467  *           CreateDesktop32W   (USER32.69)
468  */
469 HDESK32 WINAPI CreateDesktop32W(
470         LPWSTR lpszDesktop,LPWSTR lpszDevice,LPDEVMODE32W pDevmode,
471         DWORD dwFlags,DWORD dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa
472 ) {
473         FIXME(win32,"(%s,%s,%p,0x%08lx,0x%08lx,%p),stub!\n",
474                 debugstr_w(lpszDesktop),debugstr_w(lpszDevice),pDevmode,
475                 dwFlags,dwDesiredAccess,lpsa
476         );
477         return 0xcafedead;
478 }
479
480 /***********************************************************************
481  *           SetWindowStationUser   (USER32.521)
482  */
483 DWORD WINAPI SetWindowStationUser(DWORD x1,DWORD x2) {
484         FIXME(win32,"(%ld,%ld),stub!\n",x1,x2);
485         return 1;
486 }
487
488 /***********************************************************************
489  *           SetLogonNotifyWindow   (USER32.486)
490  */
491 DWORD WINAPI SetLogonNotifyWindow(HWINSTA32 hwinsta,HWND32 hwnd) {
492         FIXME(win32,"(0x%x,%ld),stub!\n",hwinsta,hwnd);
493         return 1;
494 }
495
496 /***********************************************************************
497  *           LoadLocalFonts   (USER32.486)
498  */
499 VOID WINAPI LoadLocalFonts(VOID) {
500         /* are loaded. */
501         return;
502 }
503 /***********************************************************************
504  *           GetUserObjectInformation32A   (USER32.299)
505  */
506 BOOL32 WINAPI GetUserObjectInformation32A( HANDLE32 hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
507 {       FIXME(win32,"(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
508         return TRUE;
509 }
510 /***********************************************************************
511  *           GetUserObjectInformation32W   (USER32.300)
512  */
513 BOOL32 WINAPI GetUserObjectInformation32W( HANDLE32 hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
514 {       FIXME(win32,"(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
515         return TRUE;
516 }