- Compile screensavers as UNICODE applications + minor fixes (Part 1/2)
[reactos.git] / reactos / base / applications / screensavers / logon / logon.c
1 /*
2 * Copyright 2003 J Brown
3 * Copyright 2006 Eric Kohl
4 * Copyright 2007 Marc Piulachs (marc.piulachs@codexchange.net)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21 #include <windows.h>
22 #include <tchar.h>
23 #include "resource.h"
24
25 #define RANDOM( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
26
27 #define APPNAME _T("Logon")
28 #define APP_TIMER 1
29 #define APP_TIMER_INTERVAL 2000
30
31 #define BITMAP_HEIGHT 240;
32 #define BITMAP_WIDTH 340
33
34 HINSTANCE hInstance;
35
36 BOOL fullscreen = FALSE;
37
38 void DrawScreen (HDC hdc, HDC hMemDC , RECT rect)
39 {
40 int x;
41 int y;
42 int width = BITMAP_WIDTH;
43 int height = BITMAP_HEIGHT;
44
45 if (!fullscreen)
46 {
47 width = width / 20;
48 height = height / 20;
49 }
50
51 x = RANDOM (0, rect.right - width);
52 y = RANDOM (0, rect.bottom - height);
53
54 BitBlt(
55 hdc,
56 x,
57 y,
58 width,
59 height,
60 hMemDC,
61 0,
62 0,
63 SRCCOPY);
64 }
65
66 HBITMAP GetScreenSaverBitmap (void)
67 {
68 OSVERSIONINFOEX osvi;
69
70 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
71 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
72 GetVersionEx ((OSVERSIONINFO *) &osvi);
73
74 switch(osvi.wProductType)
75 {
76 case VER_NT_WORKSTATION:
77 return LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_WORKSTATION));
78 break;
79 default:
80 return LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SERVER));
81 break;
82 }
83 }
84
85 LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
86 {
87 static POINT ptLast;
88 static POINT ptCursor;
89 static BOOL fFirstTime = TRUE;
90
91 static PAINTSTRUCT ps;
92 static RECT rect;
93 static HDC hDC;
94 static HDC hMemDC;
95 static HBRUSH hBlkBrush;
96 static HBITMAP bitmap;
97
98 switch (msg)
99 {
100 case WM_CREATE:
101 {
102 hDC = GetDC(hwnd);
103 hBlkBrush = (HBRUSH) GetStockObject(BLACK_BRUSH);
104 hMemDC = CreateCompatibleDC(hDC);
105 GetClientRect(hwnd, &rect);
106
107 bitmap = GetScreenSaverBitmap ();
108
109 if(bitmap == NULL)
110 {
111 MessageBox(
112 hwnd,
113 _T("Fatal Error: Could not load bitmap"),
114 _T("Error"),
115 MB_OK | MB_ICONEXCLAMATION);
116 }
117
118 SetTimer (
119 hwnd,
120 APP_TIMER,
121 APP_TIMER_INTERVAL,
122 NULL);
123
124 break;
125 }
126 case WM_PAINT:
127 {
128 hDC = BeginPaint(hwnd, &ps);
129 SelectObject(hMemDC, bitmap);
130 DrawScreen (hDC , hMemDC , rect);
131 EndPaint(hwnd, &ps);
132 break;
133 }
134 case WM_TIMER :
135 {
136 if (wParam == APP_TIMER)
137 {
138 InvalidateRect(hwnd, NULL, 1);
139 }
140 }
141 case WM_ERASEBKGND:
142 {
143 SelectObject(hDC, hBlkBrush);
144
145 PatBlt(
146 hDC,
147 0,
148 0,
149 rect.right,
150 rect.bottom,
151 PATCOPY);
152 break;
153 }
154 case WM_DESTROY:
155 {
156 KillTimer (hwnd, APP_TIMER);
157 DeleteObject(bitmap);
158 ShowCursor(TRUE);
159 PostQuitMessage(0);
160 break;
161 }
162
163 // break out of screen-saver if any keyboard activity
164 case WM_NOTIFY:
165 case WM_SYSKEYDOWN:
166 PostMessage(hwnd, WM_CLOSE, 0, 0);
167 break;
168
169 // break out of screen-saver if any mouse activity
170 case WM_LBUTTONDOWN:
171 case WM_LBUTTONUP:
172 case WM_RBUTTONDOWN:
173 case WM_RBUTTONUP:
174 case WM_MBUTTONDOWN:
175 case WM_MBUTTONUP:
176 case WM_MOUSEMOVE:
177 // If we've got a parent then we must be a preview
178 if(GetParent(hwnd) != 0)
179 return 0;
180
181 if(fFirstTime)
182 {
183 GetCursorPos(&ptLast);
184 fFirstTime = FALSE;
185 }
186
187 GetCursorPos(&ptCursor);
188
189 // if the mouse has moved more than 3 pixels then exit
190 if(abs(ptCursor.x - ptLast.x) >= 3 || abs(ptCursor.y - ptLast.y) >= 3)
191 PostMessage(hwnd, WM_CLOSE, 0, 0);
192
193 ptLast = ptCursor;
194
195 return 0;
196 }
197
198 return DefWindowProc(hwnd, msg, wParam, lParam);
199 }
200
201 void InitSaver(HWND hwndParent)
202 {
203 WNDCLASS wc;
204 ZeroMemory(&wc, sizeof(wc));
205 wc.style = CS_HREDRAW | CS_VREDRAW;
206 wc.lpfnWndProc = WndProc;
207 wc.lpszClassName = APPNAME;
208 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
209 RegisterClass(&wc);
210
211 if (hwndParent != 0)
212 {
213 RECT rect;
214 GetClientRect(hwndParent, &rect);
215 CreateWindow(APPNAME, APPNAME,
216 WS_VISIBLE | WS_CHILD,
217 0, 0,
218 rect.right,
219 rect.bottom,
220 hwndParent, 0,
221 hInstance, NULL);
222 fullscreen = FALSE;
223 }
224 else
225 {
226 HWND hwnd;
227 hwnd = CreateWindowEx(WS_EX_TOPMOST,
228 APPNAME,
229 APPNAME,
230 WS_VISIBLE | WS_POPUP,
231 0, 0,
232 GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
233 HWND_DESKTOP, 0,
234 hInstance, NULL);
235
236 SetWindowPos(hwnd,
237 0, 0, 0, 0, 0,
238 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_SHOWWINDOW);
239
240 ShowCursor(FALSE);
241 fullscreen = TRUE;
242 }
243 }
244
245 VOID ParseCommandLine(LPWSTR szCmdLine, UCHAR *chOption, HWND *hwndParent)
246 {
247 UCHAR ch = *szCmdLine++;
248
249 if(ch == '-' || ch == '/')
250 ch = *szCmdLine++;
251
252 if(ch >= 'A' && ch <= 'Z')
253 ch += 'a' - 'A'; //convert to lower case
254
255 *chOption = ch;
256 ch = *szCmdLine++;
257
258 if(ch == ':')
259 ch = *szCmdLine++;
260
261 while(ch == ' ' || ch == '\t')
262 ch = *szCmdLine++;
263
264 if(isdigit(ch))
265 {
266 unsigned int i = _wtoi(szCmdLine - 1);
267 *hwndParent = (HWND)i;
268 }
269 else
270 *hwndParent = NULL;
271 }
272
273 void Configure(void)
274 {
275 TCHAR szTitle[256];
276 TCHAR szText[256];
277
278 LoadString(hInstance,
279 IDS_TITLE,
280 szTitle,
281 256);
282
283 LoadString(hInstance,
284 IDS_TEXT,
285 szText,
286 256);
287
288 MessageBox(0,
289 szText,
290 szTitle,
291 MB_OK | MB_ICONWARNING);
292 }
293
294 int CALLBACK wWinMain (HINSTANCE hInst,
295 HINSTANCE hPrev,
296 LPWSTR lpCmdLine,
297 int iCmdShow)
298 {
299 HWND hwndParent;
300 UINT nPreviousState;
301 UCHAR chOption;
302 MSG Message;
303
304 hInstance = hInst;
305
306 ParseCommandLine(lpCmdLine, &chOption, &hwndParent);
307
308 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, TRUE, &nPreviousState, 0);
309
310 switch (chOption)
311 {
312 case 's':
313 InitSaver(0);
314 break;
315
316 case 'p':
317 InitSaver(hwndParent);
318 break;
319
320 case 'c':
321 default:
322 Configure();
323 return 0;
324 }
325
326 while (GetMessage(&Message, 0, 0, 0))
327 DispatchMessage(&Message);
328
329 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, FALSE, &nPreviousState, 0);
330
331 return Message.wParam;
332 }