Partly applied patch from bug 2874 by Vytis Girdzijauskas "CMan" cman<at>cman<dot>us
[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 PostQuitMessage(0);
53 break;
54
55 // break out of screen-saver if any keyboard activity
56 case WM_NOTIFY:
57 case WM_SYSKEYDOWN:
58 PostMessage(hwnd, WM_CLOSE, 0, 0);
59 break;
60
61 // break out of screen-saver if any mouse activity
62 case WM_LBUTTONDOWN:
63 case WM_LBUTTONUP:
64 case WM_RBUTTONDOWN:
65 case WM_RBUTTONUP:
66 case WM_MBUTTONDOWN:
67 case WM_MBUTTONUP:
68 case WM_MOUSEMOVE:
69 // If we've got a parent then we must be a preview
70 if(GetParent(hwnd) != 0)
71 return 0;
72
73 if(fFirstTime)
74 {
75 GetCursorPos(&ptLast);
76 fFirstTime = FALSE;
77 }
78
79 GetCursorPos(&ptCursor);
80
81 // if the mouse has moved more than 3 pixels then exit
82 if(abs(ptCursor.x - ptLast.x) >= 3 || abs(ptCursor.y - ptLast.y) >= 3)
83 PostMessage(hwnd, WM_CLOSE, 0, 0);
84
85 ptLast = ptCursor;
86
87 return 0;
88 }
89
90 return MazeWndProc(hwnd, msg, wParam, lParam);
91 }
92
93 HWND InitSaver(HWND hwndParent)
94 {
95 WNDCLASS wc;
96 HWND hwnd;
97 ZeroMemory(&wc, sizeof(wc));
98 wc.style = CS_HREDRAW | CS_VREDRAW;
99 wc.lpfnWndProc = WndProc;
100 wc.lpszClassName = APPNAME;
101 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
102 RegisterClass(&wc);
103
104 if (hwndParent != 0)
105 {
106 RECT rect;
107 GetClientRect(hwndParent, &rect);
108 hwnd = CreateWindow(APPNAME, APPNAME,
109 WS_VISIBLE | WS_CHILD,
110 0, 0,
111 rect.right,
112 rect.bottom,
113 hwndParent, 0,
114 hInstance, NULL);
115 fullscreen = FALSE;
116 }
117 else
118 {
119 hwnd = CreateWindow(APPNAME, APPNAME,
120 WS_VISIBLE | WS_POPUP | WS_EX_TOPMOST,
121 0, 0,
122 GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
123 HWND_DESKTOP, 0,
124 hInstance, NULL);
125 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
126 ShowCursor(FALSE);
127 fullscreen = TRUE;
128 }
129 return hwnd;
130 }
131
132 void ParseCommandLine(PSTR szCmdLine, int *chOption, HWND *hwndParent)
133 {
134 int ch = *szCmdLine++;
135
136 if(ch == '-' || ch == '/')
137 ch = *szCmdLine++;
138
139 if(ch >= 'A' && ch <= 'Z')
140 ch += 'a' - 'A';
141
142 *chOption = ch;
143 ch = *szCmdLine++;
144
145 if(ch == ':')
146 ch = *szCmdLine++;
147
148 while(ch == ' ' || ch == '\t')
149 ch = *szCmdLine++;
150
151 if(isdigit(ch))
152 {
153 unsigned int i = atoi(szCmdLine - 1);
154 *hwndParent = (HWND)i;
155 }
156 else
157 *hwndParent = 0;
158 }
159
160 void Configure(void)
161 {
162 TCHAR szTitle[256];
163 TCHAR szText[256];
164
165 LoadString(hInstance,
166 IDS_TITLE,
167 szTitle,
168 256);
169
170 LoadString(hInstance,
171 IDS_TEXT,
172 szText,
173 256);
174
175 MessageBox(0,
176 szText,
177 szTitle,
178 MB_OK | MB_ICONWARNING);
179 }
180
181 int WINAPI WinMain (HINSTANCE hInst,
182 HINSTANCE hPrev,
183 LPSTR lpCmdLine,
184 int iCmdShow)
185 {
186 HWND hwndParent;
187 HWND hwndChild;
188 UINT nPreviousState;
189 int chOption;
190
191 hInstance = hInst;
192
193 ParseCommandLine(lpCmdLine, &chOption, &hwndParent);
194
195 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, TRUE, &nPreviousState, 0);
196
197 switch (chOption)
198 {
199 case 's':
200 hwndChild = InitSaver(0);
201 break;
202
203 case 'p':
204 hwndChild = InitSaver(hwndParent);
205 break;
206
207 case 'c':
208 default:
209 Configure();
210 return 0;
211 }
212
213 MazeMain(hInst, hPrev, lpCmdLine, hwndChild);
214
215 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, FALSE, &nPreviousState, 0);
216
217 return 0;
218 }