* Sync with recent trunk (r52637).
[reactos.git] / base / system / userinit / userinit.c
1 /*
2 * ReactOS applications
3 * Copyright (C) 2001, 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS Userinit Logon Application
22 * FILE: subsys/system/userinit/userinit.c
23 * PROGRAMMERS: Thomas Weidenmueller (w3seek@users.sourceforge.net)
24 * Hervé Poussineau (hpoussin@reactos.org)
25 */
26 #include <windows.h>
27 #include <cfgmgr32.h>
28 #include <regstr.h>
29 #include <shlobj.h>
30 #include <shlwapi.h>
31 #include "resource.h"
32 #include <wine/debug.h>
33 #include <win32k/ntusrtyp.h>
34
35 WINE_DEFAULT_DEBUG_CHANNEL(userinit);
36
37 #define CMP_MAGIC 0x01234567
38
39 /* GLOBALS ******************************************************************/
40
41 /* FUNCTIONS ****************************************************************/
42
43 static LONG
44 ReadRegSzKey(
45 IN HKEY hKey,
46 IN LPCWSTR pszKey,
47 OUT LPWSTR* pValue)
48 {
49 LONG rc;
50 DWORD dwType;
51 DWORD cbData = 0;
52 LPWSTR Value;
53
54 TRACE("(%p, %s, %p)\n", hKey, debugstr_w(pszKey), pValue);
55
56 rc = RegQueryValueExW(hKey, pszKey, NULL, &dwType, NULL, &cbData);
57 if (rc != ERROR_SUCCESS)
58 {
59 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey), rc);
60 return rc;
61 }
62 if (dwType != REG_SZ)
63 {
64 WARN("Wrong registry data type (%u vs %u)\n", dwType, REG_SZ);
65 return ERROR_FILE_NOT_FOUND;
66 }
67 Value = (WCHAR*) HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR));
68 if (!Value)
69 {
70 WARN("No memory\n");
71 return ERROR_NOT_ENOUGH_MEMORY;
72 }
73 rc = RegQueryValueExW(hKey, pszKey, NULL, NULL, (LPBYTE)Value, &cbData);
74 if (rc != ERROR_SUCCESS)
75 {
76 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey), rc);
77 HeapFree(GetProcessHeap(), 0, Value);
78 return rc;
79 }
80 /* NULL-terminate the string */
81 Value[cbData / sizeof(WCHAR)] = '\0';
82
83 *pValue = Value;
84 return ERROR_SUCCESS;
85 }
86
87 static
88 BOOL IsConsoleShell(VOID)
89 {
90 HKEY ControlKey = NULL;
91 LPWSTR SystemStartOptions = NULL;
92 LPWSTR CurrentOption, NextOption; /* Pointers into SystemStartOptions */
93 LONG rc;
94 BOOL ret = FALSE;
95
96 TRACE("()\n");
97
98 rc = RegOpenKeyEx(
99 HKEY_LOCAL_MACHINE,
100 REGSTR_PATH_CURRENT_CONTROL_SET,
101 0,
102 KEY_QUERY_VALUE,
103 &ControlKey);
104 if (rc != ERROR_SUCCESS)
105 {
106 WARN("RegOpenKeyEx() failed with error %lu\n", rc);
107 goto cleanup;
108 }
109
110 rc = ReadRegSzKey(ControlKey, L"SystemStartOptions", &SystemStartOptions);
111 if (rc != ERROR_SUCCESS)
112 {
113 WARN("ReadRegSzKey() failed with error %lu\n", rc);
114 goto cleanup;
115 }
116
117 /* Check for CONSOLE in SystemStartOptions */
118 CurrentOption = SystemStartOptions;
119 while (CurrentOption)
120 {
121 NextOption = wcschr(CurrentOption, L' ');
122 if (NextOption)
123 *NextOption = L'\0';
124 if (_wcsicmp(CurrentOption, L"CONSOLE") == 0)
125 {
126 TRACE("Found 'CONSOLE' boot option\n");
127 ret = TRUE;
128 goto cleanup;
129 }
130 CurrentOption = NextOption ? NextOption + 1 : NULL;
131 }
132
133 cleanup:
134 if (ControlKey != NULL)
135 RegCloseKey(ControlKey);
136 HeapFree(GetProcessHeap(), 0, SystemStartOptions);
137 TRACE("IsConsoleShell() returning %d\n", ret);
138 return ret;
139 }
140
141 static
142 BOOL GetShell(
143 OUT WCHAR *CommandLine, /* must be at least MAX_PATH long */
144 IN HKEY hRootKey)
145 {
146 HKEY hKey;
147 DWORD Type, Size;
148 WCHAR Shell[MAX_PATH];
149 BOOL Ret = FALSE;
150 BOOL ConsoleShell = IsConsoleShell();
151 LONG rc;
152
153 TRACE("(%p, %p)\n", CommandLine, hRootKey);
154
155 rc = RegOpenKeyExW(hRootKey, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
156 0, KEY_QUERY_VALUE, &hKey);
157 if (rc == ERROR_SUCCESS)
158 {
159 Size = MAX_PATH * sizeof(WCHAR);
160 rc = RegQueryValueExW(hKey,
161 ConsoleShell ? L"ConsoleShell" : L"Shell",
162 NULL,
163 &Type,
164 (LPBYTE)Shell,
165 &Size);
166 if (rc == ERROR_SUCCESS)
167 {
168 if ((Type == REG_SZ) || (Type == REG_EXPAND_SZ))
169 {
170 TRACE("Found command line %s\n", debugstr_w(Shell));
171 wcscpy(CommandLine, Shell);
172 Ret = TRUE;
173 }
174 else
175 WARN("Wrong type %lu (expected %u or %u)\n", Type, REG_SZ, REG_EXPAND_SZ);
176 }
177 else
178 WARN("RegQueryValueEx() failed with error %lu\n", rc);
179 RegCloseKey(hKey);
180 }
181 else
182 WARN("RegOpenKeyEx() failed with error %lu\n", rc);
183
184 return Ret;
185 }
186
187 static VOID
188 StartAutoApplications(
189 IN INT clsid)
190 {
191 WCHAR szPath[MAX_PATH] = {0};
192 HRESULT hResult;
193 HANDLE hFind;
194 WIN32_FIND_DATAW findData;
195 SHELLEXECUTEINFOW ExecInfo;
196 size_t len;
197
198 TRACE("(%d)\n", clsid);
199
200 hResult = SHGetFolderPathW(NULL, clsid, NULL, SHGFP_TYPE_CURRENT, szPath);
201 len = wcslen(szPath);
202 if (!SUCCEEDED(hResult) || len == 0)
203 {
204 WARN("SHGetFolderPath() failed with error %lu\n", GetLastError());
205 return;
206 }
207
208 wcscat(szPath, L"\\*");
209 hFind = FindFirstFileW(szPath, &findData);
210 if (hFind == INVALID_HANDLE_VALUE)
211 {
212 WARN("FindFirstFile(%s) failed with error %lu\n", debugstr_w(szPath), GetLastError());
213 return;
214 }
215
216 do
217 {
218 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (findData.nFileSizeHigh || findData.nFileSizeLow))
219 {
220 memset(&ExecInfo, 0x0, sizeof(SHELLEXECUTEINFOW));
221 ExecInfo.cbSize = sizeof(ExecInfo);
222 wcscpy(&szPath[len+1], findData.cFileName);
223 ExecInfo.lpVerb = L"open";
224 ExecInfo.lpFile = szPath;
225 ExecInfo.lpDirectory = NULL;
226 TRACE("Executing %s in directory %s\n",
227 debugstr_w(findData.cFileName), debugstr_w(szPath));
228 ShellExecuteExW(&ExecInfo);
229 }
230 } while (FindNextFileW(hFind, &findData));
231 FindClose(hFind);
232 }
233
234 static BOOL
235 TryToStartShell(
236 IN LPCWSTR Shell)
237 {
238 STARTUPINFO si;
239 PROCESS_INFORMATION pi;
240 WCHAR ExpandedShell[MAX_PATH];
241
242 TRACE("(%s)\n", debugstr_w(Shell));
243
244 ZeroMemory(&si, sizeof(STARTUPINFO));
245 si.cb = sizeof(STARTUPINFO);
246 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
247
248 ExpandEnvironmentStrings(Shell, ExpandedShell, MAX_PATH);
249
250 if (!CreateProcess(NULL,
251 ExpandedShell,
252 NULL,
253 NULL,
254 FALSE,
255 NORMAL_PRIORITY_CLASS,
256 NULL,
257 NULL,
258 &si,
259 &pi))
260 {
261 WARN("CreateProcess() failed with error %lu\n", GetLastError());
262 return FALSE;
263 }
264
265 StartAutoApplications(CSIDL_STARTUP);
266 StartAutoApplications(CSIDL_COMMON_STARTUP);
267 CloseHandle(pi.hProcess);
268 CloseHandle(pi.hThread);
269 return TRUE;
270 }
271
272 static
273 VOID StartShell(VOID)
274 {
275 WCHAR Shell[MAX_PATH];
276 TCHAR szMsg[RC_STRING_MAX_SIZE];
277 DWORD Type, Size;
278 DWORD Value = 0;
279 LONG rc;
280 HKEY hKey;
281
282 TRACE("()\n");
283
284 /* Safe Mode shell run */
285 rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
286 L"SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Option",
287 0, KEY_QUERY_VALUE, &hKey);
288 if(rc == ERROR_SUCCESS)
289 {
290 Size = sizeof(Value);
291 rc = RegQueryValueExW(hKey, L"UseAlternateShell", NULL,
292 &Type, (LPBYTE)&Value, &Size);
293 if(rc == ERROR_SUCCESS)
294 {
295 RegCloseKey(hKey);
296 if(Type == REG_DWORD)
297 {
298 if(Value)
299 {
300 /* Safe Mode Alternate Shell required */
301 rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
302 L"SYSTEM\\CurrentControlSet\\Control\\SafeBoot",
303 0, KEY_READ, &hKey);
304 if(rc == ERROR_SUCCESS)
305 {
306 Size = MAX_PATH * sizeof(WCHAR);
307 rc = RegQueryValueExW(hKey, L"AlternateShell", NULL,
308 &Type, (LPBYTE)Shell, &Size);
309 if(rc == ERROR_SUCCESS)
310 {
311 RegCloseKey(hKey);
312 if ((Type == REG_SZ) || (Type == REG_EXPAND_SZ))
313 {
314 TRACE("Key located - %s\n", debugstr_w(Shell));
315 /* Try to run alternate shell */
316 if (TryToStartShell(Shell))
317 {
318 TRACE("Alternate shell started (Safe Mode)\n");
319 return;
320 }
321 }
322 else
323 {
324 WARN("Wrong type %lu (expected %u or %u)\n",
325 Type, REG_SZ, REG_EXPAND_SZ);
326 }
327 }
328 else
329 {
330 WARN("Alternate shell in Safe Mode required but not specified.");
331 }
332 }
333 }
334 }
335 else
336 {
337 WARN("Wrong type %lu (expected %u)\n", Type, REG_DWORD);
338 }
339 }
340 }
341 /* Try to run shell in user key */
342 if (GetShell(Shell, HKEY_CURRENT_USER) && TryToStartShell(Shell))
343 {
344 TRACE("Started shell from HKEY_CURRENT_USER\n");
345 return;
346 }
347
348 /* Try to run shell in local machine key */
349 if (GetShell(Shell, HKEY_LOCAL_MACHINE) && TryToStartShell(Shell))
350 {
351 TRACE("Started shell from HKEY_LOCAL_MACHINE\n");
352 return;
353 }
354
355 /* Try default shell */
356 if (IsConsoleShell())
357 {
358 if (GetSystemDirectory(Shell, MAX_PATH - 8))
359 wcscat(Shell, L"\\cmd.exe");
360 else
361 wcscpy(Shell, L"cmd.exe");
362 }
363 else
364 {
365 if (GetWindowsDirectory(Shell, MAX_PATH - 13))
366 wcscat(Shell, L"\\explorer.exe");
367 else
368 wcscpy(Shell, L"explorer.exe");
369 }
370 if (!TryToStartShell(Shell))
371 {
372 WARN("Failed to start default shell %s\n", debugstr_w(Shell));
373 LoadString( GetModuleHandle(NULL), STRING_USERINIT_FAIL, szMsg, sizeof(szMsg) / sizeof(szMsg[0]));
374 MessageBox(0, szMsg, NULL, 0);
375 }
376 }
377
378 const WCHAR g_RegColorNames[][32] = {
379 L"Scrollbar", /* 00 = COLOR_SCROLLBAR */
380 L"Background", /* 01 = COLOR_DESKTOP */
381 L"ActiveTitle", /* 02 = COLOR_ACTIVECAPTION */
382 L"InactiveTitle", /* 03 = COLOR_INACTIVECAPTION */
383 L"Menu", /* 04 = COLOR_MENU */
384 L"Window", /* 05 = COLOR_WINDOW */
385 L"WindowFrame", /* 06 = COLOR_WINDOWFRAME */
386 L"MenuText", /* 07 = COLOR_MENUTEXT */
387 L"WindowText", /* 08 = COLOR_WINDOWTEXT */
388 L"TitleText", /* 09 = COLOR_CAPTIONTEXT */
389 L"ActiveBorder", /* 10 = COLOR_ACTIVEBORDER */
390 L"InactiveBorder", /* 11 = COLOR_INACTIVEBORDER */
391 L"AppWorkSpace", /* 12 = COLOR_APPWORKSPACE */
392 L"Hilight", /* 13 = COLOR_HIGHLIGHT */
393 L"HilightText", /* 14 = COLOR_HIGHLIGHTTEXT */
394 L"ButtonFace", /* 15 = COLOR_BTNFACE */
395 L"ButtonShadow", /* 16 = COLOR_BTNSHADOW */
396 L"GrayText", /* 17 = COLOR_GRAYTEXT */
397 L"ButtonText", /* 18 = COLOR_BTNTEXT */
398 L"InactiveTitleText", /* 19 = COLOR_INACTIVECAPTIONTEXT */
399 L"ButtonHilight", /* 20 = COLOR_BTNHIGHLIGHT */
400 L"ButtonDkShadow", /* 21 = COLOR_3DDKSHADOW */
401 L"ButtonLight", /* 22 = COLOR_3DLIGHT */
402 L"InfoText", /* 23 = COLOR_INFOTEXT */
403 L"InfoWindow", /* 24 = COLOR_INFOBK */
404 L"ButtonAlternateFace", /* 25 = COLOR_ALTERNATEBTNFACE */
405 L"HotTrackingColor", /* 26 = COLOR_HOTLIGHT */
406 L"GradientActiveTitle", /* 27 = COLOR_GRADIENTACTIVECAPTION */
407 L"GradientInactiveTitle", /* 28 = COLOR_GRADIENTINACTIVECAPTION */
408 L"MenuHilight", /* 29 = COLOR_MENUHILIGHT */
409 L"MenuBar" /* 30 = COLOR_MENUBAR */
410 };
411 #define NUM_SYSCOLORS (sizeof(g_RegColorNames) / sizeof(g_RegColorNames[0]))
412
413 static
414 COLORREF StrToColorref(
415 IN LPWSTR lpszCol)
416 {
417 BYTE rgb[3];
418
419 TRACE("(%s)\n", debugstr_w(lpszCol));
420
421 rgb[0] = StrToIntW(lpszCol);
422 lpszCol = StrChrW(lpszCol, L' ') + 1;
423 rgb[1] = StrToIntW(lpszCol);
424 lpszCol = StrChrW(lpszCol, L' ') + 1;
425 rgb[2] = StrToIntW(lpszCol);
426 return RGB(rgb[0], rgb[1], rgb[2]);
427 }
428
429 static
430 VOID SetUserSysColors(VOID)
431 {
432 HKEY hKey;
433 INT i;
434 WCHAR szColor[20];
435 DWORD Type, Size;
436 COLORREF crColor;
437 LONG rc;
438
439 TRACE("()\n");
440
441 rc = RegOpenKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_COLORS,
442 0, KEY_QUERY_VALUE, &hKey);
443 if (rc != ERROR_SUCCESS)
444 {
445 WARN("RegOpenKeyEx() failed with error %lu\n", rc);
446 return;
447 }
448 for(i = 0; i < NUM_SYSCOLORS; i++)
449 {
450 Size = sizeof(szColor);
451 rc = RegQueryValueEx(hKey, g_RegColorNames[i], NULL, &Type,
452 (LPBYTE)szColor, &Size);
453 if (rc == ERROR_SUCCESS && Type == REG_SZ)
454 {
455 crColor = StrToColorref(szColor);
456 SetSysColors(1, &i, &crColor);
457 }
458 else
459 WARN("RegQueryValueEx(%s) failed with error %lu\n",
460 debugstr_w(g_RegColorNames[i]), rc);
461 }
462 RegCloseKey(hKey);
463 }
464
465 DWORD
466 WINAPI
467 UpdatePerUserSystemParameters(DWORD dw1, BOOL bEnable);
468
469
470 static
471 VOID SetUserWallpaper(VOID)
472 {
473 HKEY hKey;
474 DWORD Type, Size;
475 WCHAR szWallpaper[MAX_PATH + 1];
476 LONG rc;
477
478 TRACE("()\n");
479
480 rc = RegOpenKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_DESKTOP,
481 0, KEY_QUERY_VALUE, &hKey);
482 if (rc == ERROR_SUCCESS)
483 {
484 Size = sizeof(szWallpaper);
485 rc = RegQueryValueEx(hKey,
486 L"Wallpaper",
487 NULL,
488 &Type,
489 (LPBYTE)szWallpaper,
490 &Size);
491 if (rc == ERROR_SUCCESS && Type == REG_SZ)
492 {
493 ExpandEnvironmentStrings(szWallpaper, szWallpaper, MAX_PATH);
494 TRACE("Using wallpaper %s\n", debugstr_w(szWallpaper));
495
496 /* Load and change the wallpaper */
497 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, szWallpaper, SPIF_SENDCHANGE);
498 }
499 else
500 {
501 /* remove the wallpaper */
502 TRACE("No wallpaper set in registry (error %lu)\n", rc);
503 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, NULL, SPIF_SENDCHANGE);
504 }
505 RegCloseKey(hKey);
506 }
507 else
508 WARN("RegOpenKeyEx() failed with error %lu\n", rc);
509 }
510
511 static
512 VOID SetUserSettings(VOID)
513 {
514 TRACE("()\n");
515
516 UpdatePerUserSystemParameters(1, TRUE);
517 SetUserSysColors();
518 SetUserWallpaper();
519 }
520
521 typedef DWORD (WINAPI *PCMP_REPORT_LOGON)(DWORD, DWORD);
522
523 static VOID
524 NotifyLogon(VOID)
525 {
526 HINSTANCE hModule;
527 PCMP_REPORT_LOGON CMP_Report_LogOn;
528
529 TRACE("()\n");
530
531 hModule = LoadLibrary(L"setupapi.dll");
532 if (hModule)
533 {
534 CMP_Report_LogOn = (PCMP_REPORT_LOGON)GetProcAddress(hModule, "CMP_Report_LogOn");
535 if (CMP_Report_LogOn)
536 CMP_Report_LogOn(CMP_MAGIC, GetCurrentProcessId());
537 else
538 WARN("GetProcAddress() failed\n");
539
540 FreeLibrary(hModule);
541 }
542 else
543 WARN("LoadLibrary() failed with error %lu\n", GetLastError());
544 }
545
546 #ifdef _MSC_VER
547 #pragma warning(disable : 4100)
548 #endif /* _MSC_VER */
549
550 int WINAPI
551 wWinMain(IN HINSTANCE hInst,
552 IN HINSTANCE hPrevInstance,
553 IN LPWSTR lpszCmdLine,
554 IN int nCmdShow)
555 {
556 SetUserSettings();
557 StartShell();
558 NotifyLogon();
559 return 0;
560 }
561
562 /* EOF */