ntdll: Validate blocks in the heap pending free request list.
[wine.git] / dlls / user32 / user_main.c
blob0ddbd710b6bcaea49ff9c53ac0a880e725379bdd
1 /*
2 * USER initialization code
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "user_private.h"
22 #include "controls.h"
23 #include "imm.h"
24 #include "ddk/imm.h"
25 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
29 WINE_DECLARE_DEBUG_CHANNEL(message);
31 HMODULE user32_module = 0;
33 extern void WDML_NotifyThreadDetach(void);
36 /***********************************************************************
37 * UserRealizePalette (USER32.@)
39 UINT WINAPI UserRealizePalette( HDC hdc )
41 return NtUserRealizePalette( hdc );
45 /***********************************************************************
46 * dpiaware_init
48 * Initialize the DPI awareness style.
50 static void dpiaware_init(void)
52 WCHAR buffer[256];
53 DWORD option;
55 if (!LdrQueryImageFileExecutionOptions( &NtCurrentTeb()->Peb->ProcessParameters->ImagePathName,
56 L"dpiAwareness", REG_DWORD, &option, sizeof(option), NULL ))
58 TRACE( "got option %lx\n", option );
59 if (option <= 2)
61 SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT)~(ULONG_PTR)option );
62 return;
66 if (QueryActCtxSettingsW( 0, NULL, L"http://schemas.microsoft.com/SMI/2016/WindowsSettings",
67 L"dpiAwareness", buffer, ARRAY_SIZE(buffer), NULL ))
69 static const WCHAR * const types[] = { L"unaware", L"system", L"permonitor", L"permonitorv2" };
70 WCHAR *p, *start, *end;
71 ULONG_PTR i;
73 TRACE( "got dpiAwareness=%s\n", debugstr_w(buffer) );
74 for (start = buffer; *start; start = end)
76 start += wcsspn( start, L" \t\r\n" );
77 if (!(end = wcschr( start, ',' ))) end = start + lstrlenW(start);
78 else *end++ = 0;
79 if ((p = wcspbrk( start, L" \t\r\n" ))) *p = 0;
80 for (i = 0; i < ARRAY_SIZE(types); i++)
82 if (wcsicmp( start, types[i] )) continue;
83 SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT)~i );
84 return;
88 else if (QueryActCtxSettingsW( 0, NULL, L"http://schemas.microsoft.com/SMI/2005/WindowsSettings",
89 L"dpiAware", buffer, ARRAY_SIZE(buffer), NULL ))
91 TRACE( "got dpiAware=%s\n", debugstr_w(buffer) );
92 if (!wcsicmp( buffer, L"true" ))
93 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_SYSTEM_AWARE );
94 else if (!wcsicmp( buffer, L"true/pm" ) || !wcsicmp( buffer, L"per monitor" ))
95 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
96 else
97 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_UNAWARE );
101 static NTSTATUS WINAPI User32CopyImage( const struct copy_image_params *params, ULONG size )
103 HANDLE ret = CopyImage( params->hwnd, params->type, params->dx, params->dy, params->flags );
104 return HandleToUlong( ret );
107 static NTSTATUS WINAPI User32DrawNonClientButton( const struct draw_non_client_button_params *params, ULONG size )
109 user_api->pNonClientButtonDraw( params->hwnd, params->hdc, params->type, params->rect,
110 params->down, params->grayed );
111 return 0;
114 static NTSTATUS WINAPI User32DrawScrollBar( const struct draw_scroll_bar_params *params, ULONG size )
116 RECT rect = params->rect;
117 user_api->pScrollBarDraw( params->hwnd, params->hdc, params->bar, params->hit_test,
118 &params->tracking_info, params->arrows, params->interior,
119 &rect, params->enable_flags, params->arrow_size, params->thumb_pos,
120 params->thumb_size, params->vertical );
121 return 0;
124 static NTSTATUS WINAPI User32DrawText( const struct draw_text_params *params, ULONG size )
126 RECT rect = params->rect;
127 int ret;
129 size -= FIELD_OFFSET( struct draw_text_params, str );
130 ret = DrawTextW( params->hdc, params->str, size / sizeof(WCHAR), &rect, params->flags );
131 if (params->ret_rect)
133 *params->ret_rect = rect;
134 return ret;
136 return NtCallbackReturn( &rect, sizeof(rect), ret );
139 static NTSTATUS WINAPI User32ImmProcessKey( const struct imm_process_key_params *params, ULONG size )
141 return ImmProcessKey( params->hwnd, params->hkl, params->vkey, params->key_data, 0 );
144 static NTSTATUS WINAPI User32ImmTranslateMessage( const struct imm_translate_message_params *params,
145 ULONG size )
147 return ImmTranslateMessage( params->hwnd, params->msg, params->wparam, params->key_data );
150 static NTSTATUS WINAPI User32LoadImage( const struct load_image_params *params, ULONG size )
152 HANDLE ret = LoadImageW( params->hinst, params->name, params->type,
153 params->dx, params->dy, params->flags );
154 return HandleToUlong( ret );
157 static NTSTATUS WINAPI User32LoadSysMenu( const struct load_sys_menu_params *params, ULONG size )
159 HMENU ret = LoadMenuW( user32_module, params->mdi ? L"SYSMENUMDI" : L"SYSMENU" );
160 return HandleToUlong( ret );
163 static NTSTATUS WINAPI User32FreeCachedClipboardData( const struct free_cached_data_params *params,
164 ULONG size )
166 free_cached_data( params->format, params->handle );
167 return 0;
170 static NTSTATUS WINAPI User32PostDDEMessage( const struct post_dde_message_params *params, ULONG size )
172 return post_dde_message( params->hwnd, params->msg, params->wparam, params->lparam,
173 params->dest_tid, params->type );
176 static NTSTATUS WINAPI User32RenderSsynthesizedFormat( const struct render_synthesized_format_params *params,
177 ULONG size )
179 render_synthesized_format( params->format, params->from );
180 return 0;
183 static BOOL WINAPI User32LoadDriver( const WCHAR *path, ULONG size )
185 return LoadLibraryW( path ) != NULL;
188 static NTSTATUS WINAPI User32UnpackDDEMessage( const struct unpack_dde_message_params *params, ULONG size )
190 struct unpack_dde_message_result result = { .wparam = params->wparam, .lparam = params->lparam };
192 size -= FIELD_OFFSET( struct unpack_dde_message_params, data );
193 if (!unpack_dde_message( params->hwnd, params->message, &result.wparam, &result.lparam,
194 params->data, size ))
195 return FALSE;
197 if (params->result) *params->result = result;
198 else NtCallbackReturn( &result, sizeof(result), TRUE );
199 return TRUE;
202 static const void *kernel_callback_table[NtUserCallCount] =
204 User32CallEnumDisplayMonitor,
205 User32CallSendAsyncCallback,
206 User32CallWinEventHook,
207 User32CallWindowProc,
208 User32CallWindowsHook,
209 User32CopyImage,
210 User32DrawNonClientButton,
211 User32DrawScrollBar,
212 User32DrawText,
213 User32FreeCachedClipboardData,
214 User32ImmProcessKey,
215 User32ImmTranslateMessage,
216 User32InitBuiltinClasses,
217 User32LoadDriver,
218 User32LoadImage,
219 User32LoadSysMenu,
220 User32PostDDEMessage,
221 User32RenderSsynthesizedFormat,
222 User32UnpackDDEMessage,
226 /***********************************************************************
227 * USER initialisation routine
229 static BOOL process_attach(void)
231 NtCurrentTeb()->Peb->KernelCallbackTable = kernel_callback_table;
233 winproc_init();
234 dpiaware_init();
235 SYSPARAMS_Init();
237 return TRUE;
241 /**********************************************************************
242 * thread_detach
244 static void thread_detach(void)
246 struct ntuser_thread_info *thread_info = NtUserGetThreadInfo();
248 NtUserCallNoParam( NtUserExitingThread );
250 WDML_NotifyThreadDetach();
252 NtUserCallNoParam( NtUserThreadDetach );
253 HeapFree( GetProcessHeap(), 0, (void *)(UINT_PTR)thread_info->wmchar_data );
257 /***********************************************************************
258 * UserClientDllInitialize (USER32.@)
260 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
262 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
264 static HMODULE imm32_module;
265 BOOL ret = TRUE;
267 switch(reason)
269 case DLL_PROCESS_ATTACH:
270 user32_module = inst;
271 ret = process_attach();
272 if(ret)
273 imm32_module = LoadLibraryW(L"imm32.dll");
274 break;
275 case DLL_THREAD_DETACH:
276 thread_detach();
277 break;
278 case DLL_PROCESS_DETACH:
279 FreeLibrary(imm32_module);
280 break;
282 return ret;
286 /***********************************************************************
287 * ExitWindowsEx (USER32.@)
289 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
291 WCHAR app[MAX_PATH];
292 WCHAR cmdline[MAX_PATH + 64];
293 PROCESS_INFORMATION pi;
294 STARTUPINFOW si;
295 void *redir;
297 GetSystemDirectoryW( app, MAX_PATH - ARRAY_SIZE( L"\\wineboot.exe" ));
298 lstrcatW( app, L"\\wineboot.exe" );
299 lstrcpyW( cmdline, app );
301 if (flags & EWX_FORCE) lstrcatW( cmdline, L" --kill" );
302 else
304 lstrcatW( cmdline, L" --end-session" );
305 if (flags & EWX_FORCEIFHUNG) lstrcatW( cmdline, L" --force" );
307 if (!(flags & EWX_REBOOT)) lstrcatW( cmdline, L" --shutdown" );
309 memset( &si, 0, sizeof si );
310 si.cb = sizeof si;
311 Wow64DisableWow64FsRedirection( &redir );
312 if (!CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
314 Wow64RevertWow64FsRedirection( redir );
315 ERR( "Failed to run %s\n", debugstr_w(cmdline) );
316 return FALSE;
318 Wow64RevertWow64FsRedirection( redir );
319 CloseHandle( pi.hProcess );
320 CloseHandle( pi.hThread );
321 return TRUE;
324 /***********************************************************************
325 * LockWorkStation (USER32.@)
327 BOOL WINAPI LockWorkStation(void)
329 TRACE(": stub\n");
330 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
331 return FALSE;
334 /***********************************************************************
335 * RegisterServicesProcess (USER32.@)
337 int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
339 FIXME("(0x%lx): stub\n", ServicesProcessId);
340 return 0;
343 /***********************************************************************
344 * ShutdownBlockReasonCreate (USER32.@)
346 BOOL WINAPI ShutdownBlockReasonCreate(HWND hwnd, LPCWSTR reason)
348 FIXME("(%p, %s): stub\n", hwnd, debugstr_w(reason));
349 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
350 return FALSE;
353 /***********************************************************************
354 * ShutdownBlockReasonDestroy (USER32.@)
356 BOOL WINAPI ShutdownBlockReasonDestroy(HWND hwnd)
358 FIXME("(%p): stub\n", hwnd);
359 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
360 return FALSE;
363 const char *SPY_GetMsgName( UINT msg, HWND hwnd )
365 char buf[128];
366 NtUserMessageCall( hwnd, msg, ARRAYSIZE(buf), 0, buf, NtUserSpyGetMsgName, FALSE );
367 return wine_dbg_sprintf( "%s", buf );
370 void SPY_EnterMessage( INT flag, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
372 if (TRACE_ON(message)) NtUserMessageCall( hwnd, msg, wparam, lparam, 0, NtUserSpyEnter, flag );
375 void SPY_ExitMessage( INT flag, HWND hwnd, UINT msg, LRESULT lreturn, WPARAM wparam, LPARAM lparam )
377 if (TRACE_ON(message)) NtUserMessageCall( hwnd, msg, wparam, lparam, (void *)lreturn,
378 NtUserSpyExit, flag );