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