File: mjoytest.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 (291 lines) | stat: -rw-r--r-- 7,370 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Program to test the svgalib joystick functions. */
/* Written by M. Weller <eowmob@exp-math.uni-essen.de> */

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <vga.h>
#include <vgagl.h>
#include <vgamouse.h>
#include <vgajoystick.h>
#include <vgakeyboard.h>

extern char *optarg;
extern int optind, opterr, optopt;

#define PENCILSIZE 5

struct {
    int wx;		/* xoffset to add to scaled joystick position to get cx */
    int cx, cy;		/* onscreen coords of pencil */
    int jx, jy;		/* current joystick status */
    int tx;		/* onscreen xcoord of text for joypos */
    char bitmap[PENCILSIZE * PENCILSIZE * 4]; /* big enough in any screen mode */
    int ox, oy; /* location of saved patch, ox < 0 for no data saved */
    int color;		/* drawing color */
    int drawing;	/* we are drawing (actually shadows button 1 state) */
    int newpos;		/* cx/cy changed, draw new pencil position */
} joypanel[2];

int wy, sx, sy, ty;	/* wy, ty y coords of wx, tx. sx/sy are scale values:
                         * (jx * sx + 128) / 256 is pencil coords (add wx for screen
                         * coords). Same for y. */

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

void draw_pencil(int i) {
    char msg[100];

    if (!joypanel[i].newpos)
	return;

    sprintf(msg, "x = %4d, y = %4d", joypanel[i].jx, joypanel[i].jy);
    gl_write(joypanel[i].tx, ty, msg);

    if (joypanel[i].ox >= 0)
	gl_putbox(joypanel[i].ox, joypanel[i].oy, PENCILSIZE, PENCILSIZE, joypanel[i].bitmap);

    /* If not drawing, save destination area */
    if (!joypanel[i].drawing)
	gl_getbox(joypanel[i].ox = joypanel[i].cx, joypanel[i].oy = joypanel[i].cy,
		  PENCILSIZE, PENCILSIZE, joypanel[i].bitmap);
    else
	joypanel[i].ox = -1;
  
    gl_fillbox(joypanel[i].cx, joypanel[i].cy, PENCILSIZE, PENCILSIZE, joypanel[i].color);
    joypanel[i].newpos = 0;
}

void init_screen(void) {
    int white;

    gl_clearscreen(0);

    white = vga_white();

    gl_line(0, 0, WIDTH - 2, 0, white);
    gl_line(0, 0, 0, HEIGHT - 1, white);
    gl_line(WIDTH/2, 0, WIDTH/2, HEIGHT - 1, white);
    gl_line(WIDTH - 2, 0, WIDTH - 2 , HEIGHT - 1, white);
    gl_line(0, 11, WIDTH - 2, 11, white);
    gl_line(0, HEIGHT - 1, WIDTH - 2, HEIGHT - 1, white);

    ty = 2;
    sx = WIDTH / 2 - 3 - PENCILSIZE;
    sy = HEIGHT - 6 - PENCILSIZE - 9;
    wy = 2 + 8 + 3 + (((sy << 7) + 128) >> 8);

    joypanel[0].color = white;
    joypanel[0].drawing = 0;
    joypanel[0].newpos = 1;
    joypanel[0].ox = -1;
    joypanel[0].tx = 2;
    joypanel[0].jx = 0;
    joypanel[0].jy = 0;
    joypanel[0].wx = 2 + (((sx << 7) + 128) >> 8);
    joypanel[0].cx = joypanel[0].wx;
    joypanel[0].cy = wy;

    draw_pencil(0);

    joypanel[1].color = white;
    joypanel[1].drawing = 0;
    joypanel[1].newpos = 1;
    joypanel[1].ox = -1;
    joypanel[1].tx = WIDTH / 2 + 2;
    joypanel[1].jx = 0;
    joypanel[1].jy = 0;
    joypanel[1].wx = joypanel[0].wx + joypanel[1].tx;
    joypanel[1].cx = joypanel[0].cx + joypanel[1].tx;
    joypanel[1].cy = wy;

    draw_pencil(1);
}

void myhandler(int event, int number, char value, int joydev) {
#if 0 
    printf("%d: %d %d %d\n", joydev, event, number, (int)value);
#endif
    switch(event) {
	case JOY_EVENTBUTTONUP:
	    if (!number)
		joypanel[joydev].drawing = 0;
	    else {
		joypanel[joydev].color = newcolor();
		joypanel[joydev].newpos = 1;
	    }
	    break;
	case JOY_EVENTBUTTONDOWN:
	    if (!number)
		joypanel[joydev].drawing = 1;
	    break;
	case JOY_EVENTAXIS:
	    switch(number) {
		case 0: /* x */
		    joypanel[joydev].jx = value;
		    joypanel[joydev].cx = joypanel[joydev].wx + ((((int)value) * sx + 128) / 256);
		    joypanel[joydev].newpos = 1;
		    break;
		case 1:
		    joypanel[joydev].jy = value;
		    joypanel[joydev].cy = wy + ((((int)value) * sy + 128) / 256);
		    joypanel[joydev].newpos = 1;
		    break;
	    }
    }
    /* Note that any reserved events are ignored */
}

void usage(void) {
    puts("Usage: mjoytest [-j <joystick number>] [svgalib mode]\n"
	 "\ttest multiple joystick support and joystick sharing.");
    exit(1);
}

void mycalout(const char *msg) {
    gl_printf(-1, -1, msg);
}

void main(int argc, char *argv[]) {
    int vgamode = -1;
    int which, joymask = 3;
    struct timeval timeout;

    while(EOF != (which = getopt(argc, argv, "j:m:"))) {
	switch(which) {
	    case 'j':
		if (!strcmp(optarg, "0"))
		    joymask = 1;
		else if (!strcmp(optarg, "1"))
		    joymask = 1;
		else
		    usage();
		break;
	    case 'm':
		vgamode = vga_getmodenumber(optarg);
		if (vgamode < 0)
		    usage();
		break;
	    default:
		usage();
	}
    }
    if (optind < argc) {
	if (optind != 1 + argc)
	    usage();
	if (vgamode >= 0)
	    usage();
	vgamode = vga_getmodenumber(argv[optind]);
	if (vgamode < 0)
	    usage();
    }
    vga_init();
    if (vgamode < 0)
	vgamode = vga_getdefaultmode();
    if (vgamode < 0)
	vgamode = G320x200x256;

    if (!vga_hasmode(vgamode)) {
	printf("Mode not available.\n");
	exit(-1);
    }

    puts("In the demo, press\n"
	 "<1> to calibrate joystick 1.\n"
	 "<2> to calibrate joystick 2.\n"
	 "<c> to clear the screen (<1> & <2> do this too).\n"
	 "<q> to exit (<Ctrl>-C should work too.\n"
	 "Joystick button 1 enables drawing while pressed.\n"
	 "Joystick button 2 selects next color.\n"
	 "\nNow hit <Return> to start the demo.");

    getchar();
    fflush(stdin);

    for (which = 0; which < 2; which++) {
	if (!(joymask & (1 << which)))
	    continue;
	errno = 0;
	if (joystick_init(which, JOY_CALIB_STDOUT) < 0) {
	    if (errno)
		printf("Unable to initialize joystick %d: %s.\n", which, strerror(errno));
	    else
		printf("Unable to initialize joystick %d.\n", which);
	}
    }

    joystick_sethandler(-1, myhandler);

    vga_setmode(vgamode);
    gl_setcontextvga(vgamode);

    gl_setwritemode(FONT_COMPRESSED | WRITEMODE_OVERWRITE);
    gl_setfontcolors(0, vga_white());
    gl_setfont(8, 8, gl_font8x8);

    init_screen();

    for(;;) {
	timeout.tv_sec = 0;
	timeout.tv_usec = 10000;
	which = vga_waitevent(VGA_KEYEVENT, NULL, NULL, NULL, &timeout);
	if (which & VGA_KEYEVENT) {
	    switch(vga_getch()) {
		case '1':
		    gl_printf(2, 2, "");
		    if (1 & joymask) {
			vga_lockvc();
			joystick_init(0, mycalout);
		        /* IMPORTANT, reenable ownhandler! */
    		        joystick_sethandler(0, myhandler);
			vga_unlockvc();
		    }
		    init_screen();
		    break;
		case '2':
		    gl_printf(2, 2, "");
		    if (2 & joymask) {
			vga_lockvc();
			joystick_init(1, mycalout);
		        /* IMPORTANT, reenable ownhandler! */
    		        joystick_sethandler(1, myhandler);
			vga_unlockvc();
		    }
		    init_screen();
		    break;
		case 'c':
		case 'C':
		    init_screen();
		    break;
		case 'q':
		case 'Q':
		    goto leave_loop;
		default:
		    putchar('\a');
		    fflush(stdout);
		    break;
	    }
	}
	which = joystick_update();
	if (which & 1);
	    draw_pencil(0); /* It makes only sense to check for the newpos flag
			     * if something happened with the joystick at all */
	if (which & 2);
	    draw_pencil(1);
    }
  leave_loop:
    printf("Shutting down.\n");

    vga_setmode(TEXT);
    exit(0);
}