File: keytest.c

package info (click to toggle)
svgalib 1%3A1.4.3-24
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,768 kB
  • ctags: 9,153
  • sloc: ansic: 59,341; makefile: 1,141; asm: 630; sh: 61; perl: 54; pascal: 49
file content (130 lines) | stat: -rw-r--r-- 3,165 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Program to test the svgalib keyboard functions. */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <vga.h>
#include <vgagl.h>
#include <vgakeyboard.h>



static int newcolor(void)
{
    if (BYTESPERPIXEL == 1)
	return random() % 15 + 1;
    return gl_rgbcolor(random() & 255, random() & 255, random() & 255);
}


static void timeout(int sig)
{
    keyboard_close();
    vga_setmode(TEXT);
    puts("Automatic termination after 60 seconds.");
    exit(1);
}

void main(void)
{
    int vgamode, color, leftpressed;
    int x, y;
    vga_init();
    vgamode = vga_getdefaultmode();
    if ((vgamode == -1) || (vga_getmodeinfo(vgamode)->bytesperpixel != 1))
	vgamode = G320x200x256;

    if (!vga_hasmode(vgamode)) {
	printf("Mode not available.\n");
	exit(1);
    }
    printf("\nWARNING: This program will set the keyboard to RAW mode.\n"
	   "The keyboard routines in svgalib have not been tested\n"
	   "very much. There may be no recovery if something goes\n"
	   "wrong.\n\n"
	   "Press ctrl-c now to bail out, enter to continue.\n"
	   "In the test itself, use 'q' or Escape to quit.\n"
	   "It will also terminate after 60 seconds.\n"
    "Use any cursor keys to move, keypad 0 or enter to change color.\n");

    getchar();

    vga_setmode(vgamode);
    gl_setcontextvga(vgamode);
    gl_enableclipping();

    signal(SIGALRM, timeout);

    /* This installs the default handler, which is good enough for most */
    /* purposes. */
    if (keyboard_init()) {
	printf("Could not initialize keyboard.\n");
	exit(1);
    }
    /* Translate to 4 keypad cursor keys, and unify enter key. */
    keyboard_translatekeys(TRANSLATE_CURSORKEYS | TRANSLATE_KEYPADENTER |
			   TRANSLATE_DIAGONAL);
    /* (TRANSLATE_DIAGONAL seems to give problems.) Michael: No doesn't...
       but might not do what you expect.. */

    alarm(60);			/* Terminate after 60 seconds for safety. */

    x = WIDTH / 2;
    y = HEIGHT / 2;
    color = newcolor();
    leftpressed = 0;
    for (;;) {
	/* Draw moving box. */
	gl_fillbox(x, y, 5, 5, color);

	/* Draw key status bar at top of screen. */
	gl_putbox(0, 0, 128, 1, keyboard_getstate());

	/* Wait about 1/100th of a second. */
	/* Note that use of this function makes things less */
	/* smooth because of timer latency. */
	usleep(10000);

	keyboard_update();

	/* Move. */
	if (keyboard_keypressed(SCANCODE_CURSORLEFT))
	    x--;
	if (keyboard_keypressed(SCANCODE_CURSORRIGHT))
	    x++;
	if (keyboard_keypressed(SCANCODE_CURSORUP))
	    y--;
	if (keyboard_keypressed(SCANCODE_CURSORDOWN))
	    y++;

	/* Boundary checks. */
	if (x < 0)
	    x = 0;
	if (x >= WIDTH)
	    x = WIDTH - 1;
	if (y < 1)
	    y = 1;
	if (y >= HEIGHT)
	    y = HEIGHT - 1;

	/* Check for color change. */
	if (keyboard_keypressed(SCANCODE_KEYPAD0) ||
	    keyboard_keypressed(SCANCODE_ENTER)) {
	    if (!leftpressed) {
		color = newcolor();
		leftpressed = 1;
	    }
	} else
	    leftpressed = 0;

	if (keyboard_keypressed(SCANCODE_Q) ||
	    keyboard_keypressed(SCANCODE_ESCAPE))
	    break;
    }

    keyboard_close();		/* Don't forget this! */
    vga_setmode(TEXT);
    exit(0);
}