drivers/wifi: Remove unnecessary data structure copy
[coreboot2.git] / payloads / libpayload / curses / PDCurses / demos / newdemo.c
blobeefc0c16f12ea1a3d5510976bd0a826e020558e9
1 /*
2 * newdemo.c - A demo program using PDCurses. The program
3 * illustrates the use of colors for text output.
5 * Hacks by jbuhler@cs.washington.edu on 12/29/96
7 * $Id: newdemo.c,v 1.39 2008/07/13 16:08:17 wmcbrine Exp $
8 */
10 #include <stdio.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <curses.h>
14 #include <stdlib.h>
15 #include <time.h>
17 int WaitForUser(void);
18 int SubWinTest(WINDOW *);
19 int BouncingBalls(WINDOW *);
20 void trap(int);
22 /* An ASCII map of Australia */
24 char *AusMap[17] =
26 " A ",
27 " AA AA ",
28 " N.T. AAAAA AAAA ",
29 " AAAAAAAAAAA AAAAAAAA ",
30 " AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
31 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
32 " AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
33 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
34 " AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
35 "W.A. AAAAAAAAA AAAAAA Vic.",
36 " AAA S.A. AA",
37 " A Tas.",
41 /* Funny messages for the scroller */
43 char *messages[] =
45 "Hello from the Land Down Under",
46 "The Land of crocs, and a big Red Rock",
47 "Where the sunflower runs along the highways",
48 "The dusty red roads lead one to loneliness",
49 "Blue sky in the morning and",
50 "Freezing nights and twinkling stars",
51 NULL
54 int WaitForUser(void)
56 chtype ch;
58 nodelay(stdscr, TRUE);
59 halfdelay(50);
61 ch = getch();
63 nodelay(stdscr, FALSE);
64 nocbreak(); /* Reset the halfdelay() value */
65 cbreak();
67 return (ch == '\033') ? ch : 0;
70 int SubWinTest(WINDOW *win)
72 WINDOW *swin1, *swin2, *swin3;
73 int w, h, sw, sh, bx, by;
75 wattrset(win, 0);
76 getmaxyx(win, h, w);
77 getbegyx(win, by, bx);
79 sw = w / 3;
80 sh = h / 3;
82 if ((swin1 = derwin(win, sh, sw, 3, 5)) == NULL)
83 return 1;
84 if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL)
85 return 1;
86 if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL)
87 return 1;
89 init_pair(8, COLOR_RED, COLOR_BLUE);
90 wbkgd(swin1, COLOR_PAIR(8));
91 werase(swin1);
92 mvwaddstr(swin1, 0, 3, "Sub-window 1");
93 wrefresh(swin1);
95 init_pair(9, COLOR_CYAN, COLOR_MAGENTA);
96 wbkgd(swin2, COLOR_PAIR(9));
97 werase(swin2);
98 mvwaddstr(swin2, 0, 3, "Sub-window 2");
99 wrefresh(swin2);
101 init_pair(10, COLOR_YELLOW, COLOR_GREEN);
102 wbkgd(swin3, COLOR_PAIR(10));
103 werase(swin3);
104 mvwaddstr(swin3, 0, 3, "Sub-window 3");
105 wrefresh(swin3);
107 delwin(swin1);
108 delwin(swin2);
109 delwin(swin3);
110 WaitForUser();
112 return 0;
115 int BouncingBalls(WINDOW *win)
117 chtype c1, c2, c3, ball1, ball2, ball3;
118 int w, h, x1, y1, xd1, yd1, x2, y2, xd2, yd2, x3, y3, xd3, yd3, c;
120 curs_set(0);
122 wbkgd(win, COLOR_PAIR(1));
123 wrefresh(win);
124 wattrset(win, 0);
126 init_pair(11, COLOR_RED, COLOR_GREEN);
127 init_pair(12, COLOR_BLUE, COLOR_RED);
128 init_pair(13, COLOR_YELLOW, COLOR_WHITE);
130 ball1 = 'O' | COLOR_PAIR(11);
131 ball2 = '*' | COLOR_PAIR(12);
132 ball3 = '@' | COLOR_PAIR(13);
134 getmaxyx(win, h, w);
136 x1 = 2 + rand() % (w - 4);
137 y1 = 2 + rand() % (h - 4);
138 x2 = 2 + rand() % (w - 4);
139 y2 = 2 + rand() % (h - 4);
140 x3 = 2 + rand() % (w - 4);
141 y3 = 2 + rand() % (h - 4);
143 xd1 = 1;
144 yd1 = 1;
145 xd2 = 1;
146 yd2 = -1;
147 xd3 = -1;
148 yd3 = 1;
150 nodelay(stdscr, TRUE);
152 while ((c = getch()) == ERR)
154 x1 += xd1;
155 if (x1 <= 1 || x1 >= w - 2)
156 xd1 *= -1;
158 y1 += yd1;
159 if (y1 <= 1 || y1 >= h - 2)
160 yd1 *= -1;
162 x2 += xd2;
163 if (x2 <= 1 || x2 >= w - 2)
164 xd2 *= -1;
166 y2 += yd2;
167 if (y2 <= 1 || y2 >= h - 2)
168 yd2 *= -1;
170 x3 += xd3;
171 if (x3 <= 1 || x3 >= w - 2)
172 xd3 *= -1;
174 y3 += yd3;
175 if (y3 <= 1 || y3 >= h - 2)
176 yd3 *= -1;
178 c1 = mvwinch(win, y1, x1);
179 c2 = mvwinch(win, y2, x2);
180 c3 = mvwinch(win, y3, x3);
182 mvwaddch(win, y1, x1, ball1);
183 mvwaddch(win, y2, x2, ball2);
184 mvwaddch(win, y3, x3, ball3);
186 wmove(win, 0, 0);
187 wrefresh(win);
189 mvwaddch(win, y1, x1, c1);
190 mvwaddch(win, y2, x2, c2);
191 mvwaddch(win, y3, x3, c3);
193 napms(150);
196 nodelay(stdscr, FALSE);
197 ungetch(c);
198 return 0;
201 /* Trap interrupt */
203 void trap(int sig)
205 if (sig == SIGINT)
207 endwin();
209 exit(0);
213 int main(int argc, char **argv)
215 WINDOW *win;
216 chtype save[80], ch;
217 int width, height, w, x, y, i, j, seed;
219 #ifdef XCURSES
220 Xinitscr(argc, argv);
221 #else
222 initscr();
223 #endif
224 seed = time((time_t *)0);
225 srand(seed);
227 start_color();
228 # if defined(NCURSES_VERSION) || (defined(PDC_BUILD) && PDC_BUILD > 3000)
229 use_default_colors();
230 # endif
231 cbreak();
232 noecho();
234 curs_set(0);
236 #if !defined(__TURBOC__) && !defined(OS2)
237 signal(SIGINT, trap);
238 #endif
239 noecho();
241 /* refresh stdscr so that reading from it will not cause it to
242 overwrite the other windows that are being created */
244 refresh();
246 /* Create a drawing window */
248 width = 48;
249 height = 15;
251 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
253 if (win == NULL)
255 endwin();
257 return 1;
260 for (;;)
262 init_pair(1, COLOR_WHITE, COLOR_BLUE);
263 wbkgd(win, COLOR_PAIR(1));
264 werase(win);
266 init_pair(2, COLOR_RED, COLOR_RED);
267 wattrset(win, COLOR_PAIR(2));
268 box(win, ' ', ' ');
269 wrefresh(win);
271 wattrset(win, 0);
273 /* Do random output of a character */
275 ch = 'a';
277 nodelay(stdscr, TRUE);
279 for (i = 0; i < 5000; ++i)
281 x = rand() % (width - 2) + 1;
282 y = rand() % (height - 2) + 1;
284 mvwaddch(win, y, x, ch);
285 wrefresh(win);
287 if (getch() != ERR)
288 break;
290 if (i == 2000)
292 ch = 'b';
293 init_pair(3, COLOR_CYAN, COLOR_YELLOW);
294 wattrset(win, COLOR_PAIR(3));
298 nodelay(stdscr, FALSE);
300 SubWinTest(win);
302 /* Erase and draw green window */
304 init_pair(4, COLOR_YELLOW, COLOR_GREEN);
305 wbkgd(win, COLOR_PAIR(4));
306 wattrset(win, A_BOLD);
307 werase(win);
308 wrefresh(win);
310 /* Draw RED bounding box */
312 wattrset(win, COLOR_PAIR(2));
313 box(win, ' ', ' ');
314 wrefresh(win);
316 /* Display Australia map */
318 wattrset(win, A_BOLD);
319 i = 0;
321 while (*AusMap[i])
323 mvwaddstr(win, i + 1, 8, AusMap[i]);
324 wrefresh(win);
325 napms(100);
326 ++i;
329 init_pair(5, COLOR_BLUE, COLOR_WHITE);
330 wattrset(win, COLOR_PAIR(5) | A_BLINK);
331 mvwaddstr(win, height - 2, 3,
332 " PDCurses 3.4 - DOS, OS/2, Win32, X11, SDL");
333 wrefresh(win);
335 /* Draw running messages */
337 init_pair(6, COLOR_BLACK, COLOR_WHITE);
338 wattrset(win, COLOR_PAIR(6));
339 w = width - 2;
340 nodelay(win, TRUE);
342 /* jbuhler's re-hacked scrolling messages */
344 for (j = 0; messages[j] != NULL; j++)
346 char *message = messages[j];
347 int msg_len = strlen(message);
348 int scroll_len = w + 2 * msg_len;
349 char *scrollbuf = malloc(scroll_len);
350 char *visbuf = scrollbuf + msg_len;
351 int stop = 0;
352 int i;
354 for (i = w + msg_len; i > 0; i--)
356 memset(visbuf, ' ', w);
357 strncpy(scrollbuf + i, message, msg_len);
358 mvwaddnstr(win, height / 2, 1, visbuf, w);
359 wrefresh(win);
361 if (wgetch(win) != ERR)
363 flushinp();
364 stop = 1;
365 break;
368 napms(100);
371 free(scrollbuf);
373 if (stop)
374 break;
377 j = 0;
379 /* Draw running 'A's across in RED */
381 init_pair(7, COLOR_RED, COLOR_GREEN);
382 wattron(win, COLOR_PAIR(7));
384 for (i = 2; i < width - 4; ++i)
386 ch = mvwinch(win, 5, i);
387 save[j++] = ch;
388 ch = ch & 0x7f;
389 mvwaddch(win, 5, i, ch);
392 wrefresh(win);
394 /* Put a message up; wait for a key */
396 i = height - 2;
397 wattrset(win, COLOR_PAIR(5));
398 mvwaddstr(win, i, 3,
399 " Type a key to continue or ESC to quit ");
400 wrefresh(win);
402 if (WaitForUser() == '\033')
403 break;
405 /* Restore the old line */
407 wattrset(win, 0);
409 for (i = 2, j = 0; i < width - 4; ++i)
410 mvwaddch(win, 5, i, save[j++]);
412 wrefresh(win);
414 BouncingBalls(win);
416 /* BouncingBalls() leaves a keystroke in the queue */
418 if (WaitForUser() == '\033')
419 break;
422 endwin();
424 return 0;