1 /* Program to test the svgalib keyboard functions. */
2 /* and stress vga_safety_fork() */
3 
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <signal.h>
8 #include <string.h>
9 #include <time.h>
10 #include <vga.h>
11 #include <vgagl.h>
12 #include <vgakeyboard.h>
13 
14 #define zero_sa_mask(maskptr) memset(maskptr, 0, sizeof(sigset_t))
15 
16 static char sig2release[] =
17 {SIGHUP, SIGINT, SIGQUIT, SIGILL,
18  SIGTRAP, SIGIOT, SIGBUS, SIGFPE,
19  SIGSEGV, SIGPIPE, SIGALRM, SIGTERM,
20  SIGXCPU, SIGXFSZ, SIGVTALRM,
21  SIGPROF, SIGUSR1};
22 
23 
newcolor(void)24 static int newcolor(void)
25 {
26     if (BYTESPERPIXEL == 1)
27 	return random() % 15 + 1;
28     return gl_rgbcolor(random() & 255, random() & 255, random() & 255);
29 }
30 
31 
timeout(int sig)32 static void timeout(int sig)
33 {
34     keyboard_close();
35     vga_setmode(TEXT);
36     puts("Automatic termination after 60 seconds.");
37     exit(1);
38 }
39 
shutdown(void)40 void shutdown(void)
41 {
42     puts("Shutdown called!");
43 }
44 
main(void)45 void main(void)
46 {
47     struct sigaction siga;
48     int vgamode, color, leftpressed;
49     int x, y;
50 
51     printf("\nWARNING: This program will set the keyboard to RAW mode.\n"
52 	   "The keyboard routines in svgalib have not been tested\n"
53 	   "very much. There may be no recovery if something goes\n"
54 	   "wrong.\n\n"
55 	   "Press ctrl-c now to bail out, enter to continue.\n"
56 	   "In the test itself, use 'q' or Escape to quit.\n"
57 	   "It will also terminate after 60 seconds.\n"
58     "Use any cursor keys to move, keypad 0 or enter to change color.\n\n"
59     "\aWARNING, this version of keytest explicitly removes all svgalib\n"
60       "automatic restore funcs, s.t. when you kill it from the outside\n"
61     "only vga_safety_fork() can rescue you. Use this svgalib test tool\n"
62 	   "with EXTREME! care.\n"
63 	);
64 
65     getchar();
66 
67     vga_safety_fork(shutdown);	/* Does already enter a videomode */
68 
69     vga_init();
70 
71     /* Never do this in your code! */
72     siga.sa_flags = 0;
73     zero_sa_mask(&(siga.sa_mask));
74     for (x = 0; x < sizeof(sig2release); x++) {
75 	siga.sa_handler = SIG_DFL;
76 	sigaction(sig2release[x], &siga, NULL);
77     }
78 
79     vgamode = vga_getdefaultmode();
80     if ((vgamode == -1) || (vga_getmodeinfo(vgamode)->bytesperpixel != 1))
81 	vgamode = G640x480x256;
82 
83     if (!vga_hasmode(vgamode)) {
84 	printf("Mode not available.\n");
85 	exit(1);
86     }
87     vga_setmode(vgamode);
88     vga_setlinearaddressing();
89     gl_setcontextvga(vgamode);
90     gl_enableclipping();
91 
92     signal(SIGALRM, timeout);
93 
94     /* This installs the default handler, which is good enough for most */
95     /* purposes. */
96     if (keyboard_init()) {
97 	printf("Could not initialize keyboard.\n");
98 	exit(1);
99     }
100     /* Translate to 4 keypad cursor keys, and unify enter key. */
101     keyboard_translatekeys(TRANSLATE_CURSORKEYS | TRANSLATE_KEYPADENTER |
102 			   TRANSLATE_DIAGONAL);
103     /* (TRANSLATE_DIAGONAL seems to give problems.) Michael: No doesn't...
104        but might not do what you expect.. */
105 
106     alarm(60);			/* Terminate after 60 seconds for safety. */
107 
108     x = WIDTH / 2;
109     y = HEIGHT / 2;
110     color = newcolor();
111     leftpressed = 0;
112     for (;;) {
113 	/* Draw moving box. */
114 	gl_fillbox(x, y, 5, 5, color);
115 
116 	/* Draw key status bar at top of screen. */
117 	gl_putbox(0, 0, 128, 1, keyboard_getstate());
118 
119 	/* Wait about 1/100th of a second. */
120 	/* Note that use of this function makes things less */
121 	/* smooth because of timer latency. */
122 	usleep(10000);
123 
124 	keyboard_update();
125 
126 	/* Move. */
127 	if (keyboard_keypressed(SCANCODE_CURSORLEFT))
128 	    x--;
129 	if (keyboard_keypressed(SCANCODE_CURSORRIGHT))
130 	    x++;
131 	if (keyboard_keypressed(SCANCODE_CURSORUP))
132 	    y--;
133 	if (keyboard_keypressed(SCANCODE_CURSORDOWN))
134 	    y++;
135 
136 	/* Boundary checks. */
137 	if (x < 0)
138 	    x = 0;
139 	if (x >= WIDTH)
140 	    x = WIDTH - 1;
141 	if (y < 1)
142 	    y = 1;
143 	if (y >= HEIGHT)
144 	    y = HEIGHT - 1;
145 
146 	/* Check for color change. */
147 	if (keyboard_keypressed(SCANCODE_KEYPAD0) ||
148 	    keyboard_keypressed(SCANCODE_ENTER)) {
149 	    if (!leftpressed) {
150 		color = newcolor();
151 		leftpressed = 1;
152 	    }
153 	} else
154 	    leftpressed = 0;
155 
156 	if (keyboard_keypressed(SCANCODE_Q) ||
157 	    keyboard_keypressed(SCANCODE_ESCAPE))
158 	    break;
159     }
160 
161     keyboard_close();		/* Don't forget this! */
162 
163     vga_setmode(TEXT);
164     exit(0);
165 }
166