1 /* Program to test the svgalib keyboard functions. */
2 
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <signal.h>
7 #include <time.h>
8 #include <vga.h>
9 #include <vgagl.h>
10 #include <vgakeyboard.h>
11 
12 
13 
newcolor(void)14 static int newcolor(void)
15 {
16     if (BYTESPERPIXEL == 1)
17 	return random() % 15 + 1;
18     return gl_rgbcolor(random() & 255, random() & 255, random() & 255);
19 }
20 
21 
timeout(int sig)22 static void timeout(int sig)
23 {
24     keyboard_close();
25     vga_setmode(TEXT);
26     puts("Automatic termination after 60 seconds.");
27     exit(1);
28 }
29 
main(void)30 void main(void)
31 {
32     int vgamode, color, leftpressed;
33     int x, y;
34     vga_init();
35     vgamode = vga_getdefaultmode();
36     if ((vgamode == -1) || (vga_getmodeinfo(vgamode)->bytesperpixel != 1))
37 	vgamode = G320x200x256;
38 
39     if (!vga_hasmode(vgamode)) {
40 	printf("Mode not available.\n");
41 	exit(1);
42     }
43     printf("\nWARNING: This program will set the keyboard to RAW mode.\n"
44 	   "The keyboard routines in svgalib have not been tested\n"
45 	   "very much. There may be no recovery if something goes\n"
46 	   "wrong.\n\n"
47 	   "Press ctrl-c now to bail out, enter to continue.\n"
48 	   "In the test itself, use 'q' or Escape to quit.\n"
49 	   "It will also terminate after 60 seconds.\n"
50     "Use any cursor keys to move, keypad 0 or enter to change color.\n");
51 
52     getchar();
53 
54     vga_setmode(vgamode);
55     gl_setcontextvga(vgamode);
56     gl_enableclipping();
57 
58     signal(SIGALRM, timeout);
59 
60     /* This installs the default handler, which is good enough for most */
61     /* purposes. */
62     if (keyboard_init()) {
63 	printf("Could not initialize keyboard.\n");
64 	exit(1);
65     }
66     /* Translate to 4 keypad cursor keys, and unify enter key. */
67     keyboard_translatekeys(TRANSLATE_CURSORKEYS | TRANSLATE_KEYPADENTER |
68 			   TRANSLATE_DIAGONAL);
69     /* (TRANSLATE_DIAGONAL seems to give problems.) Michael: No doesn't...
70        but might not do what you expect.. */
71 
72     alarm(60);			/* Terminate after 60 seconds for safety. */
73 
74     x = WIDTH / 2;
75     y = HEIGHT / 2;
76     color = newcolor();
77     leftpressed = 0;
78     for (;;) {
79 	/* Draw moving box. */
80 	gl_fillbox(x, y, 5, 5, color);
81 
82 	/* Draw key status bar at top of screen. */
83 	gl_putbox(0, 0, 128, 1, keyboard_getstate());
84 
85 	/* Wait about 1/100th of a second. */
86 	/* Note that use of this function makes things less */
87 	/* smooth because of timer latency. */
88 	usleep(10000);
89 
90 	keyboard_update();
91 
92 	/* Move. */
93 	if (keyboard_keypressed(SCANCODE_CURSORLEFT))
94 	    x--;
95 	if (keyboard_keypressed(SCANCODE_CURSORRIGHT))
96 	    x++;
97 	if (keyboard_keypressed(SCANCODE_CURSORUP))
98 	    y--;
99 	if (keyboard_keypressed(SCANCODE_CURSORDOWN))
100 	    y++;
101 
102 	/* Boundary checks. */
103 	if (x < 0)
104 	    x = 0;
105 	if (x >= WIDTH)
106 	    x = WIDTH - 1;
107 	if (y < 1)
108 	    y = 1;
109 	if (y >= HEIGHT)
110 	    y = HEIGHT - 1;
111 
112 	/* Check for color change. */
113 	if (keyboard_keypressed(SCANCODE_KEYPAD0) ||
114 	    keyboard_keypressed(SCANCODE_ENTER)) {
115 	    if (!leftpressed) {
116 		color = newcolor();
117 		leftpressed = 1;
118 	    }
119 	} else
120 	    leftpressed = 0;
121 
122 	if (keyboard_keypressed(SCANCODE_Q) ||
123 	    keyboard_keypressed(SCANCODE_ESCAPE))
124 	    break;
125     }
126 
127     keyboard_close();		/* Don't forget this! */
128     vga_setmode(TEXT);
129     exit(0);
130 }
131