304f741153748239cf2043323c20fa60db43e3aa
[reactos.git] / rosapps / applications / screensavers / mazescr / scrnsave.c
1 /*
2 * Copyright 2003 J Brown
3 * Copyright 2006 Eric Kohl
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 */
19
20 #include <windows.h>
21 #include <tchar.h>
22 #include "resource.h"
23
24 #define APPNAME _T("Scrnsave")
25
26 LRESULT CALLBACK MazeWndProc(
27 HWND hWnd, // window handle
28 UINT message, // type of message
29 WPARAM wParam, // additional information
30 LPARAM lParam); // additional information
31
32 int APIENTRY MazeMain(
33 HINSTANCE hInstance,
34 HINSTANCE hPrevInstance,
35 LPSTR lpCmdLine,
36 HWND hParent);
37
38 HINSTANCE hInstance;
39
40 BOOL fullscreen = FALSE;
41
42
43 LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
44 {
45 static POINT ptLast;
46 static POINT ptCursor;
47 static BOOL fFirstTime = TRUE;
48
49 switch (msg)
50 {
51 case WM_DESTROY:
52 if (fullscreen)
53 ShowCursor(TRUE);
54 PostQuitMessage(0);
55 break;
56
57 // break out of screen-saver if any keyboard activity
58 case WM_NOTIFY:
59 case WM_SYSKEYDOWN:
60 PostMessage(hwnd, WM_CLOSE, 0, 0);
61 break;
62
63 // break out of screen-saver if any mouse activity
64 case WM_LBUTTONDOWN:
65 case WM_LBUTTONUP:
66 case WM_RBUTTONDOWN:
67 case WM_RBUTTONUP:
68 case WM_MBUTTONDOWN:
69 case WM_MBUTTONUP:
70 case WM_MOUSEMOVE:
71 // If we've got a parent then we must be a preview
72 if(GetParent(hwnd) != 0)
73 return 0;
74
75 if(fFirstTime)
76 {
77 GetCursorPos(&ptLast);
78 fFirstTime = FALSE;
79 }
80
81 GetCursorPos(&ptCursor);
82
83 // if the mouse has moved more than 3 pixels then exit
84 if(abs(ptCursor.x - ptLast.x) >= 3 || abs(ptCursor.y - ptLast.y) >= 3)
85 PostMessage(hwnd, WM_CLOSE, 0, 0);
86
87 ptLast = ptCursor;
88
89 return 0;
90 }
91
92 return MazeWndProc(hwnd, msg, wParam, lParam);
93 }
94
95 HWND InitSaver(HWND hwndParent)
96 {
97 WNDCLASS wc;
98 HWND hwnd;
99 ZeroMemory(&wc, sizeof(wc));
100 wc.style = CS_HREDRAW | CS_VREDRAW;
101 wc.lpfnWndProc = WndProc;
102 wc.lpszClassName = APPNAME;
103 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
104 RegisterClass(&wc);
105
106 if (hwndParent != 0)
107 {
108 RECT rect;
109 GetClientRect(hwndParent, &rect);
110 hwnd = CreateWindow(APPNAME, APPNAME,
111 WS_VISIBLE | WS_CHILD,
112 0, 0,
113 rect.right,
114 rect.bottom,
115 hwndParent, 0,
116 hInstance, NULL);
117 fullscreen = FALSE;
118 }
119 else
120 {
121 hwnd = CreateWindowEx(WS_EX_TOPMOST,
122 APPNAME,
123 APPNAME,
124 WS_VISIBLE | WS_POPUP,
125 0,
126 0,
127 GetSystemMetrics(SM_CXSCREEN),
128 GetSystemMetrics(SM_CYSCREEN),
129 HWND_DESKTOP,
130 0,
131 hInstance,
132 NULL);
133
134 SetWindowPos(hwnd,
135 0,
136 0,
137 0,
138 0,
139 0,
140 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE | SWP_SHOWWINDOW);
141
142 ShowCursor(FALSE);
143 fullscreen = TRUE;
144 }
145 return hwnd;
146 }
147
148 void ParseCommandLine(PSTR szCmdLine, int *chOption, HWND *hwndParent)
149 {
150 int ch = *szCmdLine++;
151
152 if(ch == '-' || ch == '/')
153 ch = *szCmdLine++;
154
155 if(ch >= 'A' && ch <= 'Z')
156 ch += 'a' - 'A';
157
158 *chOption = ch;
159 ch = *szCmdLine++;
160
161 if(ch == ':')
162 ch = *szCmdLine++;
163
164 while(ch == ' ' || ch == '\t')
165 ch = *szCmdLine++;
166
167 if(isdigit(ch))
168 {
169 unsigned int i = atoi(szCmdLine - 1);
170 *hwndParent = (HWND)i;
171 }
172 else
173 *hwndParent = 0;
174 }
175
176 void Configure(void)
177 {
178 TCHAR szTitle[256];
179 TCHAR szText[256];
180
181 LoadString(hInstance,
182 IDS_TITLE,
183 szTitle,
184 256);
185
186 LoadString(hInstance,
187 IDS_TEXT,
188 szText,
189 256);
190
191 MessageBox(0,
192 szText,
193 szTitle,
194 MB_OK | MB_ICONWARNING);
195 }
196
197 int WINAPI WinMain (HINSTANCE hInst,
198 HINSTANCE hPrev,
199 LPSTR lpCmdLine,
200 int iCmdShow)
201 {
202 HWND hwndParent;
203 HWND hwndChild;
204 UINT nPreviousState;
205 int chOption;
206
207 hInstance = hInst;
208
209 ParseCommandLine(lpCmdLine, &chOption, &hwndParent);
210
211 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, TRUE, &nPreviousState, 0);
212
213 switch (chOption)
214 {
215 case 's':
216 hwndChild = InitSaver(0);
217 break;
218
219 case 'p':
220 hwndChild = InitSaver(hwndParent);
221 break;
222
223 case 'c':
224 default:
225 Configure();
226 return 0;
227 }
228
229 MazeMain(hInst, hPrev, lpCmdLine, hwndChild);
230
231 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, FALSE, &nPreviousState, 0);
232
233 return 0;
234 }