Sweden-Number/dlls/user32/user_main.c

477 lines
14 KiB
C
Raw Normal View History

/*
* USER initialization code
*
* Copyright 2000 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "controls.h"
#include "user_private.h"
#include "win.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(graphics);
#define DESKTOP_ALL_ACCESS 0x01ff
HMODULE user32_module = 0;
2001-03-23 20:13:23 +01:00
static CRITICAL_SECTION user_section;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &user_section,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": user_section") }
};
static CRITICAL_SECTION user_section = { &critsect_debug, -1, 0, 0, 0, 0 };
static HPALETTE (WINAPI *pfnGDISelectPalette)( HDC hdc, HPALETTE hpal, WORD bkgnd );
static UINT (WINAPI *pfnGDIRealizePalette)( HDC hdc );
static HPALETTE hPrimaryPalette;
static DWORD exiting_thread_id;
extern void WDML_NotifyThreadDetach(void);
2001-07-24 22:49:41 +02:00
#ifdef __MINGW32__
/* work around a Mingw build issue where _wassert causes a duplicate reference to MessageBoxW */
void __cdecl _wassert( const WCHAR *msg, const WCHAR *file, unsigned line) { abort(); }
#endif
/***********************************************************************
* USER_Lock
*/
void USER_Lock(void)
{
EnterCriticalSection( &user_section );
}
/***********************************************************************
* USER_Unlock
*/
void USER_Unlock(void)
{
LeaveCriticalSection( &user_section );
}
/***********************************************************************
* USER_CheckNotLock
*
* Make sure that we don't hold the user lock.
*/
void USER_CheckNotLock(void)
{
if (RtlIsCriticalSectionLockedByThread(&user_section))
{
ERR( "BUG: holding USER lock\n" );
DebugBreak();
}
}
/***********************************************************************
* UserSelectPalette (Not a Windows API)
*/
static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
{
WORD wBkgPalette = 1;
if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
{
HWND hwnd = WindowFromDC( hDC );
if (hwnd)
{
HWND hForeground = GetForegroundWindow();
/* set primary palette if it's related to current active */
if (hForeground == hwnd || IsChild(hForeground,hwnd))
{
wBkgPalette = 0;
hPrimaryPalette = hPal;
}
}
}
return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
}
/***********************************************************************
* UserRealizePalette (USER32.@)
*/
UINT WINAPI UserRealizePalette( HDC hDC )
{
UINT realized = pfnGDIRealizePalette( hDC );
/* do not send anything if no colors were changed */
if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
{
/* send palette change notification */
HWND hWnd = WindowFromDC( hDC );
if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
SMTO_ABORTIFHUNG, 2000, NULL );
}
return realized;
}
/***********************************************************************
* palette_init
*
* Patch the function pointers in GDI for SelectPalette and RealizePalette
*/
static void palette_init(void)
{
void **ptr;
HMODULE module = GetModuleHandleA( "gdi32" );
if (!module)
{
ERR( "cannot get GDI32 handle\n" );
return;
}
if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
else ERR( "cannot find pfnSelectPalette in GDI32\n" );
if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
else ERR( "cannot find pfnRealizePalette in GDI32\n" );
}
/***********************************************************************
* get_default_desktop
*
* Get the name of the desktop to use for this app if not specified explicitly.
*/
static const WCHAR *get_default_desktop(void)
{
static WCHAR buffer[MAX_PATH + ARRAY_SIZE(L"\\Explorer")];
WCHAR *p, *appname = buffer;
const WCHAR *ret = NULL;
DWORD len;
HKEY tmpkey, appkey;
len = (GetModuleFileNameW( 0, buffer, MAX_PATH ));
if (!len || len >= MAX_PATH) return L"Default";
if ((p = wcsrchr( appname, '/' ))) appname = p + 1;
if ((p = wcsrchr( appname, '\\' ))) appname = p + 1;
p = appname + lstrlenW(appname);
lstrcpyW( p, L"\\Explorer" );
/* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Explorer */
if (!RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\AppDefaults", &tmpkey ))
{
if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
RegCloseKey( tmpkey );
if (appkey)
{
len = sizeof(buffer);
if (!RegQueryValueExW( appkey, L"Desktop", 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
RegCloseKey( appkey );
if (ret && *ret) return ret;
ret = NULL;
}
}
/* @@ Wine registry key: HKCU\Software\Wine\Explorer */
if (!RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\Explorer", &appkey ))
{
len = sizeof(buffer);
if (!RegQueryValueExW( appkey, L"Desktop", 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
RegCloseKey( appkey );
if (ret && *ret) return ret;
}
return L"Default";
}
/***********************************************************************
* dpiaware_init
*
* Initialize the DPI awareness style.
*/
static void dpiaware_init(void)
{
WCHAR buffer[256];
DWORD option;
if (!LdrQueryImageFileExecutionOptions( &NtCurrentTeb()->Peb->ProcessParameters->ImagePathName,
L"dpiAwareness", REG_DWORD, &option, sizeof(option), NULL ))
{
TRACE( "got option %x\n", option );
if (option <= 2)
{
SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT)~(ULONG_PTR)option );
return;
}
}
if (QueryActCtxSettingsW( 0, NULL, L"http://schemas.microsoft.com/SMI/2016/WindowsSettings",
L"dpiAwareness", buffer, ARRAY_SIZE(buffer), NULL ))
{
static const WCHAR * const types[] = { L"unaware", L"system", L"permonitor", L"permonitorv2" };
WCHAR *p, *start, *end;
ULONG_PTR i;
TRACE( "got dpiAwareness=%s\n", debugstr_w(buffer) );
for (start = buffer; *start; start = end)
{
start += wcsspn( start, L" \t\r\n" );
if (!(end = wcschr( start, ',' ))) end = start + lstrlenW(start);
else *end++ = 0;
if ((p = wcspbrk( start, L" \t\r\n" ))) *p = 0;
for (i = 0; i < ARRAY_SIZE(types); i++)
{
if (wcsicmp( start, types[i] )) continue;
SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT)~i );
return;
}
}
}
else if (QueryActCtxSettingsW( 0, NULL, L"http://schemas.microsoft.com/SMI/2005/WindowsSettings",
L"dpiAware", buffer, ARRAY_SIZE(buffer), NULL ))
{
TRACE( "got dpiAware=%s\n", debugstr_w(buffer) );
if (!wcsicmp( buffer, L"true" ))
SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_SYSTEM_AWARE );
else if (!wcsicmp( buffer, L"true/pm" ) || !wcsicmp( buffer, L"per monitor" ))
SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
else
SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_UNAWARE );
}
}
/***********************************************************************
* winstation_init
*
* Connect to the process window station and desktop.
*/
static void winstation_init(void)
{
STARTUPINFOW info;
WCHAR *winstation = NULL, *desktop = NULL, *buffer = NULL;
HANDLE handle;
GetStartupInfoW( &info );
if (info.lpDesktop && *info.lpDesktop)
{
buffer = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(info.lpDesktop) + 1) * sizeof(WCHAR) );
lstrcpyW( buffer, info.lpDesktop );
if ((desktop = wcschr( buffer, '\\' )))
{
*desktop++ = 0;
winstation = buffer;
}
else desktop = buffer;
}
/* set winstation if explicitly specified, or if we don't have one yet */
if (buffer || !GetProcessWindowStation())
{
handle = CreateWindowStationW( winstation ? winstation : L"WinSta0", 0, WINSTA_ALL_ACCESS, NULL );
if (handle)
{
SetProcessWindowStation( handle );
/* only WinSta0 is visible */
if (!winstation || !wcsicmp( winstation, L"WinSta0" ))
{
USEROBJECTFLAGS flags;
flags.fInherit = FALSE;
flags.fReserved = FALSE;
flags.dwFlags = WSF_VISIBLE;
SetUserObjectInformationW( handle, UOI_FLAGS, &flags, sizeof(flags) );
}
}
}
if (buffer || !GetThreadDesktop( GetCurrentThreadId() ))
{
handle = CreateDesktopW( desktop ? desktop : get_default_desktop(),
NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
if (handle) SetThreadDesktop( handle );
}
HeapFree( GetProcessHeap(), 0, buffer );
register_desktop_class();
}
/***********************************************************************
* USER initialisation routine
*/
static BOOL process_attach(void)
{
dpiaware_init();
winstation_init();
2002-05-20 20:01:44 +02:00
/* Initialize system colors and metrics */
SYSPARAMS_Init();
/* Setup palette function pointers */
palette_init();
keyboard_init();
return TRUE;
}
/**********************************************************************
* USER_IsExitingThread
*/
BOOL USER_IsExitingThread( DWORD tid )
{
return (tid == exiting_thread_id);
}
/**********************************************************************
* thread_detach
*/
static void thread_detach(void)
{
struct user_thread_info *thread_info = get_user_thread_info();
exiting_thread_id = GetCurrentThreadId();
WDML_NotifyThreadDetach();
USER_Driver->pThreadDetach();
destroy_thread_windows();
CloseHandle( thread_info->server_queue );
HeapFree( GetProcessHeap(), 0, thread_info->wmchar_data );
HeapFree( GetProcessHeap(), 0, thread_info->key_state );
2012-09-11 00:27:04 +02:00
HeapFree( GetProcessHeap(), 0, thread_info->rawinput );
exiting_thread_id = 0;
}
/***********************************************************************
* UserClientDllInitialize (USER32.@)
*
2003-10-10 06:30:54 +02:00
* USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
*/
2003-10-10 06:30:54 +02:00
BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
{
static HMODULE imm32_module;
BOOL ret = TRUE;
switch(reason)
{
case DLL_PROCESS_ATTACH:
user32_module = inst;
ret = process_attach();
if(ret)
imm32_module = LoadLibraryW(L"imm32.dll");
break;
case DLL_THREAD_DETACH:
thread_detach();
break;
case DLL_PROCESS_DETACH:
USER_unload_driver();
FreeLibrary(imm32_module);
DeleteCriticalSection(&user_section);
break;
}
return ret;
}
/***********************************************************************
* ExitWindowsEx (USER32.@)
*/
BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
{
WCHAR app[MAX_PATH];
WCHAR cmdline[MAX_PATH + 64];
PROCESS_INFORMATION pi;
STARTUPINFOW si;
void *redir;
GetSystemDirectoryW( app, MAX_PATH - ARRAY_SIZE( L"\\wineboot.exe" ));
lstrcatW( app, L"\\wineboot.exe" );
lstrcpyW( cmdline, app );
if (flags & EWX_FORCE) lstrcatW( cmdline, L" --kill" );
else
{
lstrcatW( cmdline, L" --end-session" );
if (flags & EWX_FORCEIFHUNG) lstrcatW( cmdline, L" --force" );
}
if (!(flags & EWX_REBOOT)) lstrcatW( cmdline, L" --shutdown" );
memset( &si, 0, sizeof si );
si.cb = sizeof si;
Wow64DisableWow64FsRedirection( &redir );
if (!CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
{
Wow64RevertWow64FsRedirection( redir );
ERR( "Failed to run %s\n", debugstr_w(cmdline) );
return FALSE;
}
Wow64RevertWow64FsRedirection( redir );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return TRUE;
}
2008-09-17 17:12:20 +02:00
/***********************************************************************
* LockWorkStation (USER32.@)
*/
BOOL WINAPI LockWorkStation(void)
{
TRACE(": stub\n");
SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
return FALSE;
}
/***********************************************************************
* RegisterServicesProcess (USER32.@)
*/
int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
{
FIXME("(0x%x): stub\n", ServicesProcessId);
return 0;
}
/***********************************************************************
* ShutdownBlockReasonCreate (USER32.@)
*/
BOOL WINAPI ShutdownBlockReasonCreate(HWND hwnd, LPCWSTR reason)
{
FIXME("(%p, %s): stub\n", hwnd, debugstr_w(reason));
SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
return FALSE;
}
/***********************************************************************
* ShutdownBlockReasonDestroy (USER32.@)
*/
BOOL WINAPI ShutdownBlockReasonDestroy(HWND hwnd)
{
FIXME("(%p): stub\n", hwnd);
SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
return FALSE;
}