Add support for anonymous struct/unions on compilers that implement it.
[wine.git] / windows / user.c
blob9297ee75972d436f2df5f7a5713742a457d8914e
1 /*
2 * Misc. USER functions
4 * Copyright 1993 Robert J. Amstadt
5 * 1996 Alex Korobka
6 */
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"
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)
38 /***********************************************************************
39 * GetFreeSystemResources (USER.284)
41 WORD WINAPI GetFreeSystemResources16( WORD resType )
43 int userPercent, gdiPercent;
45 switch(resType)
47 case GFSR_USERRESOURCES:
48 userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
49 LOCAL_HeapSize( USER_HeapSel );
50 gdiPercent = 100;
51 break;
53 case GFSR_GDIRESOURCES:
54 gdiPercent = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
55 LOCAL_HeapSize( GDI_HeapSel );
56 userPercent = 100;
57 break;
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;
66 default:
67 return 0;
69 return (WORD)MIN( userPercent, gdiPercent );
73 /***********************************************************************
74 * SystemHeapInfo (TOOLHELP.71)
76 BOOL16 WINAPI SystemHeapInfo16( SYSHEAPINFO *pHeapInfo )
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;
86 /***********************************************************************
87 * TimerCount (TOOLHELP.80)
89 BOOL16 WINAPI TimerCount16( TIMERINFO *pTimerInfo )
91 /* FIXME
92 * In standard mode, dwmsSinceStart = dwmsThisVM
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.
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?
102 pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
103 return TRUE;
106 /**********************************************************************
107 * InitApp (USER.5)
109 INT16 WINAPI InitApp16( HINSTANCE16 hInstance )
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 ) );
115 /* Create task message queue */
116 if ( !GetFastQueue16() ) return 0;
118 return 1;
121 /**********************************************************************
122 * USER_ModuleUnload
124 static void USER_ModuleUnload( HMODULE16 hModule )
126 HOOK_FreeModuleHooks( hModule );
127 CLASS_FreeModuleClasses( hModule );
128 CURSORICON_FreeModuleIcons( hModule );
131 /**********************************************************************
132 * USER_QueueCleanup
134 static void USER_QueueCleanup( HQUEUE16 hQueue )
136 if ( hQueue )
138 WND* desktop = WIN_GetDesktop();
140 /* Patch desktop window */
141 if ( desktop->hmemTaskQ == hQueue )
143 HTASK16 nextTask = TASK_GetNextTask( GetCurrentTask() );
144 desktop->hmemTaskQ = GetTaskQueue16( nextTask );
147 /* Patch resident popup menu window */
148 MENU_PatchResidentPopup( hQueue, NULL );
150 TIMER_RemoveQueueTimers( hQueue );
152 HOOK_FreeQueueHooks( hQueue );
154 QUEUE_SetExitingQueue( hQueue );
155 WIN_ResetQueueWindows( desktop, hQueue, (HQUEUE16)0);
156 QUEUE_SetExitingQueue( 0 );
158 /* Free the message queue */
159 QUEUE_DeleteMsgQueue( hQueue );
161 WIN_ReleaseDesktop();
165 /**********************************************************************
166 * USER_AppExit
168 static void USER_AppExit( HINSTANCE16 hInstance )
170 /* FIXME: maybe destroy menus (Windows only complains about them
171 * but does nothing);
174 /* TODO: Start up persistant WINE X clipboard server process which will
175 * take ownership of the X selection and continue to service selection
176 * requests from other apps.
179 /* ModuleUnload() in "Internals" */
181 hInstance = GetExePtr( hInstance );
182 if( GetModuleUsage16( hInstance ) <= 1 )
183 USER_ModuleUnload( hInstance );
187 /***********************************************************************
188 * USER_SignalProc (USER.314)
190 void WINAPI USER_SignalProc( HANDLE16 hTaskOrModule, UINT16 uCode,
191 UINT16 uExitFn, HINSTANCE16 hInstance,
192 HQUEUE16 hQueue )
194 FIXME_(win)("Win 3.1 USER signal %04x\n", uCode );
197 /***********************************************************************
198 * FinalUserInit (USER.400)
200 void WINAPI FinalUserInit16( void )
202 /* FIXME: Should chain to FinalGdiInit now. */
205 /***********************************************************************
206 * UserSignalProc (USER.610) (USER32.559)
208 * For comments about the meaning of uCode and dwFlags
209 * see PROCESS_CallUserSignalProc.
212 WORD WINAPI UserSignalProc( UINT uCode, DWORD dwThreadOrProcessID,
213 DWORD dwFlags, HMODULE16 hModule )
215 HINSTANCE16 hInst;
217 /* FIXME: Proper reaction to most signals still missing. */
219 switch ( uCode )
221 case USIG_DLL_UNLOAD_WIN16:
222 case USIG_DLL_UNLOAD_WIN32:
223 USER_ModuleUnload( hModule );
224 break;
226 case USIG_DLL_UNLOAD_ORPHANS:
227 break;
229 case USIG_FAULT_DIALOG_PUSH:
230 case USIG_FAULT_DIALOG_POP:
231 break;
233 case USIG_THREAD_INIT:
234 break;
236 case USIG_THREAD_EXIT:
237 USER_QueueCleanup( GetThreadQueue16( dwThreadOrProcessID ) );
238 SetThreadQueue16( dwThreadOrProcessID, 0 );
239 break;
241 case USIG_PROCESS_CREATE:
242 case USIG_PROCESS_INIT:
243 case USIG_PROCESS_LOADED:
244 case USIG_PROCESS_RUNNING:
245 break;
247 case USIG_PROCESS_EXIT:
248 break;
250 case USIG_PROCESS_DESTROY:
251 hInst = GetProcessDword( dwThreadOrProcessID, GPD_HINSTANCE16 );
252 USER_AppExit( hInst );
253 break;
255 default:
256 FIXME_(win)("(%04x, %08lx, %04lx, %04x)\n",
257 uCode, dwThreadOrProcessID, dwFlags, hModule );
258 break;
261 /* FIXME: Should chain to GdiSignalProc now. */
263 return 0;
268 /***********************************************************************
269 * ExitWindows16 (USER.7)
271 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
273 return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
277 /***********************************************************************
278 * ExitWindowsExec16 (USER.246)
280 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
282 TRACE_(system)("Should run the following in DOS-mode: \"%s %s\"\n",
283 lpszExe, lpszParams);
284 return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
288 /***********************************************************************
289 * ExitWindowsEx (USER32.196)
291 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reserved )
293 int i;
294 BOOL result;
295 WND **list, **ppWnd;
297 /* We have to build a list of all windows first, as in EnumWindows */
299 if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL )))
301 WIN_ReleaseDesktop();
302 return FALSE;
305 /* Send a WM_QUERYENDSESSION message to every window */
307 for (ppWnd = list, i = 0; *ppWnd; ppWnd++, i++)
309 /* Make sure that the window still exists */
310 if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
311 if (!SendMessage16( (*ppWnd)->hwndSelf, WM_QUERYENDSESSION, 0, 0 ))
312 break;
314 result = !(*ppWnd);
316 /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
318 for (ppWnd = list; i > 0; i--, ppWnd++)
320 if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
321 SendMessage16( (*ppWnd)->hwndSelf, WM_ENDSESSION, result, 0 );
323 WIN_ReleaseWinArray(list);
325 if (result) ExitKernel16();
326 WIN_ReleaseDesktop();
327 return FALSE;
331 /***********************************************************************
332 * ChangeDisplaySettingA (USER32.589)
334 LONG WINAPI ChangeDisplaySettingsA( LPDEVMODEA devmode, DWORD flags )
336 FIXME_(system)(": stub\n");
337 if (devmode==NULL)
338 FIXME_(system)(" devmode=NULL (return to default mode)\n");
339 else if ( (devmode->dmBitsPerPel != DESKTOP_GetScreenDepth())
340 || (devmode->dmPelsHeight != DESKTOP_GetScreenHeight())
341 || (devmode->dmPelsWidth != DESKTOP_GetScreenWidth()) )
345 if (devmode->dmFields & DM_BITSPERPEL)
346 FIXME_(system)(" bpp=%ld\n",devmode->dmBitsPerPel);
347 if (devmode->dmFields & DM_PELSWIDTH)
348 FIXME_(system)(" width=%ld\n",devmode->dmPelsWidth);
349 if (devmode->dmFields & DM_PELSHEIGHT)
350 FIXME_(system)(" height=%ld\n",devmode->dmPelsHeight);
351 FIXME_(system)(" (Putting X in this mode beforehand might help)\n");
352 /* we don't, but the program ... does not need to know */
353 return DISP_CHANGE_SUCCESSFUL;
355 return DISP_CHANGE_SUCCESSFUL;
358 /***********************************************************************
359 * EnumDisplaySettingsA (USER32.592)
360 * FIXME: Currently uses static list of modes.
362 * RETURNS
363 * TRUE if nth setting exists found (described in the LPDEVMODE32A struct)
364 * FALSE if we do not have the nth setting
366 BOOL WINAPI EnumDisplaySettingsA(
367 LPCSTR name, /* [in] huh? */
368 DWORD n, /* [in] nth entry in display settings list*/
369 LPDEVMODEA devmode /* [out] devmode for that setting */
371 #define NRMODES 5
372 #define NRDEPTHS 4
373 struct {
374 int w,h;
375 } modes[NRMODES]={{512,384},{640,400},{640,480},{800,600},{1024,768}};
376 int depths[4] = {8,16,24,32};
378 TRACE_(system)("(%s,%ld,%p)\n",name,n,devmode);
379 if (n==0) {
380 devmode->dmBitsPerPel = DESKTOP_GetScreenDepth();
381 devmode->dmPelsHeight = DESKTOP_GetScreenHeight();
382 devmode->dmPelsWidth = DESKTOP_GetScreenWidth();
383 return TRUE;
385 if ((n-1)<NRMODES*NRDEPTHS) {
386 devmode->dmBitsPerPel = depths[(n-1)/NRMODES];
387 devmode->dmPelsHeight = modes[(n-1)%NRMODES].h;
388 devmode->dmPelsWidth = modes[(n-1)%NRMODES].w;
389 return TRUE;
391 return FALSE;
394 /***********************************************************************
395 * EnumDisplaySettingsW (USER32.593)
397 BOOL WINAPI EnumDisplaySettingsW(LPCWSTR name,DWORD n,LPDEVMODEW devmode) {
398 LPSTR nameA = HEAP_strdupWtoA(GetProcessHeap(),0,name);
399 DEVMODEA devmodeA;
400 BOOL ret = EnumDisplaySettingsA(nameA,n,&devmodeA);
402 if (ret) {
403 devmode->dmBitsPerPel = devmodeA.dmBitsPerPel;
404 devmode->dmPelsHeight = devmodeA.dmPelsHeight;
405 devmode->dmPelsWidth = devmodeA.dmPelsWidth;
406 /* FIXME: convert rest too, if they are ever returned */
408 HeapFree(GetProcessHeap(),0,nameA);
409 return ret;
412 /***********************************************************************
413 * SetEventHook (USER.321)
415 * Used by Turbo Debugger for Windows
417 FARPROC16 WINAPI SetEventHook16(FARPROC16 lpfnEventHook)
419 FIXME_(hook)("(lpfnEventHook=%08x): stub\n", (UINT)lpfnEventHook);
420 return NULL;
423 /***********************************************************************
424 * UserSeeUserDo (USER.216)
426 DWORD WINAPI UserSeeUserDo16(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
428 switch (wReqType)
430 case USUD_LOCALALLOC:
431 return LOCAL_Alloc(USER_HeapSel, wParam1, wParam3);
432 case USUD_LOCALFREE:
433 return LOCAL_Free(USER_HeapSel, wParam1);
434 case USUD_LOCALCOMPACT:
435 return LOCAL_Compact(USER_HeapSel, wParam3, 0);
436 case USUD_LOCALHEAP:
437 return USER_HeapSel;
438 case USUD_FIRSTCLASS:
439 FIXME_(local)("return a pointer to the first window class.\n");
440 return (DWORD)-1;
441 default:
442 WARN_(local)("wReqType %04x (unknown)", wReqType);
443 return (DWORD)-1;
447 /***********************************************************************
448 * GetSystemDebugState16 (USER.231)
450 WORD WINAPI GetSystemDebugState16(void)
452 return 0; /* FIXME */
455 /***********************************************************************
456 * RegisterLogonProcess (USER32.434)
458 DWORD WINAPI RegisterLogonProcess(HANDLE hprocess,BOOL x) {
459 FIXME_(win32)("(%d,%d),stub!\n",hprocess,x);
460 return 1;
463 /***********************************************************************
464 * CreateWindowStation32W (USER32.86)
466 HWINSTA WINAPI CreateWindowStationW(
467 LPWSTR winstation,DWORD res1,DWORD desiredaccess,
468 LPSECURITY_ATTRIBUTES lpsa
470 FIXME_(win32)("(%s,0x%08lx,0x%08lx,%p),stub!\n",debugstr_w(winstation),
471 res1,desiredaccess,lpsa
473 return 0xdeadcafe;
476 /***********************************************************************
477 * SetProcessWindowStation (USER32.496)
479 BOOL WINAPI SetProcessWindowStation(HWINSTA hWinSta) {
480 FIXME_(win32)("(%d),stub!\n",hWinSta);
481 return TRUE;
484 /***********************************************************************
485 * SetUserObjectSecurity (USER32.514)
487 BOOL WINAPI SetUserObjectSecurity(
488 HANDLE hObj,
489 /*LPSECURITY_INFORMATION*/LPVOID pSIRequested,
490 PSECURITY_DESCRIPTOR pSID
492 FIXME_(win32)("(0x%08x,%p,%p),stub!\n",hObj,pSIRequested,pSID);
493 return TRUE;
496 /***********************************************************************
497 * CreateDesktop32W (USER32.69)
499 HDESK WINAPI CreateDesktopW(
500 LPWSTR lpszDesktop,LPWSTR lpszDevice,LPDEVMODEW pDevmode,
501 DWORD dwFlags,DWORD dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa
503 FIXME_(win32)("(%s,%s,%p,0x%08lx,0x%08lx,%p),stub!\n",
504 debugstr_w(lpszDesktop),debugstr_w(lpszDevice),pDevmode,
505 dwFlags,dwDesiredAccess,lpsa
507 return 0xcafedead;
510 BOOL WINAPI CloseWindowStation(HWINSTA hWinSta)
512 FIXME_(win32)("(0x%08x)\n", hWinSta);
513 return TRUE;
515 BOOL WINAPI CloseDesktop(HDESK hDesk)
517 FIXME_(win32)("(0x%08x)\n", hDesk);
518 return TRUE;
521 /***********************************************************************
522 * SetWindowStationUser (USER32.521)
524 DWORD WINAPI SetWindowStationUser(DWORD x1,DWORD x2) {
525 FIXME_(win32)("(0x%08lx,0x%08lx),stub!\n",x1,x2);
526 return 1;
529 /***********************************************************************
530 * SetLogonNotifyWindow (USER32.486)
532 DWORD WINAPI SetLogonNotifyWindow(HWINSTA hwinsta,HWND hwnd) {
533 FIXME_(win32)("(0x%x,%04x),stub!\n",hwinsta,hwnd);
534 return 1;
537 /***********************************************************************
538 * LoadLocalFonts (USER32.486)
540 VOID WINAPI LoadLocalFonts(VOID) {
541 /* are loaded. */
542 return;
544 /***********************************************************************
545 * GetUserObjectInformation32A (USER32.299)
547 BOOL WINAPI GetUserObjectInformationA( 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;
551 /***********************************************************************
552 * GetUserObjectInformation32W (USER32.300)
554 BOOL WINAPI GetUserObjectInformationW( HANDLE hObj, int nIndex, LPVOID pvInfo, DWORD nLength, LPDWORD lpnLen )
555 { FIXME_(win32)("(0x%x %i %p %ld %p),stub!\n", hObj, nIndex, pvInfo, nLength, lpnLen );
556 return TRUE;
558 /***********************************************************************
559 * GetUserObjectSecurity32 (USER32.300)
561 BOOL WINAPI GetUserObjectSecurity(HANDLE hObj, SECURITY_INFORMATION * pSIRequested,
562 PSECURITY_DESCRIPTOR pSID, DWORD nLength, LPDWORD lpnLengthNeeded)
563 { FIXME_(win32)("(0x%x %p %p len=%ld %p),stub!\n", hObj, pSIRequested, pSID, nLength, lpnLengthNeeded);
564 return TRUE;
567 /***********************************************************************
568 * SetSystemCursor (USER32.507)
570 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
571 { FIXME_(win32)("(%08x,%08lx),stub!\n", hcur, id);
572 return TRUE;
575 /***********************************************************************
576 * RegisterSystemThread (USER32.435)
578 void WINAPI RegisterSystemThread(DWORD flags, DWORD reserved)
580 FIXME_(win32)("(%08lx, %08lx)\n", flags, reserved);