win32u: Use platform-independent layout for ntuser_thread_info.
[wine.git] / dlls / user32 / user_main.c
blobdf3e6b27b81c54a3568781af8e1301441961b2cf
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 <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "imm.h"
29 #include "ddk/imm.h"
31 #include "controls.h"
32 #include "user_private.h"
33 #include "win.h"
34 #include "wine/debug.h"
35 #include "wine/exception.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
38 WINE_DECLARE_DEBUG_CHANNEL(message);
40 HMODULE user32_module = 0;
42 extern void WDML_NotifyThreadDetach(void);
45 /***********************************************************************
46 * UserRealizePalette (USER32.@)
48 UINT WINAPI UserRealizePalette( HDC hdc )
50 return NtUserRealizePalette( hdc );
54 /***********************************************************************
55 * dpiaware_init
57 * Initialize the DPI awareness style.
59 static void dpiaware_init(void)
61 WCHAR buffer[256];
62 DWORD option;
64 if (!LdrQueryImageFileExecutionOptions( &NtCurrentTeb()->Peb->ProcessParameters->ImagePathName,
65 L"dpiAwareness", REG_DWORD, &option, sizeof(option), NULL ))
67 TRACE( "got option %lx\n", option );
68 if (option <= 2)
70 SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT)~(ULONG_PTR)option );
71 return;
75 if (QueryActCtxSettingsW( 0, NULL, L"http://schemas.microsoft.com/SMI/2016/WindowsSettings",
76 L"dpiAwareness", buffer, ARRAY_SIZE(buffer), NULL ))
78 static const WCHAR * const types[] = { L"unaware", L"system", L"permonitor", L"permonitorv2" };
79 WCHAR *p, *start, *end;
80 ULONG_PTR i;
82 TRACE( "got dpiAwareness=%s\n", debugstr_w(buffer) );
83 for (start = buffer; *start; start = end)
85 start += wcsspn( start, L" \t\r\n" );
86 if (!(end = wcschr( start, ',' ))) end = start + lstrlenW(start);
87 else *end++ = 0;
88 if ((p = wcspbrk( start, L" \t\r\n" ))) *p = 0;
89 for (i = 0; i < ARRAY_SIZE(types); i++)
91 if (wcsicmp( start, types[i] )) continue;
92 SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT)~i );
93 return;
97 else if (QueryActCtxSettingsW( 0, NULL, L"http://schemas.microsoft.com/SMI/2005/WindowsSettings",
98 L"dpiAware", buffer, ARRAY_SIZE(buffer), NULL ))
100 TRACE( "got dpiAware=%s\n", debugstr_w(buffer) );
101 if (!wcsicmp( buffer, L"true" ))
102 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_SYSTEM_AWARE );
103 else if (!wcsicmp( buffer, L"true/pm" ) || !wcsicmp( buffer, L"per monitor" ))
104 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
105 else
106 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_UNAWARE );
110 static NTSTATUS WINAPI User32CopyImage( const struct copy_image_params *params, ULONG size )
112 HANDLE ret = CopyImage( params->hwnd, params->type, params->dx, params->dy, params->flags );
113 return HandleToUlong( ret );
116 static NTSTATUS WINAPI User32DrawScrollBar( const struct draw_scroll_bar_params *params, ULONG size )
118 RECT rect = params->rect;
119 user_api->pScrollBarDraw( params->hwnd, params->hdc, params->bar, params->hit_test,
120 &params->tracking_info, params->arrows, params->interior,
121 &rect, params->enable_flags, params->arrow_size, params->thumb_pos,
122 params->thumb_size, params->vertical );
123 return 0;
126 static NTSTATUS WINAPI User32DrawText( const struct draw_text_params *params, ULONG size )
128 size -= FIELD_OFFSET( struct draw_text_params, str );
129 return DrawTextW( params->hdc, params->str, size / sizeof(WCHAR), params->rect, params->flags );
132 static NTSTATUS WINAPI User32ImmProcessKey( const struct imm_process_key_params *params, ULONG size )
134 return ImmProcessKey( params->hwnd, params->hkl, params->vkey, params->key_data, 0 );
137 static NTSTATUS WINAPI User32ImmTranslateMessage( const struct imm_translate_message_params *params,
138 ULONG size )
140 return ImmTranslateMessage( params->hwnd, params->msg, params->wparam, params->key_data );
143 static NTSTATUS WINAPI User32LoadImage( const struct load_image_params *params, ULONG size )
145 HANDLE ret = LoadImageW( params->hinst, params->name, params->type,
146 params->dx, params->dy, params->flags );
147 return HandleToUlong( ret );
150 static NTSTATUS WINAPI User32LoadSysMenu( const struct load_sys_menu_params *params, ULONG size )
152 HMENU ret = LoadMenuW( user32_module, params->mdi ? L"SYSMENUMDI" : L"SYSMENU" );
153 return HandleToUlong( ret );
156 static NTSTATUS WINAPI User32FreeCachedClipboardData( const struct free_cached_data_params *params,
157 ULONG size )
159 free_cached_data( params->format, params->handle );
160 return 0;
163 static NTSTATUS WINAPI User32PostDDEMessage( const struct post_dde_message_params *params, ULONG size )
165 return post_dde_message( params->hwnd, params->msg, params->wparam, params->lparam,
166 params->dest_tid, params->type );
169 static NTSTATUS WINAPI User32RenderSsynthesizedFormat( const struct render_synthesized_format_params *params,
170 ULONG size )
172 render_synthesized_format( params->format, params->from );
173 return 0;
176 static BOOL WINAPI User32LoadDriver( const WCHAR *path, ULONG size )
178 return LoadLibraryW( path ) != NULL;
181 static NTSTATUS WINAPI User32UnpackDDEMessage( const struct unpack_dde_message_params *params, ULONG size )
183 struct unpack_dde_message_result *result = params->result;
184 result->wparam = params->wparam;
185 result->lparam = params->lparam;
186 size -= FIELD_OFFSET( struct unpack_dde_message_params, data );
187 return unpack_dde_message( params->hwnd, params->message, &result->wparam, &result->lparam,
188 params->data, size );
191 static const void *kernel_callback_table[NtUserCallCount] =
193 User32CallEnumDisplayMonitor,
194 User32CallSendAsyncCallback,
195 User32CallWinEventHook,
196 User32CallWindowProc,
197 User32CallWindowsHook,
198 User32CopyImage,
199 User32DrawScrollBar,
200 User32DrawText,
201 User32FreeCachedClipboardData,
202 User32ImmProcessKey,
203 User32ImmTranslateMessage,
204 User32InitBuiltinClasses,
205 User32LoadDriver,
206 User32LoadImage,
207 User32LoadSysMenu,
208 User32PostDDEMessage,
209 User32RenderSsynthesizedFormat,
210 User32UnpackDDEMessage,
214 /***********************************************************************
215 * USER initialisation routine
217 static BOOL process_attach(void)
219 NtCurrentTeb()->Peb->KernelCallbackTable = kernel_callback_table;
221 dpiaware_init();
222 winproc_init();
224 /* Initialize system colors and metrics */
225 SYSPARAMS_Init();
227 return TRUE;
231 /**********************************************************************
232 * thread_detach
234 static void thread_detach(void)
236 struct ntuser_thread_info *thread_info = NtUserGetThreadInfo();
238 NtUserCallNoParam( NtUserExitingThread );
240 WDML_NotifyThreadDetach();
242 NtUserCallNoParam( NtUserThreadDetach );
243 HeapFree( GetProcessHeap(), 0, (void *)(UINT_PTR)thread_info->wmchar_data );
247 /***********************************************************************
248 * UserClientDllInitialize (USER32.@)
250 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
252 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
254 static HMODULE imm32_module;
255 BOOL ret = TRUE;
257 switch(reason)
259 case DLL_PROCESS_ATTACH:
260 user32_module = inst;
261 ret = process_attach();
262 if(ret)
263 imm32_module = LoadLibraryW(L"imm32.dll");
264 break;
265 case DLL_THREAD_DETACH:
266 thread_detach();
267 break;
268 case DLL_PROCESS_DETACH:
269 FreeLibrary(imm32_module);
270 break;
272 return ret;
276 /***********************************************************************
277 * ExitWindowsEx (USER32.@)
279 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
281 WCHAR app[MAX_PATH];
282 WCHAR cmdline[MAX_PATH + 64];
283 PROCESS_INFORMATION pi;
284 STARTUPINFOW si;
285 void *redir;
287 GetSystemDirectoryW( app, MAX_PATH - ARRAY_SIZE( L"\\wineboot.exe" ));
288 lstrcatW( app, L"\\wineboot.exe" );
289 lstrcpyW( cmdline, app );
291 if (flags & EWX_FORCE) lstrcatW( cmdline, L" --kill" );
292 else
294 lstrcatW( cmdline, L" --end-session" );
295 if (flags & EWX_FORCEIFHUNG) lstrcatW( cmdline, L" --force" );
297 if (!(flags & EWX_REBOOT)) lstrcatW( cmdline, L" --shutdown" );
299 memset( &si, 0, sizeof si );
300 si.cb = sizeof si;
301 Wow64DisableWow64FsRedirection( &redir );
302 if (!CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
304 Wow64RevertWow64FsRedirection( redir );
305 ERR( "Failed to run %s\n", debugstr_w(cmdline) );
306 return FALSE;
308 Wow64RevertWow64FsRedirection( redir );
309 CloseHandle( pi.hProcess );
310 CloseHandle( pi.hThread );
311 return TRUE;
314 /***********************************************************************
315 * LockWorkStation (USER32.@)
317 BOOL WINAPI LockWorkStation(void)
319 TRACE(": stub\n");
320 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
321 return FALSE;
324 /***********************************************************************
325 * RegisterServicesProcess (USER32.@)
327 int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
329 FIXME("(0x%lx): stub\n", ServicesProcessId);
330 return 0;
333 /***********************************************************************
334 * ShutdownBlockReasonCreate (USER32.@)
336 BOOL WINAPI ShutdownBlockReasonCreate(HWND hwnd, LPCWSTR reason)
338 FIXME("(%p, %s): stub\n", hwnd, debugstr_w(reason));
339 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
340 return FALSE;
343 /***********************************************************************
344 * ShutdownBlockReasonDestroy (USER32.@)
346 BOOL WINAPI ShutdownBlockReasonDestroy(HWND hwnd)
348 FIXME("(%p): stub\n", hwnd);
349 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
350 return FALSE;
353 const char *SPY_GetMsgName( UINT msg, HWND hwnd )
355 return (const char *)NtUserCallHwndParam( hwnd, msg, NtUserSpyGetMsgName );
358 void SPY_EnterMessage( INT flag, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
360 if (TRACE_ON(message)) NtUserMessageCall( hwnd, msg, wparam, lparam, 0, NtUserSpyEnter, flag );
363 void SPY_ExitMessage( INT flag, HWND hwnd, UINT msg, LRESULT lreturn, WPARAM wparam, LPARAM lparam )
365 if (TRACE_ON(message)) NtUserMessageCall( hwnd, msg, wparam, lparam, (void *)lreturn,
366 NtUserSpyExit, flag );