[LT2013]
[reactos.git] / modules / rosapps / applications / notevil / notevil.c
1 /*
2 * notevil.c
3 *
4 * --------------------------------------------------------------------
5 *
6 * This software is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This software 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 GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this software; see the file COPYING.LIB. If
18 * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 * Cambridge, MA 02139, USA.
20 *
21 * --------------------------------------------------------------------
22 * ReactOS Coders Console Parade
23 *
24 * 19990411 EA
25 * 19990515 EA
26 */
27 //#define UNICODE
28 #include <windows.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <tchar.h>
32 #include "resource.h"
33
34 LPCTSTR app_name = _TEXT("notevil");
35
36 HANDLE myself;
37 HANDLE ScreenBuffer;
38 CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo;
39 HANDLE WaitableTimer;
40
41 void
42 WriteStringAt(
43 LPTSTR lpString,
44 COORD xy,
45 WORD wColor
46 )
47 {
48 DWORD cWritten = 0;
49 WORD wLen = lstrlen(lpString);
50
51 if (0 == wLen)
52 return;
53 // don't bother writing text when erasing
54 if( wColor )
55 WriteConsoleOutputCharacter( ScreenBuffer,
56 lpString,
57 wLen,
58 xy,
59 & cWritten
60 );
61 FillConsoleOutputAttribute(
62 ScreenBuffer,
63 wColor,
64 wLen,
65 xy,
66 & cWritten
67 );
68 }
69
70
71 #ifdef DISPLAY_COORD
72 void
73 WriteCoord(COORD c)
74 {
75 COORD xy = {0,0};
76 TCHAR buf [40];
77
78 wsprintf(
79 buf,
80 _TEXT("x=%02d y=%02d"),
81 c.X,
82 c.Y
83 );
84 WriteStringAt(
85 buf,
86 xy,
87 (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
88 );
89 }
90 #endif /* def DISPLAY_COORD */
91
92
93 INT
94 GetNextString(
95 LPTSTR Buffer,
96 INT BufferSize,
97 DWORD *Index
98 )
99 {
100 if (RES_LAST_INDEX == *Index)
101 {
102 *Index = RES_FIRST_INDEX;
103 }
104 else
105 {
106 ++*Index;
107 }
108 LoadString(
109 myself,
110 *Index,
111 Buffer,
112 BufferSize
113 );
114 return 0;
115 }
116
117
118 VOID
119 DisplayTitle(VOID)
120 {
121 LPTSTR szTitle = _TEXT("ReactOS Coders Console Parade");
122 COORD xy;
123
124 xy.X = (ScreenBufferInfo.dwSize.X - lstrlen(szTitle)) / 2;
125 xy.Y = ScreenBufferInfo.dwSize.Y / 2;
126
127 WriteStringAt(
128 szTitle,
129 xy,
130 (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
131 );
132 }
133
134
135 #define RES_DELAY_CHANGE 12
136 #define RES_BUFFER_SIZE 1024
137 void
138 MainLoop(void)
139 {
140 TCHAR NameString [RES_BUFFER_SIZE];
141 DWORD NameIndex = 1;
142 INT NameLength = 0;
143 COORD xy;
144 INT n = RES_DELAY_CHANGE;
145 INT dir_y = 1;
146 INT dir_x = 1;
147 WORD wColor = 1;
148
149 xy.X = ScreenBufferInfo.dwSize.X / 2;
150 xy.Y = ScreenBufferInfo.dwSize.Y / 2;
151
152 for ( ; 1; ++n )
153 {
154 if (n == RES_DELAY_CHANGE)
155 {
156 n = GetNextString(
157 NameString,
158 RES_BUFFER_SIZE,
159 & NameIndex
160 );
161 NameLength = lstrlen(NameString);
162 wColor++;
163 if ((wColor & 0x000F) == 0)
164 wColor = 1;
165 }
166 if (xy.X == 0)
167 {
168 if (dir_x == -1)
169 dir_x = 1;
170 }
171 else if (xy.X >= ScreenBufferInfo.dwSize.X - NameLength - 1)
172 {
173 if (dir_x == 1)
174 dir_x = -1;
175 }
176 xy.X += dir_x;
177
178 if (xy.Y == 0)
179 {
180 if (dir_y == -1)
181 dir_y = 1;
182 }
183 else if (xy.Y >= ScreenBufferInfo.dwSize.Y - 1)
184 {
185 if (dir_y == 1)
186 dir_y = -1;
187 }
188 xy.Y += dir_y;
189 #ifdef DISPLAY_COORD
190 WriteCoord(xy);
191 #endif /* def DISPLAY_COORD */
192 DisplayTitle();
193 WriteStringAt(
194 NameString,
195 xy,
196 wColor
197 );
198 WaitForSingleObject( WaitableTimer, INFINITE );
199 WriteStringAt(
200 NameString,
201 xy,
202 0
203 );
204 }
205 }
206
207
208 int
209 main(
210 int argc,
211 char *argv []
212 )
213 {
214 LARGE_INTEGER lint;
215 DWORD Written;
216 COORD Coord = { 0, 0 };
217
218 myself = GetModuleHandle(NULL);
219
220 GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE),
221 &ScreenBufferInfo);
222 ScreenBufferInfo.dwSize.X = ScreenBufferInfo.srWindow.Right - ScreenBufferInfo.srWindow.Left + 1;
223 ScreenBufferInfo.dwSize.Y = ScreenBufferInfo.srWindow.Bottom - ScreenBufferInfo.srWindow.Top + 1;
224 ScreenBuffer = CreateConsoleScreenBuffer(
225 GENERIC_WRITE,
226 0,
227 NULL,
228 CONSOLE_TEXTMODE_BUFFER,
229 NULL
230 );
231 if (INVALID_HANDLE_VALUE == ScreenBuffer)
232 {
233 _ftprintf(
234 stderr,
235 _TEXT("%s: could not create a new screen buffer\n"),
236 app_name
237 );
238 return EXIT_FAILURE;
239 }
240 // Fill buffer with black background
241 FillConsoleOutputAttribute( ScreenBuffer,
242 0,
243 ScreenBufferInfo.dwSize.X * ScreenBufferInfo.dwSize.Y,
244 Coord,
245 &Written );
246
247 WaitableTimer = CreateWaitableTimer( NULL, FALSE, NULL );
248 if( WaitableTimer == INVALID_HANDLE_VALUE )
249 {
250 printf( "CreateWaitabletimer() failed\n" );
251 return 1;
252 }
253 lint.QuadPart = -2000000;
254 if( SetWaitableTimer( WaitableTimer, &lint, 200, NULL, NULL, FALSE ) == FALSE )
255 {
256 printf( "SetWaitableTimer() failed: 0x%lx\n", GetLastError() );
257 return 2;
258 }
259 SetConsoleActiveScreenBuffer(ScreenBuffer);
260 MainLoop();
261 CloseHandle(ScreenBuffer);
262 return EXIT_SUCCESS;
263 }
264
265
266 /* EOF */