1 /* Program to test the svgalib mouse functions. */
2 /* Updated to use middle button and rx axis (for wheel mice)
3 by Brion Vibber <brion@pobox.com>, 5 July 1998 */
4 // Part of the svgalib package.
5 // Included in Linrad with a return value from main
6 // to compile without warnings or errors.
7
8
9
10 #include <stdlib.h>
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <vga.h>
15 #include <vgagl.h>
16 #include <vgamouse.h>
17 #include <vgakeyboard.h>
18
19 /* Manually open and close mouse? */
20 #define MANUALLY_SETUP_MOUSE_NOT
21
22
newcolor(void)23 static int newcolor(void)
24 {
25 if (BYTESPERPIXEL == 1)
26 return random() % 15 + 1;
27 return gl_rgbcolor(random() & 255, random() & 255, random() & 255);
28 }
29
30
main(void)31 int main(void)
32 {
33 int vgamode, color, leftpressed, middlepressed;
34 int x, y, rx, ox, oy, boxsize, button, wheel;
35 struct MouseCaps caps;
36
37 vga_init();
38 vgamode = vga_getdefaultmode();
39 if (vgamode == -1)
40 vgamode = G320x200x256;
41
42 if (!vga_hasmode(vgamode)) {
43 printf("Mode not available.\n");
44 exit(-1);
45 }
46 #ifndef MANUALLY_SETUP_MOUSE
47 /* Enable automatic mouse setup at mode set. */
48 vga_setmousesupport(1);
49 #endif
50 vga_setmode(vgamode);
51 /* Disable wrapping (default). */
52 /* mouse_setwrap(MOUSE_NOWRAP); */
53 gl_setcontextvga(vgamode);
54 gl_enableclipping();
55
56 #ifdef MANUALLY_SETUP_MOUSE
57 mouse_init("/dev/mouse", MOUSE_MICROSOFT, MOUSE_DEFAULTSAMPLERATE);
58 mouse_setxrange(0, WIDTH - 1);
59 mouse_setyrange(0, HEIGHT - 1);
60 mouse_setwrap(MOUSE_NOWRAP);
61 #endif
62
63 /* Check the mouse capabilities */
64 if(mouse_getcaps(&caps)) {
65 /* Failed! Old library version... Check the mouse type. */
66 switch(vga_getmousetype() & MOUSE_TYPE_MASK) {
67 case MOUSE_INTELLIMOUSE:
68 case MOUSE_IMPS2:
69 wheel = 1;
70 break;
71 default:
72 wheel = 0;
73 }
74 } else {
75 /* If this is a wheel mouse, interpret rx as a wheel */
76 wheel = ((caps.info & MOUSE_INFO_WHEEL) != 0);
77 }
78
79 /* To be able to test fake mouse events... */
80 if (keyboard_init()) {
81 printf("Could not initialize keyboard.\n");
82 exit(1);
83 }
84
85 /* Set the range for the wheel */
86 if(wheel)
87 mouse_setrange_6d(0,0, 0,0, 0, 0, -180,180, 0,0, 0,0, MOUSE_RXDIM);
88
89 color = newcolor();
90 leftpressed = middlepressed = x = y = rx = ox = oy = 0;
91 boxsize = 5;
92
93 for (;;) {
94 keyboard_update();
95 gl_fillbox(x, y, boxsize, boxsize, color);
96 mouse_update();
97
98 /* The RX axis represents the wheel on an wheel mouse */
99 mouse_getposition_6d(&x, &y, NULL, &rx, NULL, NULL);
100
101 if(wheel && rx) {
102 /* For clarity - wipe the old location out
103 so we can redraw with the new box size */
104 gl_fillbox(ox, oy, boxsize, boxsize, 0);
105
106 /* Interpret wheel turns; we care only about direction,
107 not amount, for our purposes */
108 boxsize += (rx / abs(rx));
109 (boxsize < 1)?(boxsize = 1):((boxsize > 10)?(boxsize = 10):0);
110
111 /* Zero the wheel position */
112 mouse_setposition_6d(0,0,0, 0,0,0, MOUSE_RXDIM);
113 }
114
115 ox = x; oy = y;
116
117 button = mouse_getbutton();
118 if (button & MOUSE_LEFTBUTTON) {
119 if (!leftpressed) {
120 color = newcolor();
121 leftpressed = 1;
122 }
123 } else
124 leftpressed = 0;
125
126 if (button & MOUSE_MIDDLEBUTTON) {
127 if (!middlepressed) {
128 /* Move the cursor to a random location */
129 mouse_setposition_6d(random() % WIDTH, random() % HEIGHT,0,
130 0,0,0,
131 MOUSE_2DIM);
132 middlepressed = 1;
133 }
134 } else
135 middlepressed = 0;
136
137 if (button & MOUSE_RIGHTBUTTON)
138 break;
139 }
140
141 #ifdef MANUALLY_SETUP_MOUSE
142 mouse_close();
143 #endif
144 vga_setmode(TEXT);
145 return 0;
146 }
147