Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

416 lines
10 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <ntexapi.h>
  5. #include <windows.h>
  6. #include <scrnsave.h>
  7. #include <logon.h>
  8. // For IsOS()
  9. #include <shlwapi.h>
  10. #include <shlwapip.h>
  11. // for added XPSP1 resources
  12. #include "winbrand.h"
  13. #include "debug.h"
  14. #ifndef ARRAYSIZE
  15. #define ARRAYSIZE(a) (sizeof(a)/sizeof(*a))
  16. #endif
  17. HANDLE hInst;
  18. int cxhwndLogon;
  19. int cyhwndLogon;
  20. int xScreen; // the top-left corner of the screen, might be different than (0,0)
  21. int yScreen;
  22. int cxScreen;
  23. int cyScreen;
  24. HBRUSH hbrBlack;
  25. HDC hdcLogon;
  26. HWND hwndLogon;
  27. HBITMAP hbmLogon = NULL;
  28. HICON ghiconLogon = NULL;
  29. HICON hMovingIcon = NULL;
  30. HPALETTE ghpal = NULL;
  31. #define APPCLASS "LOGON"
  32. #define MAX_CAPTION_LENGTH 128
  33. DWORD FAR lRandom(VOID)
  34. {
  35. static DWORD glSeed = (DWORD)-365387184;
  36. glSeed *= 69069;
  37. return(++glSeed);
  38. }
  39. HPALETTE GetPalette(HBITMAP hbm)
  40. {
  41. DIBSECTION ds;
  42. int i;
  43. HANDLE hmem;
  44. HDC hdc, hdcMem;
  45. LOGPALETTE *ppal;
  46. HPALETTE hpal;
  47. RGBQUAD rgbquad[256];
  48. USHORT nColors;
  49. GetObject(hbm, sizeof(DIBSECTION), &ds);
  50. if (ds.dsBmih.biBitCount > 8)
  51. return NULL;
  52. nColors = (ds.dsBmih.biBitCount < 16) ? (1 << ds.dsBmih.biBitCount) : 0xffff;
  53. hmem = GlobalAlloc(GHND, sizeof (LOGPALETTE) + sizeof (PALETTEENTRY) * nColors);
  54. if (hmem == NULL)
  55. return NULL;
  56. ppal = (LPLOGPALETTE) GlobalLock(hmem);
  57. hdc = GetDC(NULL);
  58. hdcMem = CreateCompatibleDC(hdc);
  59. SelectObject(hdcMem, hbm);
  60. ppal->palVersion = 0x300;
  61. ppal->palNumEntries = nColors;
  62. GetDIBColorTable(hdcMem, 0, nColors, rgbquad);
  63. for (i = 0; i < nColors; i++) {
  64. ppal->palPalEntry[i].peRed = rgbquad[i].rgbRed;
  65. ppal->palPalEntry[i].peGreen = rgbquad[i].rgbGreen;
  66. ppal->palPalEntry[i].peBlue = rgbquad[i].rgbBlue;
  67. }
  68. hpal = CreatePalette(ppal);
  69. GlobalUnlock(hmem);
  70. GlobalFree(hmem);
  71. DeleteObject(hdcMem);
  72. ReleaseDC(NULL, hdc);
  73. return hpal;
  74. }
  75. LRESULT APIENTRY
  76. WndProc(
  77. HWND hwnd,
  78. UINT message,
  79. WPARAM wParam,
  80. LPARAM lParam)
  81. {
  82. int x, y;
  83. int nColorsChanged;
  84. switch (message) {
  85. PAINTSTRUCT ps;
  86. case WM_PALETTECHANGED:
  87. if ((HWND)wParam == hwnd)
  88. break;
  89. case WM_QUERYNEWPALETTE:
  90. {
  91. HDC hdc = GetDC(hwnd);
  92. SelectPalette(hdc, ghpal, FALSE);
  93. nColorsChanged = RealizePalette(hdc);
  94. ReleaseDC(hwnd, hdc);
  95. if (nColorsChanged != 0) {
  96. InvalidateRect(hwnd, NULL, TRUE);
  97. }
  98. }
  99. break;
  100. case WM_PAINT:
  101. BeginPaint(hwnd, &ps);
  102. SelectPalette(ps.hdc, ghpal, FALSE);
  103. BitBlt(ps.hdc, 0, 0, cxhwndLogon, cyhwndLogon, hdcLogon, 0, 0, SRCCOPY);
  104. EndPaint(hwnd, &ps);
  105. break;
  106. case WM_TIMER:
  107. /*
  108. * Pick a new place on the screen to put the dialog.
  109. */
  110. x = lRandom() % (cxScreen - cxhwndLogon) + xScreen;
  111. y = lRandom() % (cyScreen - cyhwndLogon) + yScreen;
  112. SetWindowPos(hwndLogon, NULL, x, y, 0, 0,
  113. SWP_NOSIZE | SWP_NOZORDER);
  114. break;
  115. case WM_CLOSE:
  116. ExitProcess(0);
  117. break;
  118. case WM_SETFOCUS:
  119. /*
  120. * Don't allow DefDlgProc() to do default processing on this
  121. * message because it'll set the focus to the first control and
  122. * we want it set to the main dialog so that DefScreenSaverProc()
  123. * will see the key input and cancel the screen saver.
  124. */
  125. return TRUE;
  126. break;
  127. /*
  128. * Call DefScreenSaverProc() so we get its default processing (so it
  129. * can detect key and mouse input).
  130. */
  131. default:
  132. return DefScreenSaverProc(hwnd, message, wParam, lParam) ? TRUE : FALSE;
  133. }
  134. return 0;
  135. }
  136. int sx;
  137. int sy;
  138. LRESULT OnCreateSS(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  139. {
  140. // Background window is black
  141. // Make sure we use the entire virtual desktop size for multiple
  142. // displays:
  143. cxScreen = ((LPCREATESTRUCT)lParam)->cx;
  144. cyScreen = ((LPCREATESTRUCT)lParam)->cy;
  145. xScreen = ((LPCREATESTRUCT)lParam)->x;
  146. yScreen = ((LPCREATESTRUCT)lParam)->y;
  147. hbrBlack = GetStockObject(BLACK_BRUSH);
  148. if (!fChildPreview)
  149. {
  150. WNDCLASS wndClass;
  151. BITMAP bm = {0};
  152. UINT uXpSpLevel = 0;
  153. HMODULE hResourceDll = hMainInstance;
  154. if (hbmLogon == NULL)
  155. {
  156. LPTSTR res;
  157. // Embedded OS has it's own logo
  158. if (IsOS(OS_EMBEDDED))
  159. {
  160. res = MAKEINTRESOURCE(IDB_EMBEDDED);
  161. }
  162. else if (IsOS(OS_TABLETPC))
  163. {
  164. uXpSpLevel = 1;
  165. res = MAKEINTRESOURCE(IDB_TABLETPC_LOGON_SCR);
  166. }
  167. else if (IsOS(OS_MEDIACENTER))
  168. {
  169. uXpSpLevel = 1;
  170. res = MAKEINTRESOURCE(IDB_MEDIACENTER_LOGON_SCR);
  171. }
  172. else if (IsOS(OS_DATACENTER))
  173. {
  174. AssertMsg(FALSE, "Should not see this on XPSP");
  175. res = MAKEINTRESOURCE(IDB_DATACENTER);
  176. }
  177. else if (IsOS(OS_ADVSERVER))
  178. {
  179. AssertMsg(FALSE, "Should not see this on XPSP");
  180. res = MAKEINTRESOURCE(IDB_ADVANCED);
  181. }
  182. else if (IsOS(OS_SERVER))
  183. {
  184. AssertMsg(FALSE, "Should not see this on XPSP");
  185. res = MAKEINTRESOURCE(IDB_SERVER);
  186. }
  187. else if (IsOS(OS_PERSONAL))
  188. {
  189. res = MAKEINTRESOURCE(IDB_PERSONAL);
  190. }
  191. else
  192. {
  193. res = MAKEINTRESOURCE(IDB_WORKSTA);
  194. }
  195. if (uXpSpLevel > 0)
  196. {
  197. hResourceDll = LoadLibraryEx(TEXT("winbrand.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE);
  198. if (hResourceDll == NULL)
  199. {
  200. hResourceDll = hMainInstance;
  201. }
  202. }
  203. hbmLogon = LoadImage(hResourceDll, res, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  204. if (hbmLogon)
  205. {
  206. ghpal = GetPalette(hbmLogon);
  207. }
  208. if (hResourceDll != hMainInstance)
  209. {
  210. FreeLibrary(hResourceDll);
  211. }
  212. }
  213. if (hbmLogon)
  214. {
  215. GetObject(hbmLogon, sizeof(bm), &bm);
  216. }
  217. cxhwndLogon = bm.bmWidth;
  218. cyhwndLogon = bm.bmHeight;
  219. hdcLogon = CreateCompatibleDC(NULL);
  220. if (hdcLogon && hbmLogon)
  221. {
  222. SelectObject(hdcLogon, hbmLogon);
  223. }
  224. wndClass.style = CS_HREDRAW | CS_VREDRAW;
  225. wndClass.lpfnWndProc = WndProc;
  226. wndClass.cbClsExtra = 0;
  227. wndClass.cbWndExtra = sizeof(LONG);
  228. wndClass.hInstance = hInst;
  229. wndClass.hIcon = NULL;
  230. wndClass.hCursor = NULL;
  231. wndClass.hbrBackground = NULL;
  232. wndClass.lpszMenuName = NULL;
  233. wndClass.lpszClassName = TEXT("LOGON");
  234. RegisterClass(&wndClass);
  235. // Create the window we'll move around every 10 seconds.
  236. hwndLogon = CreateWindowEx(WS_EX_TOPMOST, TEXT("LOGON"), NULL, WS_VISIBLE | WS_POPUP,
  237. 50, 50, cxhwndLogon, cyhwndLogon, hMainWindow, NULL, hInst, NULL);
  238. if (hwndLogon)
  239. {
  240. SetTimer(hwndLogon, 1, 10 * 1000, 0);
  241. }
  242. // Post this message so we activate after this window is created.
  243. PostMessage(hwnd, WM_USER, 0, 0);
  244. }
  245. else
  246. {
  247. SetTimer(hwnd, 1, 10 * 1000, 0);
  248. cxhwndLogon = GetSystemMetrics(SM_CXICON);
  249. cyhwndLogon = GetSystemMetrics(SM_CYICON);
  250. ghiconLogon = LoadIcon(hMainInstance, MAKEINTRESOURCE(1));
  251. sx = lRandom() % (cxScreen - cxhwndLogon) + xScreen;
  252. sy = lRandom() % (cyScreen - cyhwndLogon) + yScreen;
  253. }
  254. return 0;
  255. }
  256. LRESULT APIENTRY ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  257. {
  258. RECT rc;
  259. HDC hdc;
  260. PAINTSTRUCT ps;
  261. switch (message)
  262. {
  263. case WM_CREATE:
  264. OnCreateSS(hwnd, message, wParam, lParam);
  265. break;
  266. case WM_SIZE:
  267. cxScreen = LOWORD(lParam);
  268. cyScreen = HIWORD(lParam);
  269. break;
  270. case WM_WINDOWPOSCHANGING:
  271. /*
  272. * Take down hwndLogon if this window is going invisible.
  273. */
  274. if (hwndLogon == NULL)
  275. break;
  276. if (((LPWINDOWPOS)lParam)->flags & SWP_HIDEWINDOW) {
  277. ShowWindow(hwndLogon, SW_HIDE);
  278. }
  279. break;
  280. case WM_USER:
  281. /*
  282. * Now show and activate this window.
  283. */
  284. if (hwndLogon == NULL)
  285. break;
  286. SetWindowPos(hwndLogon, NULL, 0, 0, 0, 0, SWP_SHOWWINDOW |
  287. SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
  288. break;
  289. case WM_PAINT:
  290. hdc = BeginPaint(hwnd, &ps);
  291. SetRect(&rc, xScreen, yScreen, cxScreen, cyScreen);
  292. FillRect(hdc, &rc, hbrBlack);
  293. if (fChildPreview) {
  294. DrawIcon(hdc, sx, sy, ghiconLogon);
  295. }
  296. EndPaint(hwnd, &ps);
  297. break;
  298. case WM_NCACTIVATE:
  299. /*
  300. * Case out WM_NCACTIVATE so the dialog activates: DefScreenSaverProc
  301. * returns FALSE for this message, not allowing activation.
  302. */
  303. if (!fChildPreview)
  304. return DefWindowProc(hwnd, message, wParam, lParam);
  305. break;
  306. case WM_TIMER:
  307. /*
  308. * Pick a new place on the screen to put the dialog.
  309. */
  310. sx = lRandom() % (cxScreen - cxhwndLogon) + xScreen;
  311. sy = lRandom() % (cyScreen - cyhwndLogon) + yScreen;
  312. InvalidateRect(hwnd, NULL, TRUE);
  313. break;
  314. }
  315. return DefScreenSaverProc(hwnd, message, wParam, lParam);
  316. }
  317. BOOL APIENTRY
  318. ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  319. {
  320. TCHAR ach1[256];
  321. TCHAR ach2[256];
  322. switch (message) {
  323. case WM_INITDIALOG:
  324. /*
  325. * This is hack-o-rama, but fast and cheap.
  326. */
  327. LoadString(hMainInstance, IDS_DESCRIPTION, ach1, ARRAYSIZE(ach1));
  328. LoadString(hMainInstance, 2, ach2, ARRAYSIZE(ach2));
  329. MessageBox(hDlg, ach2, ach1, MB_OK | MB_ICONEXCLAMATION);
  330. EndDialog(hDlg, TRUE);
  331. break;
  332. }
  333. return FALSE;
  334. }
  335. BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
  336. {
  337. return TRUE;
  338. }