File: linearfork.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 (165 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download | duplicates (7)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Program to test the svgalib keyboard functions. */
/* and stress vga_safety_fork() */

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

#define zero_sa_mask(maskptr) memset(maskptr, 0, sizeof(sigset_t))

static char sig2release[] =
{SIGHUP, SIGINT, SIGQUIT, SIGILL,
 SIGTRAP, SIGIOT, SIGBUS, SIGFPE,
 SIGSEGV, SIGPIPE, SIGALRM, SIGTERM,
 SIGXCPU, SIGXFSZ, SIGVTALRM,
 SIGPROF, SIGPWR};


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 shutdown(void)
{
    puts("Shutdown called!");
}

void main(void)
{
    struct sigaction siga;
    int vgamode, color, leftpressed;
    int x, y;

    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\n"
    "\aWARNING, this version of keytest explicitly removes all svgalib\n"
      "automatic restore funcs, s.t. when you kill it from the outside\n"
    "only vga_safety_fork() can rescue you. Use this svgalib test tool\n"
	   "with EXTREME! care.\n"
	);

    getchar();

    vga_safety_fork(shutdown);	/* Does already enter a videomode */

    vga_init();

    /* Never do this in your code! */
    siga.sa_flags = 0;
    zero_sa_mask(&(siga.sa_mask));
    for (x = 0; x < sizeof(sig2release); x++) {
	siga.sa_handler = SIG_DFL;
	sigaction(sig2release[x], &siga, NULL);
    }

    vgamode = vga_getdefaultmode();
    if ((vgamode == -1) || (vga_getmodeinfo(vgamode)->bytesperpixel != 1))
	vgamode = G640x480x256;

    if (!vga_hasmode(vgamode)) {
	printf("Mode not available.\n");
	exit(1);
    }
    vga_setmode(vgamode);
    vga_setlinearaddressing();
    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);
}