Remove HANDLEINFO struct.
[wine/testsucceed.git] / windows / user.c
blobcf0ef18e3bdc573359e5eff97dc7f88c02092205
1 /*
2 * Misc. USER functions
4 * Copyright 1993 Robert J. Amstadt
5 * 1996 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "wine/winbase16.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "wine/winuser16.h"
31 #include "winreg.h"
32 #include "winternl.h"
33 #include "tlhelp32.h"
34 #include "user_private.h"
35 #include "win.h"
36 #include "controls.h"
37 #include "cursoricon.h"
38 #include "local.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(user);
43 /* USER signal proc flags and codes */
44 /* See UserSignalProc for comments */
45 #define USIG_FLAGS_WIN32 0x0001
46 #define USIG_FLAGS_GUI 0x0002
47 #define USIG_FLAGS_FEEDBACK 0x0004
48 #define USIG_FLAGS_FAULT 0x0008
50 #define USIG_DLL_UNLOAD_WIN16 0x0001
51 #define USIG_DLL_UNLOAD_WIN32 0x0002
52 #define USIG_FAULT_DIALOG_PUSH 0x0003
53 #define USIG_FAULT_DIALOG_POP 0x0004
54 #define USIG_DLL_UNLOAD_ORPHANS 0x0005
55 #define USIG_THREAD_INIT 0x0010
56 #define USIG_THREAD_EXIT 0x0020
57 #define USIG_PROCESS_CREATE 0x0100
58 #define USIG_PROCESS_INIT 0x0200
59 #define USIG_PROCESS_EXIT 0x0300
60 #define USIG_PROCESS_DESTROY 0x0400
61 #define USIG_PROCESS_RUNNING 0x0500
62 #define USIG_PROCESS_LOADED 0x0600
64 /* UserSeeUserDo parameters */
65 #define USUD_LOCALALLOC 0x0001
66 #define USUD_LOCALFREE 0x0002
67 #define USUD_LOCALCOMPACT 0x0003
68 #define USUD_LOCALHEAP 0x0004
69 #define USUD_FIRSTCLASS 0x0005
71 /***********************************************************************
72 * GetFreeSystemResources (USER.284)
74 WORD WINAPI GetFreeSystemResources16( WORD resType )
76 HINSTANCE16 gdi_inst;
77 WORD gdi_heap;
78 int userPercent, gdiPercent;
80 if ((gdi_inst = LoadLibrary16( "GDI" )) < 32) return 0;
81 gdi_heap = gdi_inst | 7;
83 switch(resType)
85 case GFSR_USERRESOURCES:
86 userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
87 LOCAL_HeapSize( USER_HeapSel );
88 gdiPercent = 100;
89 break;
91 case GFSR_GDIRESOURCES:
92 gdiPercent = (int)LOCAL_CountFree( gdi_inst ) * 100 /
93 LOCAL_HeapSize( gdi_inst );
94 userPercent = 100;
95 break;
97 case GFSR_SYSTEMRESOURCES:
98 userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
99 LOCAL_HeapSize( USER_HeapSel );
100 gdiPercent = (int)LOCAL_CountFree( gdi_inst ) * 100 /
101 LOCAL_HeapSize( gdi_inst );
102 break;
104 default:
105 userPercent = gdiPercent = 0;
106 break;
108 FreeLibrary16( gdi_inst );
109 TRACE("<- userPercent %d, gdiPercent %d\n", userPercent, gdiPercent);
110 return (WORD)min( userPercent, gdiPercent );
114 /***********************************************************************
115 * SignalProc32 (USER.391)
116 * UserSignalProc (USER32.@)
118 * The exact meaning of the USER signals is undocumented, but this
119 * should cover the basic idea:
121 * USIG_DLL_UNLOAD_WIN16
122 * This is sent when a 16-bit module is unloaded.
124 * USIG_DLL_UNLOAD_WIN32
125 * This is sent when a 32-bit module is unloaded.
127 * USIG_DLL_UNLOAD_ORPHANS
128 * This is sent after the last Win3.1 module is unloaded,
129 * to allow removal of orphaned menus.
131 * USIG_FAULT_DIALOG_PUSH
132 * USIG_FAULT_DIALOG_POP
133 * These are called to allow USER to prepare for displaying a
134 * fault dialog, even though the fault might have happened while
135 * inside a USER critical section.
137 * USIG_THREAD_INIT
138 * This is called from the context of a new thread, as soon as it
139 * has started to run.
141 * USIG_THREAD_EXIT
142 * This is called, still in its context, just before a thread is
143 * about to terminate.
145 * USIG_PROCESS_CREATE
146 * This is called, in the parent process context, after a new process
147 * has been created.
149 * USIG_PROCESS_INIT
150 * This is called in the new process context, just after the main thread
151 * has started execution (after the main thread's USIG_THREAD_INIT has
152 * been sent).
154 * USIG_PROCESS_LOADED
155 * This is called after the executable file has been loaded into the
156 * new process context.
158 * USIG_PROCESS_RUNNING
159 * This is called immediately before the main entry point is called.
161 * USIG_PROCESS_EXIT
162 * This is called in the context of a process that is about to
163 * terminate (but before the last thread's USIG_THREAD_EXIT has
164 * been sent).
166 * USIG_PROCESS_DESTROY
167 * This is called after a process has terminated.
170 * The meaning of the dwFlags bits is as follows:
172 * USIG_FLAGS_WIN32
173 * Current process is 32-bit.
175 * USIG_FLAGS_GUI
176 * Current process is a (Win32) GUI process.
178 * USIG_FLAGS_FEEDBACK
179 * Current process needs 'feedback' (determined from the STARTUPINFO
180 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
182 * USIG_FLAGS_FAULT
183 * The signal is being sent due to a fault.
185 WORD WINAPI UserSignalProc( UINT uCode, DWORD dwThreadOrProcessID,
186 DWORD dwFlags, HMODULE16 hModule )
188 FIXME("(%04x, %08lx, %04lx, %04x)\n",
189 uCode, dwThreadOrProcessID, dwFlags, hModule );
190 /* FIXME: Should chain to GdiSignalProc now. */
191 return 0;
194 /***********************************************************************
195 * USER_GetProcessHandleList(Internal)
197 static HANDLE *USER_GetProcessHandleList(void)
199 DWORD count, i, n;
200 HANDLE *list;
201 PROCESSENTRY32 pe;
202 HANDLE hSnapshot;
203 BOOL r;
205 hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
206 if (!hSnapshot)
208 ERR("cannot create snapshot\n");
209 return FALSE;
212 /* count the number of processes plus one */
213 for (count=0; ;count++)
215 pe.dwSize = sizeof pe;
216 if (count)
217 r = Process32Next( hSnapshot, &pe );
218 else
219 r = Process32First( hSnapshot, &pe );
220 if (!r)
221 break;
224 /* allocate memory make a list of the process handles */
225 list = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof(HANDLE) );
226 n=0;
227 for (i=0; i<count; i++)
229 pe.dwSize = sizeof pe;
230 if (i)
231 r = Process32Next( hSnapshot, &pe );
232 else
233 r = Process32First( hSnapshot, &pe );
234 if (!r)
235 break;
237 /* don't kill ourselves */
238 if (GetCurrentProcessId() == pe.th32ProcessID )
239 continue;
241 /* open the process so we don't can track it */
242 list[n] = OpenProcess( PROCESS_QUERY_INFORMATION|
243 PROCESS_TERMINATE,
244 FALSE, pe.th32ProcessID );
246 /* check it didn't terminate already */
247 if( list[n] )
248 n++;
250 list[n]=0;
251 CloseHandle( hSnapshot );
253 if (!r)
254 ERR("Error enumerating processes\n");
256 TRACE("return %lu processes\n", n);
258 return list;
261 /***********************************************************************
262 * USER_KillProcesses (Internal)
264 static DWORD USER_KillProcesses(void)
266 DWORD n, r, i;
267 HANDLE *handles;
268 const DWORD dwShutdownTimeout = 10000;
270 TRACE("terminating other processes\n");
272 /* kill it and add it to our list of object to wait on */
273 handles = USER_GetProcessHandleList();
274 for (n=0; handles && handles[n]; n++)
275 TerminateProcess( handles[n], 0 );
277 /* wait for processes to exit */
278 for (i=0; i<n; i+=MAXIMUM_WAIT_OBJECTS)
280 int n_objs = ((n-i)>MAXIMUM_WAIT_OBJECTS) ? MAXIMUM_WAIT_OBJECTS : (n-i);
281 r = WaitForMultipleObjects( n_objs, &handles[i], TRUE, dwShutdownTimeout );
282 if (r==WAIT_TIMEOUT)
283 ERR("wait failed!\n");
286 /* close the handles */
287 for (i=0; i<n; i++)
288 CloseHandle( handles[i] );
290 HeapFree( GetProcessHeap(), 0, handles );
292 return n;
295 /***********************************************************************
296 * USER_DoShutdown (Internal)
298 static void USER_DoShutdown(void)
300 DWORD i, n;
301 const DWORD nRetries = 10;
303 for (i=0; i<nRetries; i++)
305 n = USER_KillProcesses();
306 TRACE("Killed %ld processes, attempt %ld\n", n, i);
307 if(!n)
308 break;
312 /***********************************************************************
313 * USER_StartRebootProcess (Internal)
315 static BOOL USER_StartRebootProcess(void)
317 WCHAR winebootW[] = { 'w','i','n','e','b','o','o','t',0 };
318 PROCESS_INFORMATION pi;
319 STARTUPINFOW si;
320 BOOL r;
322 memset( &si, 0, sizeof si );
323 si.cb = sizeof si;
325 r = CreateProcessW( NULL, winebootW, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
326 if (r)
328 CloseHandle( pi.hProcess );
329 CloseHandle( pi.hThread );
331 else
332 MESSAGE("wine: Failed to start wineboot\n");
334 return r;
337 /***********************************************************************
338 * ExitWindowsEx (USER32.@)
340 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reserved )
342 int i;
343 BOOL result = FALSE;
344 HWND *list, *phwnd;
346 /* We have to build a list of all windows first, as in EnumWindows */
347 TRACE("(%x,%lx)\n", flags, reserved);
349 list = WIN_ListChildren( GetDesktopWindow() );
350 if (list)
352 /* Send a WM_QUERYENDSESSION message to every window */
354 for (i = 0; list[i]; i++)
356 /* Make sure that the window still exists */
357 if (!IsWindow( list[i] )) continue;
358 if (!SendMessageW( list[i], WM_QUERYENDSESSION, 0, 0 )) break;
360 result = !list[i];
362 /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
364 for (phwnd = list; i > 0; i--, phwnd++)
366 if (!IsWindow( *phwnd )) continue;
367 SendMessageW( *phwnd, WM_ENDSESSION, result, 0 );
369 HeapFree( GetProcessHeap(), 0, list );
371 if ( !(result || (flags & EWX_FORCE) ))
372 return FALSE;
375 /* USER_DoShutdown will kill all processes except the current process */
376 USER_DoShutdown();
378 if (flags & EWX_REBOOT)
379 USER_StartRebootProcess();
381 if (result) ExitProcess(0);
382 return TRUE;
385 /***********************************************************************
386 * ChangeDisplaySettingsA (USER32.@)
388 LONG WINAPI ChangeDisplaySettingsA( LPDEVMODEA devmode, DWORD flags )
390 return ChangeDisplaySettingsExA(NULL,devmode,NULL,flags,NULL);
393 /***********************************************************************
394 * ChangeDisplaySettingsW (USER32.@)
396 LONG WINAPI ChangeDisplaySettingsW( LPDEVMODEW devmode, DWORD flags )
398 return ChangeDisplaySettingsExW(NULL,devmode,NULL,flags,NULL);
401 /***********************************************************************
402 * ChangeDisplaySettingsExA (USER32.@)
404 LONG WINAPI ChangeDisplaySettingsExA(
405 LPCSTR devname, LPDEVMODEA devmode, HWND hwnd, DWORD flags,
406 LPVOID lparam
408 DEVMODEW devmodeW;
409 LONG ret;
410 UNICODE_STRING nameW;
412 if (devname) RtlCreateUnicodeStringFromAsciiz(&nameW, devname);
413 else nameW.Buffer = NULL;
415 if (devmode)
417 devmodeW.dmBitsPerPel = devmode->dmBitsPerPel;
418 devmodeW.dmPelsHeight = devmode->dmPelsHeight;
419 devmodeW.dmPelsWidth = devmode->dmPelsWidth;
420 devmodeW.dmDisplayFlags = devmode->dmDisplayFlags;
421 devmodeW.dmDisplayFrequency = devmode->dmDisplayFrequency;
422 devmodeW.dmFields = devmode->dmFields;
423 ret = ChangeDisplaySettingsExW(nameW.Buffer, &devmodeW, hwnd, flags, lparam);
425 else
427 ret = ChangeDisplaySettingsExW(nameW.Buffer, NULL, hwnd, flags, lparam);
430 if (devname) RtlFreeUnicodeString(&nameW);
431 return ret;
434 /***********************************************************************
435 * ChangeDisplaySettingsExW (USER32.@)
437 LONG WINAPI ChangeDisplaySettingsExW( LPCWSTR devname, LPDEVMODEW devmode, HWND hwnd,
438 DWORD flags, LPVOID lparam )
440 /* Pass the request on to the driver */
441 if (!USER_Driver.pChangeDisplaySettingsExW) return DISP_CHANGE_FAILED;
442 return USER_Driver.pChangeDisplaySettingsExW( devname, devmode, hwnd, flags, lparam );
445 /***********************************************************************
446 * EnumDisplaySettingsW (USER32.@)
448 * RETURNS
449 * TRUE if nth setting exists found (described in the LPDEVMODEW struct)
450 * FALSE if we do not have the nth setting
452 BOOL WINAPI EnumDisplaySettingsW(
453 LPCWSTR name, /* [in] huh? */
454 DWORD n, /* [in] nth entry in display settings list*/
455 LPDEVMODEW devmode /* [out] devmode for that setting */
457 return EnumDisplaySettingsExW(name, n, devmode, 0);
460 /***********************************************************************
461 * EnumDisplaySettingsA (USER32.@)
463 BOOL WINAPI EnumDisplaySettingsA(LPCSTR name,DWORD n,LPDEVMODEA devmode)
465 return EnumDisplaySettingsExA(name, n, devmode, 0);
468 /***********************************************************************
469 * EnumDisplaySettingsExA (USER32.@)
471 BOOL WINAPI EnumDisplaySettingsExA(LPCSTR lpszDeviceName, DWORD iModeNum,
472 LPDEVMODEA lpDevMode, DWORD dwFlags)
474 DEVMODEW devmodeW;
475 BOOL ret;
476 UNICODE_STRING nameW;
478 if (lpszDeviceName) RtlCreateUnicodeStringFromAsciiz(&nameW, lpszDeviceName);
479 else nameW.Buffer = NULL;
481 ret = EnumDisplaySettingsExW(nameW.Buffer,iModeNum,&devmodeW,dwFlags);
482 if (ret)
484 lpDevMode->dmBitsPerPel = devmodeW.dmBitsPerPel;
485 lpDevMode->dmPelsHeight = devmodeW.dmPelsHeight;
486 lpDevMode->dmPelsWidth = devmodeW.dmPelsWidth;
487 lpDevMode->dmDisplayFlags = devmodeW.dmDisplayFlags;
488 lpDevMode->dmDisplayFrequency = devmodeW.dmDisplayFrequency;
489 lpDevMode->dmFields = devmodeW.dmFields;
491 if (lpszDeviceName) RtlFreeUnicodeString(&nameW);
492 return ret;
495 /***********************************************************************
496 * EnumDisplaySettingsExW (USER32.@)
498 BOOL WINAPI EnumDisplaySettingsExW(LPCWSTR lpszDeviceName, DWORD iModeNum,
499 LPDEVMODEW lpDevMode, DWORD dwFlags)
501 /* Pass the request on to the driver */
502 if (!USER_Driver.pEnumDisplaySettingsExW) return FALSE;
503 return USER_Driver.pEnumDisplaySettingsExW(lpszDeviceName, iModeNum, lpDevMode, dwFlags);
506 /***********************************************************************
507 * EnumDisplayDevicesA (USER32.@)
509 BOOL WINAPI EnumDisplayDevicesA(
510 LPVOID unused,DWORD i,LPDISPLAY_DEVICEA lpDisplayDevice,DWORD dwFlags
512 if (i)
513 return FALSE;
514 FIXME("(%p,%ld,%p,0x%08lx), stub!\n",unused,i,lpDisplayDevice,dwFlags);
515 strcpy(lpDisplayDevice->DeviceName,"X11");
516 strcpy(lpDisplayDevice->DeviceString,"X 11 Windowing System");
517 lpDisplayDevice->StateFlags =
518 DISPLAY_DEVICE_ATTACHED_TO_DESKTOP |
519 DISPLAY_DEVICE_PRIMARY_DEVICE |
520 DISPLAY_DEVICE_VGA_COMPATIBLE;
521 return TRUE;
524 /***********************************************************************
525 * EnumDisplayDevicesW (USER32.@)
527 BOOL WINAPI EnumDisplayDevicesW(
528 LPVOID unused,DWORD i,LPDISPLAY_DEVICEW lpDisplayDevice,DWORD dwFlags
530 if (i)
531 return FALSE;
532 FIXME("(%p,%ld,%p,0x%08lx), stub!\n",unused,i,lpDisplayDevice,dwFlags);
533 MultiByteToWideChar( CP_ACP, 0, "X11", -1, lpDisplayDevice->DeviceName,
534 sizeof(lpDisplayDevice->DeviceName)/sizeof(WCHAR) );
535 MultiByteToWideChar( CP_ACP, 0, "X11 Windowing System", -1, lpDisplayDevice->DeviceString,
536 sizeof(lpDisplayDevice->DeviceString)/sizeof(WCHAR) );
537 lpDisplayDevice->StateFlags =
538 DISPLAY_DEVICE_ATTACHED_TO_DESKTOP |
539 DISPLAY_DEVICE_PRIMARY_DEVICE |
540 DISPLAY_DEVICE_VGA_COMPATIBLE;
541 return TRUE;
544 /***********************************************************************
545 * UserSeeUserDo (USER.216)
547 DWORD WINAPI UserSeeUserDo16(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
549 switch (wReqType)
551 case USUD_LOCALALLOC:
552 return LOCAL_Alloc(USER_HeapSel, wParam1, wParam3);
553 case USUD_LOCALFREE:
554 return LOCAL_Free(USER_HeapSel, wParam1);
555 case USUD_LOCALCOMPACT:
556 return LOCAL_Compact(USER_HeapSel, wParam3, 0);
557 case USUD_LOCALHEAP:
558 return USER_HeapSel;
559 case USUD_FIRSTCLASS:
560 FIXME("return a pointer to the first window class.\n");
561 return (DWORD)-1;
562 default:
563 WARN("wReqType %04x (unknown)\n", wReqType);
564 return (DWORD)-1;
568 /***********************************************************************
569 * SetSystemCursor (USER32.@)
571 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
572 { FIXME("(%p,%08lx),stub!\n", hcur, id);
573 return TRUE;