ReactOS 0.4.15-dev-8241-g63935f8
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 <stdlib.h>
18#include <scrnsave.h>
19
20// Screen Saver window class
21#define CLASS_SCRNSAVE TEXT("WindowsScreenSaverClass")
22
23// Globals
34
35// Local house keeping
38
39static int ISSPACE(TCHAR c)
40{
41 return (c == ' ' || c == '\t');
42}
43
44#define ISNUM(c) ((c) >= '0' && (c) <= '9')
45
46static ULONG_PTR _toulptr(const TCHAR *s)
47{
50 const TCHAR *p;
51
52 for (p = s; *p; p++)
53 if (!ISNUM(*p))
54 break;
55
56 p--;
57 res = 0;
58
59 for (n = 1; p >= s; p--, n *= 10)
60 res += (*p - '0') * n;
61
62 return res;
63}
64
65// This function takes care of *must* do tasks, like terminating screen saver
67{
68 switch(uMsg)
69 {
70 case WM_DESTROY:
72 break;
73
74 case WM_SYSCOMMAND:
75 if (!fChildPreview)
76 {
77 switch (wParam)
78 {
79 case SC_CLOSE: // - Closing the screen saver, or...
80 case SC_NEXTWINDOW: // - Switching to
81 case SC_PREVWINDOW: // different windows, or...
82 case SC_SCREENSAVE: // - Starting another screen saver:
83 return FALSE; // Fail it!
84 }
85 }
86 break;
87 }
88
89 return ScreenSaverProc(hWnd, uMsg, wParam, lParam);
90}
91
93{
94 // Don't do any special processing when in preview mode
95 if (fChildPreview)
96 return DefWindowProc(hWnd, uMsg, wParam, lParam);
97
98 switch (uMsg)
99 {
100 case WM_NCACTIVATE:
101 case WM_ACTIVATE:
102 case WM_ACTIVATEAPP:
103 if (!wParam)
104 {
105 // wParam is FALSE, so the screen saver is losing the focus.
106 PostMessage(hWnd, WM_CLOSE, 0, 0);
107 }
108 break;
109
110 case WM_MOUSEMOVE:
111 {
112 POINT pt;
114
115 if (!pt_init)
116 {
117 pt_orig = pt;
118 pt_init = TRUE;
119 break;
120 }
121
122 if (abs(pt.x-pt_orig.x) > 10 || abs(pt.y-pt_orig.y) > 10)
123 PostMessage(hWnd, WM_CLOSE, 0, 0);
124 break;
125 }
126
127 case WM_LBUTTONDOWN:
128 case WM_MBUTTONDOWN:
129 case WM_RBUTTONDOWN:
130 case WM_XBUTTONDOWN:
131 case WM_KEYDOWN:
132 case WM_KEYUP:
133 case WM_SYSKEYDOWN:
134 // Send a WM_CLOSE to close the screen saver (allows
135 // the screen saver to perform clean-up tasks)
136 PostMessage(hWnd, WM_CLOSE, 0, 0);
137 break;
138
139 case WM_SETCURSOR:
141 return TRUE;
142 }
143
144 return DefWindowProc(hWnd, uMsg, wParam, lParam);
145}
146
147// Registers the screen saver window class
149{
150 WNDCLASS cls;
151
152 cls.hCursor = NULL;
154 cls.lpszMenuName = NULL;
160 cls.cbWndExtra = 0;
161 cls.cbClsExtra = 0;
162
163 return (RegisterClass(&cls) != 0);
164}
165
166static int LaunchConfig(HWND hParent)
167{
168 // Only show the dialog if the RegisterDialogClasses function succeeded.
169 // This is the same behaviour as MS' scrnsave.lib.
171 return -1;
172
175}
176
177static int LaunchScreenSaver(HWND hParent)
178{
179 LPCTSTR lpWindowName;
180 UINT style, exstyle;
181 RECT rc;
182 MSG msg;
183
185 {
186 MessageBox(NULL, TEXT("RegisterClass() failed"), NULL, MB_ICONHAND);
187 return -1;
188 }
189
190 // A slightly different approach needs to be used when displaying in a preview window
191 if (hParent)
192 {
194 lpWindowName = TEXT("Preview");
195
197 exstyle = 0;
198
199 GetClientRect(hParent, &rc);
200 rc.left = 0;
201 rc.top = 0;
202 }
203 else
204 {
206 lpWindowName = TEXT("Screen Saver");
207
210
211 // Get the left & top side coordinates of the virtual screen
214 // Get the width and height of the virtual screen
217 }
218
219 // Create the main screen saver window
220 hMainWindow = CreateWindowEx(exstyle, CLASS_SCRNSAVE, lpWindowName, style,
221 rc.left, rc.top, rc.right, rc.bottom,
222 hParent, NULL, hMainInstance, NULL);
223 if (!hMainWindow)
224 return -1;
225
226 // Display window and start pumping messages
228 if (!hParent)
230
231 while (GetMessage(&msg, NULL, 0, 0))
232 {
235 }
236
237 return msg.wParam;
238}
239
240// Screen Saver entry point
242{
243 LPTSTR p;
244
245 UNREFERENCED_PARAMETER(nCmdShow);
246 UNREFERENCED_PARAMETER(hPrevInst);
247
249
250 // Parse the arguments:
251 // -a <hwnd> (Change the password; only for Win9x, unused on WinNT)
252 // -s (Run the screensaver)
253 // -p <hwnd> (Preview)
254 // -c <hwnd> (Configure)
255 for (p = CmdLine; *p; p++)
256 {
257 switch (*p)
258 {
259 case 'S':
260 case 's':
261 // Start the screen saver
262 return LaunchScreenSaver(NULL);
263
264 case 'P':
265 case 'p':
266 {
267 HWND hParent;
268
269 while (ISSPACE(*++p));
270 hParent = (HWND)_toulptr(p);
271
272 // Start the screen saver in preview mode
273 if (hParent && IsWindow(hParent))
274 return LaunchScreenSaver(hParent);
275 else
276 return -1;
277 }
278
279 case 'C':
280 case 'c':
281 {
282 HWND hParent;
283
284 if (p[1] == ':')
285 hParent = (HWND)_toulptr(p + 2);
286 else
287 hParent = GetForegroundWindow();
288
289 // Display the configuration dialog
290 if (hParent && IsWindow(hParent))
291 return LaunchConfig(hParent);
292 else
293 return -1;
294 }
295
296 case '-':
297 case '/':
298 case ' ':
299 default:
300 break;
301 }
302 }
303
304 return LaunchConfig(NULL);
305}
Arabic default style
Definition: afstyles.h:94
#define msg(x)
Definition: auth_time.c:54
HWND hWnd
Definition: settings.c:17
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define DLGPROC
Definition: maze.c:62
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define APIENTRY
Definition: api.h:79
HANDLE HWND
Definition: compat.h:19
static const WCHAR CmdLine[]
Definition: install.c:48
#define pt(x, y)
Definition: drawing.c:79
HINSTANCE hInst
Definition: dxdiag.c:13
#define abs(i)
Definition: fconv.c:206
unsigned int BOOL
Definition: ntddk_ex.h:94
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
GLuint res
Definition: glext.h:9613
const GLubyte * c
Definition: glext.h:8905
GLfloat GLfloat p
Definition: glext.h:8902
#define _tWinMain
Definition: tchar.h:498
#define TEXT(s)
Definition: k32.h:26
#define SC_SCREENSAVE
Definition: mmsystem.h:933
LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:24
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:29
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
Definition: scrnsave.c:36
unsigned int UINT
Definition: ndis.h:50
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define WS_CHILD
Definition: pedump.c:617
#define WS_POPUP
Definition: pedump.c:616
#define WS_VISIBLE
Definition: pedump.c:620
#define WS_EX_TOPMOST
Definition: pedump.c:647
#define WS_CLIPSIBLINGS
Definition: pedump.c:618
#define WS_CLIPCHILDREN
Definition: pedump.c:619
#define DefWindowProc
Definition: ros2win.h:31
#define ID_APP
Definition: scrnsave.h:33
#define BUFFLEN
Definition: scrnsave.h:39
LRESULT WINAPI DefScreenSaverProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:92
#define APPNAMEBUFFERLEN
Definition: scrnsave.h:38
#define MAXFILELEN
Definition: scrnsave.h:36
#define TITLEBARNAMELEN
Definition: scrnsave.h:37
#define DLG_SCRNSAVECONFIGURE
Definition: scrnsave.h:30
static POINT pt_orig
Definition: scrnsave.c:36
static int ISSPACE(TCHAR c)
Definition: scrnsave.c:39
TCHAR szName[TITLEBARNAMELEN]
Definition: scrnsave.c:27
static int LaunchConfig(HWND hParent)
Definition: scrnsave.c:166
#define CLASS_SCRNSAVE
Definition: scrnsave.c:21
HWND hMainWindow
Definition: scrnsave.c:24
TCHAR szAppName[APPNAMEBUFFERLEN]
Definition: scrnsave.c:28
TCHAR szIniFile[MAXFILELEN]
Definition: scrnsave.c:29
TCHAR szHelpFile[MAXFILELEN]
Definition: scrnsave.c:31
TCHAR szScreenSaver[22]
Definition: scrnsave.c:30
HINSTANCE hMainInstance
Definition: scrnsave.c:26
static ULONG_PTR _toulptr(const TCHAR *s)
Definition: scrnsave.c:46
#define ISNUM(c)
Definition: scrnsave.c:44
BOOL fChildPreview
Definition: scrnsave.c:25
TCHAR szNoHelpMemory[BUFFLEN]
Definition: scrnsave.c:32
UINT MyHelpMessage
Definition: scrnsave.c:33
static BOOL pt_init
Definition: scrnsave.c:37
static int LaunchScreenSaver(HWND hParent)
Definition: scrnsave.c:177
static LRESULT WINAPI SysScreenSaverProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: scrnsave.c:66
static BOOL RegisterScreenSaverClass(void)
Definition: scrnsave.c:148
HBRUSH hbrBackground
Definition: winuser.h:3170
HICON hIcon
Definition: winuser.h:3168
HINSTANCE hInstance
Definition: winuser.h:3167
HCURSOR hCursor
Definition: winuser.h:3169
int cbWndExtra
Definition: winuser.h:3166
UINT style
Definition: winuser.h:3163
LPCSTR lpszMenuName
Definition: winuser.h:3171
LPCSTR lpszClassName
Definition: winuser.h:3172
WNDPROC lpfnWndProc
Definition: winuser.h:3164
int cbClsExtra
Definition: winuser.h:3165
long y
Definition: polytest.cpp:48
long x
Definition: polytest.cpp:48
LONG right
Definition: windef.h:308
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1829
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define MAKEINTATOM(i)
Definition: winbase.h:1463
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
#define WINAPI
Definition: msvc.h:6
HGDIOBJ WINAPI GetStockObject(_In_ int)
#define BLACK_BRUSH
Definition: wingdi.h:896
#define CS_VREDRAW
Definition: winuser.h:658
#define CreateWindowEx
Definition: winuser.h:5764
BOOL WINAPI IsWindow(_In_opt_ HWND)
#define WM_CLOSE
Definition: winuser.h:1621
#define SM_CYVIRTUALSCREEN
Definition: winuser.h:1039
#define WM_SYSCOMMAND
Definition: winuser.h:1741
BOOL WINAPI TranslateMessage(_In_ const MSG *)
#define WM_KEYUP
Definition: winuser.h:1716
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
HWND WINAPI GetForegroundWindow(void)
Definition: ntwrapper.h:392
#define MB_ICONHAND
Definition: winuser.h:788
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
#define SC_PREVWINDOW
Definition: winuser.h:2591
#define CS_HREDRAW
Definition: winuser.h:653
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2701
#define WS_EX_TOOLWINDOW
Definition: winuser.h:404
HCURSOR WINAPI SetCursor(_In_opt_ HCURSOR)
#define WM_MOUSEMOVE
Definition: winuser.h:1775
#define CS_DBLCLKS
Definition: winuser.h:651
#define WM_LBUTTONDOWN
Definition: winuser.h:1776
#define WM_ACTIVATE
Definition: winuser.h:1612
#define WM_RBUTTONDOWN
Definition: winuser.h:1779
#define SM_CXVIRTUALSCREEN
Definition: winuser.h:1038
#define SC_NEXTWINDOW
Definition: winuser.h:2590
#define GetMessage
Definition: winuser.h:5799
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
#define WM_NCACTIVATE
Definition: winuser.h:1688
#define LoadIcon
Definition: winuser.h:5822
#define SC_CLOSE
Definition: winuser.h:2592
#define PostMessage
Definition: winuser.h:5841
#define CS_SAVEBITS
Definition: winuser.h:657
#define MessageBox
Definition: winuser.h:5831
#define WM_SETCURSOR
Definition: winuser.h:1636
#define WM_ACTIVATEAPP
Definition: winuser.h:1632
#define SW_SHOW
Definition: winuser.h:775
#define RegisterClass
Definition: winuser.h:5845
#define WM_DESTROY
Definition: winuser.h:1609
#define WM_KEYDOWN
Definition: winuser.h:1715
#define DispatchMessage
Definition: winuser.h:5774
#define SM_XVIRTUALSCREEN
Definition: winuser.h:1036
#define CS_PARENTDC
Definition: winuser.h:656
#define MAKEINTRESOURCE
Definition: winuser.h:591
int WINAPI GetSystemMetrics(_In_ int)
#define WM_SYSKEYDOWN
Definition: winuser.h:1719
#define WM_MBUTTONDOWN
Definition: winuser.h:1782
#define SM_YVIRTUALSCREEN
Definition: winuser.h:1037
#define DialogBox
Definition: winuser.h:5770
char TCHAR
Definition: xmlstorage.h:189
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
CHAR * LPTSTR
Definition: xmlstorage.h:192