ReactOS  0.4.13-dev-464-g6b95727
scrnsave.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Screen Saver Library
3  * LICENSE: GPL v2 or any later version
4  * FILE: lib/sdk/scrnsave/scrnsave.c
5  * PURPOSE: Library for writing screen savers, compatible with
6  * MS' scrnsave.lib without Win9x support.
7  * PROGRAMMERS: Anders Norlander <anorland@hem2.passagen.se>
8  * Colin Finck <mail@colinfinck.de>
9  */
10 
11 #include <stdarg.h>
12 #include <windef.h>
13 #include <winbase.h>
14 #include <wingdi.h>
15 #include <winuser.h>
16 #include <tchar.h>
17 #include <scrnsave.h>
18 
19 // Screen Saver window class
20 #define CLASS_SCRNSAVE TEXT("WindowsScreenSaverClass")
21 
22 // Globals
33 
34 // Local house keeping
35 static POINT pt_orig;
36 
37 static int ISSPACE(TCHAR c)
38 {
39  return (c == ' ' || c == '\t');
40 }
41 
42 #define ISNUM(c) ((c) >= '0' && (c) <= '9')
43 
44 static ULONG_PTR _toulptr(const TCHAR *s)
45 {
46  ULONG_PTR res;
47  ULONG_PTR n;
48  const TCHAR *p;
49 
50  for (p = s; *p; p++)
51  if (!ISNUM(*p))
52  break;
53 
54  p--;
55  res = 0;
56 
57  for (n = 1; p >= s; p--, n *= 10)
58  res += (*p - '0') * n;
59 
60  return res;
61 }
62 
63 // This function takes care of *must* do tasks, like terminating screen saver
65 {
66  switch(uMsg)
67  {
68  case WM_CREATE:
69  // Mouse is not supposed to move from this position
71  break;
72 
73  case WM_DESTROY:
74  PostQuitMessage(0);
75  break;
76 
77  case WM_SYSCOMMAND:
78  if (!fChildPreview)
79  {
80  switch (wParam)
81  {
82  case SC_CLOSE: // - Closing the screen saver, or...
83  case SC_NEXTWINDOW: // - Switching to
84  case SC_PREVWINDOW: // different windows, or...
85  case SC_SCREENSAVE: // - Starting another screen saver:
86  return FALSE; // Fail it!
87  }
88  }
89  break;
90  }
91 
92  return ScreenSaverProc(hWnd, uMsg, wParam, lParam);
93 }
94 
96 {
97  // Don't do any special processing when in preview mode
98  if (fChildPreview)
99  return DefWindowProc(hWnd, uMsg, wParam, lParam);
100 
101  switch (uMsg)
102  {
103  case WM_NCACTIVATE:
104  case WM_ACTIVATE:
105  case WM_ACTIVATEAPP:
106  if (!wParam)
107  {
108  // wParam is FALSE, so the screen saver is losing the focus.
109  PostMessage(hWnd, WM_CLOSE, 0, 0);
110  }
111  break;
112 
113  case WM_MOUSEMOVE:
114  {
115  POINT pt;
116  GetCursorPos(&pt);
117  // TODO: Implement mouse move threshold. See:
118  // http://svn.reactos.org/svn/reactos/trunk/rosapps/applications/screensavers/starfield/screensaver.c?r1=67455&r2=67454&pathrev=67455
119  if (pt.x == pt_orig.x && pt.y == pt_orig.y)
120  break;
121 
122  // Fall through
123  }
124 
125  case WM_LBUTTONDOWN:
126  case WM_MBUTTONDOWN:
127  case WM_RBUTTONDOWN:
128  case WM_XBUTTONDOWN:
129  case WM_KEYDOWN:
130  case WM_KEYUP:
131  case WM_SYSKEYDOWN:
132  // Send a WM_CLOSE to close the screen saver (allows
133  // the screen saver to perform clean-up tasks)
134  PostMessage(hWnd, WM_CLOSE, 0, 0);
135  break;
136 
137  case WM_SETCURSOR:
138  SetCursor(NULL);
139  return TRUE;
140  }
141 
142  return DefWindowProc(hWnd, uMsg, wParam, lParam);
143 }
144 
145 // Registers the screen saver window class
147 {
148  WNDCLASS cls;
149 
150  cls.hCursor = NULL;
152  cls.lpszMenuName = NULL;
155  cls.hInstance = hMainInstance;
158  cls.cbWndExtra = 0;
159  cls.cbClsExtra = 0;
160 
161  return (RegisterClass(&cls) != 0);
162 }
163 
165 {
166  // Only show the dialog if the RegisterDialogClasses function succeeded.
167  // This is the same behaviour as MS' scrnsave.lib.
169  return -1;
170 
173 }
174 
176 {
177  LPCTSTR lpWindowName;
178  UINT style, exstyle;
179  RECT rc;
180  MSG msg;
181 
183  {
184  MessageBox(NULL, TEXT("RegisterClass() failed"), NULL, MB_ICONHAND);
185  return -1;
186  }
187 
188  // A slightly different approach needs to be used when displaying in a preview window
189  if (hParent)
190  {
192  lpWindowName = TEXT("Preview");
193 
195  exstyle = 0;
196 
197  GetClientRect(hParent, &rc);
198  rc.left = 0;
199  rc.top = 0;
200  }
201  else
202  {
204  lpWindowName = TEXT("Screen Saver");
205 
207  exstyle = WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
208 
209  // Get the left & top side coordinates of the virtual screen
212  // Get the width and height of the virtual screen
215  }
216 
217  // Create the main screen saver window
218  hMainWindow = CreateWindowEx(exstyle, CLASS_SCRNSAVE, lpWindowName, style,
219  rc.left, rc.top, rc.right, rc.bottom,
221  if (!hMainWindow)
222  return -1;
223 
224  // Display window and start pumping messages
226  if (!hParent)
227  SetCursor(NULL);
228 
229  while (GetMessage(&msg, NULL, 0, 0))
230  {
233  }
234 
235  return msg.wParam;
236 }
237 
238 // Screen Saver entry point
239 int APIENTRY _tWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR CmdLine, int nCmdShow)
240 {
241  LPTSTR p;
242 
243  UNREFERENCED_PARAMETER(nCmdShow);
244  UNREFERENCED_PARAMETER(hPrevInst);
245 
247 
248  // Parse the arguments:
249  // -a <hwnd> (Change the password; only for Win9x, unused on WinNT)
250  // -s (Run the screensaver)
251  // -p <hwnd> (Preview)
252  // -c <hwnd> (Configure)
253  for (p = CmdLine; *p; p++)
254  {
255  switch (*p)
256  {
257  case 'S':
258  case 's':
259  // Start the screen saver
260  return LaunchScreenSaver(NULL);
261 
262  case 'P':
263  case 'p':
264  {
265  HWND hParent;
266 
267  while (ISSPACE(*++p));
268  hParent = (HWND)_toulptr(p);
269 
270  // Start the screen saver in preview mode
271  if (hParent && IsWindow(hParent))
272  return LaunchScreenSaver(hParent);
273  else
274  return -1;
275  }
276 
277  case 'C':
278  case 'c':
279  {
280  HWND hParent;
281 
282  if (p[1] == ':')
283  hParent = (HWND)_toulptr(p + 2);
284  else
286 
287  // Display the configuration dialog
288  if (hParent && IsWindow(hParent))
289  return LaunchConfig(hParent);
290  else
291  return -1;
292  }
293 
294  case '-':
295  case '/':
296  case ' ':
297  default:
298  break;
299  }
300  }
301 
302  return LaunchConfig(NULL);
303 }
HGDIOBJ WINAPI GetStockObject(_In_ int)
#define WS_CLIPSIBLINGS
Definition: pedump.c:618
#define MB_ICONHAND
Definition: winuser.h:782
static int LaunchScreenSaver(HWND hParent)
Definition: scrnsave.c:175
#define MAKEINTRESOURCE
Definition: winuser.h:591
char CmdLine[0x100]
Definition: mach.c:35
#define DispatchMessage
Definition: winuser.h:5631
BOOL WINAPI TranslateMessage(_In_ const MSG *)
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
long y
Definition: polytest.cpp:48
#define SM_XVIRTUALSCREEN
Definition: winuser.h:1026
static ULONG_PTR _toulptr(const TCHAR *s)
Definition: scrnsave.c:44
#define WM_LBUTTONDOWN
Definition: winuser.h:1752
BOOL WINAPI IsWindow(_In_opt_ HWND)
long x
Definition: polytest.cpp:48
#define SM_CXVIRTUALSCREEN
Definition: winuser.h:1028
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
#define pt(x, y)
Definition: drawing.c:79
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
HWND hMainWindow
Definition: scrnsave.c:23
#define SC_SCREENSAVE
Definition: mmsystem.h:933
HWND hWnd
Definition: settings.c:17
HINSTANCE hMainInstance
Definition: scrnsave.c:25
GLdouble n
Definition: glext.h:7729
#define APPNAMEBUFFERLEN
Definition: scrnsave.h:38
LONG top
Definition: windef.h:292
HANDLE HWND
Definition: compat.h:13
int cbClsExtra
Definition: winuser.h:3118
#define CS_HREDRAW
Definition: winuser.h:648
LPCSTR lpszMenuName
Definition: winuser.h:3124
UINT MyHelpMessage
Definition: scrnsave.c:32
UINT_PTR WPARAM
Definition: windef.h:207
static LRESULT WINAPI SysScreenSaverProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:64
HBRUSH hbrBackground
Definition: winuser.h:3123
#define WS_CHILD
Definition: pedump.c:617
TCHAR szName[TITLEBARNAMELEN]
Definition: scrnsave.c:26
LONG left
Definition: windef.h:291
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
#define WS_CLIPCHILDREN
Definition: pedump.c:619
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2635
LONG right
Definition: windef.h:293
#define WS_EX_TOOLWINDOW
Definition: winuser.h:404
WPARAM wParam
Definition: combotst.c:138
CHAR * LPTSTR
Definition: xmlstorage.h:192
BOOL fChildPreview
Definition: scrnsave.c:24
int APIENTRY _tWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR CmdLine, int nCmdShow)
Definition: scrnsave.c:239
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define DialogBox
Definition: winuser.h:5627
#define ID_APP
Definition: scrnsave.h:33
int cbWndExtra
Definition: winuser.h:3119
#define WM_NCACTIVATE
Definition: winuser.h:1670
unsigned int BOOL
Definition: ntddk_ex.h:94
#define WM_SETCURSOR
Definition: winuser.h:1618
HCURSOR hCursor
Definition: winuser.h:3122
#define CreateWindowEx
Definition: winuser.h:5621
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:29
static int ISSPACE(TCHAR c)
Definition: scrnsave.c:37
static POINT pt_orig
Definition: scrnsave.c:35
smooth NULL
Definition: ftsmooth.c:416
TCHAR szScreenSaver[22]
Definition: scrnsave.c:29
#define WM_KEYDOWN
Definition: winuser.h:1691
LONG_PTR LPARAM
Definition: windef.h:208
#define DLG_SCRNSAVECONFIGURE
Definition: scrnsave.h:30
#define SW_SHOW
Definition: winuser.h:769
#define SM_CYVIRTUALSCREEN
Definition: winuser.h:1029
#define DefWindowProc
Definition: ros2win.h:31
#define WM_DESTROY
Definition: winuser.h:1591
#define WM_RBUTTONDOWN
Definition: winuser.h:1755
#define PostMessage
Definition: winuser.h:5698
#define BLACK_BRUSH
Definition: wingdi.h:895
#define MAXFILELEN
Definition: scrnsave.h:36
#define CS_VREDRAW
Definition: winuser.h:653
char TCHAR
Definition: xmlstorage.h:189
TCHAR szNoHelpMemory[BUFFLEN]
Definition: scrnsave.c:31
#define WM_CLOSE
Definition: winuser.h:1603
#define WINAPI
Definition: msvc.h:8
const GLubyte * c
Definition: glext.h:8905
#define WM_KEYUP
Definition: winuser.h:1692
int WINAPI GetSystemMetrics(_In_ int)
#define SC_PREVWINDOW
Definition: winuser.h:2545
#define CS_SAVEBITS
Definition: winuser.h:652
#define CLASS_SCRNSAVE
Definition: scrnsave.c:20
LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:24
const DOCKBAR PVOID HWND hParent
Definition: tooldock.h:22
#define WS_EX_TOPMOST
Definition: pedump.c:647
GLdouble s
Definition: gl.h:2039
#define MessageBox
Definition: winuser.h:5688
TCHAR szHelpFile[MAXFILELEN]
Definition: scrnsave.c:30
#define CS_DBLCLKS
Definition: winuser.h:646
LPCSTR lpszClassName
Definition: winuser.h:3125
INT_PTR(CALLBACK * DLGPROC)(HWND, UINT, WPARAM, LPARAM)
Definition: winuser.h:2849
#define SM_YVIRTUALSCREEN
Definition: winuser.h:1027
UINT style
Definition: winuser.h:3116
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
HINSTANCE hInst
Definition: dxdiag.c:13
#define TEXT(s)
Definition: k32.h:26
#define WM_ACTIVATEAPP
Definition: winuser.h:1614
#define WM_ACTIVATE
Definition: winuser.h:1594
HINSTANCE hInstance
Definition: winuser.h:3120
HCURSOR WINAPI SetCursor(_In_opt_ HCURSOR)
#define WM_MBUTTONDOWN
Definition: winuser.h:1758
#define WS_POPUP
Definition: pedump.c:616
unsigned int UINT
Definition: ndis.h:50
#define WM_MOUSEMOVE
Definition: winuser.h:1751
HWND WINAPI GetForegroundWindow(void)
Definition: ntwrapper.h:392
#define GetMessage
Definition: winuser.h:5656
static BOOL RegisterScreenSaverClass(void)
Definition: scrnsave.c:146
WNDPROC lpfnWndProc
Definition: winuser.h:3117
#define msg(x)
Definition: auth_time.c:54
#define SC_CLOSE
Definition: winuser.h:2546
#define BUFFLEN
Definition: scrnsave.h:39
#define WM_CREATE
Definition: winuser.h:1590
TCHAR szAppName[APPNAMEBUFFERLEN]
Definition: scrnsave.c:27
GLuint res
Definition: glext.h:9613
#define WM_SYSKEYDOWN
Definition: winuser.h:1695
LONG bottom
Definition: windef.h:294
#define ISNUM(c)
Definition: scrnsave.c:42
#define RegisterClass
Definition: winuser.h:5702
#define SC_NEXTWINDOW
Definition: winuser.h:2544
#define LoadIcon
Definition: winuser.h:5679
GLfloat GLfloat p
Definition: glext.h:8902
#define TITLEBARNAMELEN
Definition: scrnsave.h:37
LONG_PTR LRESULT
Definition: windef.h:209
static int LaunchConfig(HWND hParent)
Definition: scrnsave.c:164
LRESULT WINAPI DefScreenSaverProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:95
Arabic default style
Definition: afstyles.h:93
#define WS_VISIBLE
Definition: pedump.c:620
#define WM_SYSCOMMAND
Definition: winuser.h:1717
LPARAM lParam
Definition: combotst.c:139
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
#define CS_PARENTDC
Definition: winuser.h:651
#define MAKEINTATOM(i)
Definition: winbase.h:1416
TCHAR szIniFile[MAXFILELEN]
Definition: scrnsave.c:28
#define APIENTRY
Definition: api.h:79
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
Definition: scrnsave.c:36
HICON hIcon
Definition: winuser.h:3121