Leaked source code of windows server 2003
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.

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