File: mousetest.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 (140 lines) | stat: -rw-r--r-- 3,701 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
/* Program to test the svgalib mouse functions. */
/* Updated to use middle button and rx axis (for wheel mice)
   by Brion Vibber <brion@pobox.com>, 5 July 1998 */

#include <stdlib.h>
#include <stdio.h>
#include <vga.h>
#include <vgagl.h>
#include <vgamouse.h>
#include <vgakeyboard.h>

/* Manually open and close mouse? */
#define MANUALLY_SETUP_MOUSE_NOT


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


void main(void)
{
    int vgamode, color, leftpressed, middlepressed;
    int x, y, rx, ox, oy, boxsize, button, wheel;
    struct MouseCaps caps;

    vga_init();
    vgamode = vga_getdefaultmode();
    if (vgamode == -1)
	vgamode = G320x200x256;

    if (!vga_hasmode(vgamode)) {
	printf("Mode not available.\n");
	exit(-1);
    }
#ifndef MANUALLY_SETUP_MOUSE
    /* Enable automatic mouse setup at mode set. */
    vga_setmousesupport(1);
#endif
    vga_setmode(vgamode);
    /* Disable wrapping (default). */
    /* mouse_setwrap(MOUSE_NOWRAP); */
    gl_setcontextvga(vgamode);
    gl_enableclipping();

#ifdef MANUALLY_SETUP_MOUSE
    mouse_init("/dev/mouse", MOUSE_MICROSOFT, MOUSE_DEFAULTSAMPLERATE);
    mouse_setxrange(0, WIDTH - 1);
    mouse_setyrange(0, HEIGHT - 1);
    mouse_setwrap(MOUSE_NOWRAP);
#endif

    /* Check the mouse capabilities */
    if(mouse_getcaps(&caps)) {
        /* Failed! Old library version... Check the mouse type. */
        switch(vga_getmousetype() & MOUSE_TYPE_MASK) {
        case MOUSE_INTELLIMOUSE:
        case MOUSE_IMPS2:
            wheel = 1;
            break;
        default:
            wheel = 0;
        }
    } else {
        /* If this is a wheel mouse, interpret rx as a wheel */
        wheel = ((caps.info & MOUSE_INFO_WHEEL) != 0);
    }

    /* To be able to test fake mouse events... */
    if (keyboard_init()) {
	printf("Could not initialize keyboard.\n");
	exit(1);
    }

    /* Set the range for the wheel */
    if(wheel)
        mouse_setrange_6d(0,0, 0,0, 0, 0, -180,180, 0,0, 0,0, MOUSE_RXDIM);

    color = newcolor();
    leftpressed = middlepressed = x = y = rx = ox = oy = 0;
    boxsize = 5;
    
    for (;;) {
	keyboard_update();
	gl_fillbox(x, y, boxsize, boxsize, color);
        mouse_update();

        /* The RX axis represents the wheel on an wheel mouse */
        mouse_getposition_6d(&x, &y, NULL, &rx, NULL, NULL);

        if(wheel && rx) {
            /* For clarity - wipe the old location out
             so we can redraw with the new box size */
            gl_fillbox(ox, oy, boxsize, boxsize, 0);

            /* Interpret wheel turns; we care only about direction,
               not amount, for our purposes */
            boxsize += (rx / abs(rx));
            (boxsize < 1)?(boxsize = 1):((boxsize > 10)?(boxsize = 10):0);

            /* Zero the wheel position */
            mouse_setposition_6d(0,0,0, 0,0,0, MOUSE_RXDIM);
        }

        ox = x; oy = y;

	button = mouse_getbutton();
	if (button & MOUSE_LEFTBUTTON) {
	    if (!leftpressed) {
		color = newcolor();
		leftpressed = 1;
	    }
	} else
            leftpressed = 0;

        if (button & MOUSE_MIDDLEBUTTON) {
	    if (!middlepressed) {
                /* Move the cursor to a random location */
                mouse_setposition_6d(random() % WIDTH, random() % HEIGHT,0,
                                     0,0,0,
                                     MOUSE_2DIM);
                middlepressed = 1;
            }
        } else
            middlepressed = 0;

	if (button & MOUSE_RIGHTBUTTON)
	    break;
    }

#ifdef MANUALLY_SETUP_MOUSE
    mouse_close();
#endif

    vga_setmode(TEXT);
    exit(0);
}