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.

405 lines
12 KiB

  1. /*
  2. * SCRNSAVE.C - default screen saver.
  3. *
  4. * this app makes a IdleWild screen saver compatible with the windows 3.1
  5. * screen saver interface.
  6. *
  7. * Usage: SCRNSAVE.EXE saver.iw [/s] [/c]
  8. *
  9. * the IdleWild screen saver 'saver.iw' will be loaded and told to
  10. * screen save. if '/c' is specifed the savers configure dialog will
  11. * be shown.
  12. *
  13. * when the screen saver terminates SCRNSAVE.EXE will terminate too.
  14. *
  15. * if the saver.iw is not specifed or refuses to load then a
  16. * builtin 'blackness' screen saver will be used.
  17. *
  18. * Restrictions:
  19. *
  20. * because only one screen saver is loaded, (not all the screen savers
  21. * like IdleWild.exe does) the random screen saver will not work correctly
  22. *
  23. * History:
  24. * 10/15/90 ToddLa stolen from SOS.C by BradCh
  25. * 6/17/91 stevecat ported to NT Windows
  26. *
  27. */
  28. #include <string.h>
  29. #define WIN31 /* For topmost windows */
  30. #include <windows.h>
  31. #include "strings.h"
  32. #include <stdlib.h>
  33. #define BUFFER_LEN 255
  34. CHAR szAppName[BUFFER_LEN];
  35. CHAR szNoConfigure[BUFFER_LEN];
  36. #define THRESHOLD 3
  37. #define abs(x) ( (x)<0 ? -(x) : (x) )
  38. //
  39. // private stuff in IWLIB.DLL
  40. //
  41. HANDLE hIdleWildDll;
  42. CHAR szIdleWildDll[] = "IWLIB.DLL";
  43. SHORT (*FInitScrSave) (HANDLE, HWND);
  44. VOID (*TermScrSave) (VOID);
  45. VOID (*ScrBlank) (SHORT);
  46. VOID (*ScrSetIgnore) (SHORT);
  47. SHORT (*ScrLoadServer) (CHAR *);
  48. SHORT (*ScrSetServer) (CHAR *);
  49. VOID (*ScrInvokeDlg) (HANDLE, HWND);
  50. HANDLE hMainInstance = NULL;
  51. HWND hwndApp = NULL;
  52. HWND hwndActive = NULL;
  53. HWND hwndPreview = NULL;
  54. BOOL fBlankNow = FALSE;
  55. BOOL fIdleWild = FALSE;
  56. //SHORT wmScrSave = -1;
  57. // changed to what I believe it should be
  58. UINT wmScrSave = 0xffffffff;
  59. typedef LONG (*LPWNDPROC)(); // pointer to a window procedure
  60. BOOL FInitIdleWild (LPSTR szCmdLine);
  61. BOOL FTermIdleWild (VOID);
  62. BOOL FInitApp (HANDLE hInstance, LPSTR szCmdLine, WORD sw);
  63. BOOL FTermApp (VOID);
  64. BOOL FInitDefault (VOID);
  65. LRESULT DefaultProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  66. int __cdecl main (USHORT argc, CHAR **argv)
  67. {
  68. HANDLE hInstance;
  69. HANDLE hPrev = NULL;
  70. LPSTR szCmdLine = GetCommandLine();
  71. WORD sw = SW_SHOWNORMAL;
  72. MSG msg;
  73. hInstance = GetModuleHandle (NULL);
  74. hMainInstance = hInstance;
  75. // If we're already running another instance, get out
  76. if (hPrev != NULL)
  77. return FALSE;
  78. if (!FInitApp (hInstance, szCmdLine, sw))
  79. {
  80. //MessageBox (NULL, "Cannot initialize!", szAppName, MB_OK);
  81. return FALSE;
  82. }
  83. while (GetMessage (&msg, NULL, 0, 0))
  84. {
  85. //
  86. // IWLIB.DLL will brodcast a message when the screen saving
  87. // is done.
  88. //
  89. if (msg.message == wmScrSave && msg.wParam == FALSE)
  90. break;
  91. TranslateMessage (&msg);
  92. DispatchMessage (&msg);
  93. }
  94. return FTermApp ();
  95. }
  96. BOOL FTermApp (VOID)
  97. {
  98. ////FTermDefault ();
  99. FTermIdleWild ();
  100. return TRUE;
  101. }
  102. BOOL FInitApp (HANDLE hInstance, LPSTR szCmdLine, WORD sw)
  103. {
  104. LPSTR lpch, lpT;
  105. LoadString(hMainInstance, idsAppName, szAppName, BUFFER_LEN);
  106. LoadString(hMainInstance, idsNoConfigure, szNoConfigure, BUFFER_LEN);
  107. //=================================================================
  108. // on NT, szCmdLine's first string includes its own name, remove this
  109. // to make it exactly like the windows command line.
  110. if (*szCmdLine)
  111. {
  112. lpT = strchr(szCmdLine, ' '); // skip self name
  113. if (lpT)
  114. {
  115. szCmdLine = lpT;
  116. while (*szCmdLine == ' ')
  117. szCmdLine++; // skip spaces to end or first cmd
  118. }
  119. else
  120. {
  121. szCmdLine += strlen(szCmdLine); // point to NULL
  122. }
  123. }
  124. //=====================================================================
  125. //
  126. // parse command line looking for switches
  127. //
  128. for (lpch = szCmdLine; *lpch != '\0'; lpch += 1)
  129. {
  130. if (*lpch == '/' || *lpch == '-')
  131. {
  132. if (lpch[1] == 's' || lpch[1] == 'S')
  133. fBlankNow = TRUE;
  134. if (lpch[1] == 'c' || lpch[1] == 'C')
  135. hwndActive = GetActiveWindow ();
  136. if (lpch[1] == 'p' || lpch[1] == 'P')
  137. {
  138. fBlankNow = TRUE;
  139. hwndPreview = (HWND)IntToPtr(atoi(lpch+2));
  140. break;
  141. }
  142. lpch[0] = ' ';
  143. lpch[1] = ' ';
  144. }
  145. }
  146. //
  147. // try to load the IdleWild screen saver, if none specifed or
  148. // we are unable to load it then use the default one.
  149. //
  150. if (FInitIdleWild (szCmdLine))
  151. {
  152. if (fBlankNow)
  153. {
  154. ScrSetIgnore (1);
  155. ScrBlank (TRUE);
  156. }
  157. else
  158. {
  159. ScrInvokeDlg (hMainInstance, hwndActive);
  160. PostQuitMessage (0);
  161. }
  162. }
  163. else if (!fBlankNow || !FInitDefault ())
  164. {
  165. MessageBox (hwndActive, szNoConfigure, szAppName, MB_OK | MB_ICONEXCLAMATION);
  166. PostQuitMessage (0);
  167. }
  168. return TRUE;
  169. }
  170. //
  171. // run-time link to IWLIB.DLL
  172. //
  173. BOOL FInitIdleWild (LPSTR szCmdLine)
  174. {
  175. OFSTRUCT of;
  176. while (*szCmdLine == ' ')
  177. szCmdLine++;
  178. if (*szCmdLine == 0)
  179. return FALSE;
  180. if (-1 == OpenFile(szIdleWildDll, &of, OF_EXIST | OF_SHARE_DENY_NONE) ||
  181. -1 == OpenFile(szCmdLine, &of, OF_EXIST | OF_SHARE_DENY_NONE))
  182. return FALSE;
  183. if ((hIdleWildDll = LoadLibrary (szIdleWildDll)) == NULL)
  184. return FALSE;
  185. FInitScrSave = (SHORT (*) (HANDLE, HWND))GetProcAddress (hIdleWildDll, "FInitScrSave" );
  186. TermScrSave = (VOID (*) (VOID)) GetProcAddress (hIdleWildDll, "TermScrSave" );
  187. ScrBlank = (VOID (*) (SHORT)) GetProcAddress (hIdleWildDll, "ScrBlank" );
  188. ScrSetIgnore = (VOID (*) (SHORT)) GetProcAddress (hIdleWildDll, "ScrSetIgnore" );
  189. ScrLoadServer = (SHORT (*) (CHAR *)) GetProcAddress (hIdleWildDll, "ScrLoadServer");
  190. ScrSetServer = (SHORT (*) (CHAR *)) GetProcAddress (hIdleWildDll, "ScrSetServer" );
  191. ScrInvokeDlg = (VOID (*) (HANDLE, HWND)) GetProcAddress (hIdleWildDll, "ScrInvokeDlg" );
  192. //
  193. // must be a invalid dll?
  194. //
  195. if (!FInitScrSave || !TermScrSave)
  196. {
  197. FreeLibrary (hIdleWildDll);
  198. return FALSE;
  199. }
  200. //
  201. // init iwlib.dll
  202. //
  203. if (!FInitScrSave (hMainInstance, NULL)) // NULL hwnd???
  204. {
  205. FreeLibrary (hIdleWildDll);
  206. return FALSE;
  207. }
  208. //
  209. // load the screen saver on the command line.
  210. // if the load fails, abort
  211. //
  212. if (!ScrLoadServer (szCmdLine))
  213. {
  214. TermScrSave ();
  215. FreeLibrary (hIdleWildDll);
  216. return FALSE;
  217. }
  218. wmScrSave = RegisterWindowMessage ("SCRSAVE"); // REVIEW: for win 3.1
  219. fIdleWild = TRUE;
  220. return TRUE;
  221. }
  222. BOOL FTermIdleWild (VOID)
  223. {
  224. if (fIdleWild)
  225. {
  226. TermScrSave ();
  227. FreeLibrary (hIdleWildDll);
  228. }
  229. return TRUE;
  230. }
  231. //
  232. // init the default screen saver
  233. //
  234. BOOL FInitDefault (VOID)
  235. {
  236. WNDCLASS cls;
  237. HWND hwnd;
  238. HDC hdc;
  239. RECT rc;
  240. OSVERSIONINFO osvi;
  241. BOOL bWin2000 = FALSE;
  242. cls.style = 0;
  243. cls.lpfnWndProc = DefaultProc;
  244. cls.cbClsExtra = 0;
  245. cls.cbWndExtra = 0;
  246. cls.hInstance = hMainInstance;
  247. cls.hIcon = NULL;
  248. if (hwndPreview == NULL)
  249. {
  250. cls.hCursor = NULL;
  251. }
  252. else
  253. {
  254. cls.hCursor = LoadCursor(NULL,IDC_ARROW);
  255. }
  256. cls.hbrBackground = GetStockObject (BLACK_BRUSH);
  257. cls.lpszMenuName = NULL;
  258. cls.lpszClassName = szAppName;
  259. if (!RegisterClass (&cls))
  260. return FALSE;
  261. //
  262. // Make sure we use the entire virtual desktop size for multiple
  263. // displays
  264. //
  265. hdc = GetDC(NULL);
  266. GetClipBox(hdc, &rc);
  267. ReleaseDC(NULL, hdc);
  268. // On Win2000 Terminal Services we must detect the case where a remotte session
  269. // is on the disconnected desktop, because in this case GetClipBox() returns
  270. // an empty rect.
  271. if (IsRectEmpty(&rc)) {
  272. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  273. if (GetVersionEx (&osvi)){
  274. if ((osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) && (osvi.dwMajorVersion >= 5)) {
  275. bWin2000 = TRUE;
  276. }
  277. }
  278. if (bWin2000 && GetSystemMetrics(SM_REMOTESESSION)) {
  279. rc.left = 0;
  280. rc.top = 0;
  281. rc.right = GetSystemMetrics(SM_CXVIRTUALSCREEN);
  282. rc.bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN);
  283. }
  284. }
  285. hwnd = CreateWindowEx (WS_EX_TOPMOST, szAppName, szAppName,
  286. WS_VISIBLE | ((hwndPreview == NULL) ? WS_POPUP : WS_CHILD),
  287. rc.left,
  288. rc.top,
  289. rc.right - rc.left,
  290. rc.bottom - rc.top,
  291. hwndPreview, NULL,
  292. hMainInstance, NULL);
  293. return hwnd != NULL;
  294. }
  295. LRESULT DefaultProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  296. {
  297. static POINT ptLast;
  298. POINT ptMouse;
  299. switch (msg)
  300. {
  301. case WM_CREATE:
  302. GetCursorPos (&ptLast);
  303. break;
  304. case WM_DESTROY:
  305. PostQuitMessage (0);
  306. break;
  307. case WM_ACTIVATE:
  308. case WM_ACTIVATEAPP:
  309. if (wParam)
  310. break;
  311. case WM_LBUTTONDOWN:
  312. case WM_MBUTTONDOWN:
  313. case WM_RBUTTONDOWN:
  314. case WM_KEYDOWN:
  315. case WM_CHAR:
  316. if (hwndPreview == NULL)
  317. {
  318. PostMessage (hwnd, WM_CLOSE, 0, 0L);
  319. }
  320. break;
  321. case WM_MOUSEMOVE:
  322. if (hwndPreview == NULL)
  323. {
  324. GetCursorPos (&ptMouse);
  325. if (abs (ptMouse.x - ptLast.x) + abs (ptMouse.y - ptLast.y) > THRESHOLD)
  326. PostMessage (hwnd, WM_CLOSE, 0, 0L);
  327. }
  328. break;
  329. case WM_SETCURSOR:
  330. if (hwndPreview == NULL)
  331. {
  332. SetCursor (NULL);
  333. return 0L;
  334. }
  335. break;
  336. }
  337. return DefWindowProc (hwnd, msg, wParam, lParam);
  338. }