]> git.sur5r.net Git - cc65/blob - samples/mousetest.c
Don't fiddle with foreign files.
[cc65] / samples / mousetest.c
1 /*
2 ** Test/demo program for mouse usage.
3 ** Will work for the C64/C128/CBM510/Atari/Apple2.
4 **
5 ** 2001-09-13, Ullrich von Bassewitz
6 ** 2013-09-05, Greg King
7 **
8 */
9
10
11
12 #include <stdbool.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <mouse.h>
16 #include <pen.h>
17 #include <conio.h>
18 #include <ctype.h>
19 #include <dbg.h>
20
21 #define max(a,b)  (((a) > (b)) ? (a) : (b))
22 #define min(a,b)  (((a) < (b)) ? (a) : (b))
23
24
25
26 #ifdef MOUSE_DRIVER
27
28 /* A statically linked driver was named on the compiler's command line.
29 ** Make sure that it is used instead of a dynamic one.
30 */
31 #  undef DYN_DRV
32 #  define DYN_DRV       0
33 #else
34
35 /* Use a dynamically loaded driver, by default. */
36 #  ifndef DYN_DRV
37 #    define DYN_DRV     1
38 #  endif
39 #endif
40
41
42
43 #ifdef __CBM__
44
45 /* Set dark-on-light colors. */
46 const unsigned char mouse_def_pointercolor = COLOR_BLACK;
47
48 #endif
49
50
51
52 static void __fastcall__ CheckError (const char* S, unsigned char Error)
53 {
54     if (Error != MOUSE_ERR_OK) {
55         cprintf ("\n%s: %s(%u)\r\n", S, mouse_geterrormsg (Error), Error);
56
57         /* Wait for a key-press, so that some platforms can show the error
58         ** message before they remove the current screen.
59         */
60         cgetc();
61         exit (EXIT_FAILURE);
62     }
63 }
64
65
66
67 #if DYN_DRV
68
69 /* Points to the dynamic driver's name. */
70 static const char *mouse_name;
71
72
73
74 static void DoWarning (void)
75 /* Warn the user that a driver is needed for this program. */
76 {
77     cprintf ("Warning: This program needs\r\n"
78              "the driver with the name\r\n"
79              "    %s\r\n"
80              "on a disk! Press 'y' if you have it;\r\n"
81              "or, any other key to exit.\r\n", mouse_stddrv);
82     if (tolower (cgetc ()) != 'y') {
83         exit (EXIT_SUCCESS);
84     }
85     cprintf ("OK. Please wait patiently...\r\n");
86 }
87 #endif
88
89
90
91 static void __fastcall__ ShowState (unsigned char Jailed, unsigned char Invisible)
92 /* Display jail and cursor states. */
93 {
94     cclearxy (0, 7, 32);
95     gotoxy (0, 7);
96     cprintf ("Pointer is %svisible%s.", Invisible? "in" : "", Jailed? " and jailed" : "");
97 }
98
99
100
101 #if DYN_DRV
102 int main (int argc, char *argv[])
103 #else
104 int main (void)
105 #endif
106 {
107     struct mouse_info info;
108     struct mouse_box full_box, small_box;
109     unsigned char width, height;
110     char C;
111     bool Invisible = true, Done = false, Jailed = false;
112
113     /* Initialize the debugger */
114     DbgInit (0);
115
116     /* Set dark-on-light colors.  Clear the screen. */
117 #ifdef __CBM__
118     (void) bordercolor (COLOR_GRAY2);
119     (void) bgcolor (COLOR_WHITE);
120     (void) textcolor (COLOR_GRAY1);
121 #else
122     (void) bordercolor (COLOR_BLUE);
123     (void) bgcolor (COLOR_WHITE);
124     (void) textcolor (COLOR_BLACK);
125 #endif
126     cursor (0);
127     clrscr ();
128
129     /* If a lightpen driver is installed, then it can get a calibration value
130     ** from this file (if it exists).  Or, the user can adjust the pen; and,
131     ** the value will be put into this file, for the next time.
132     ** (Other drivers will ignore this.)
133     */
134 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
135     pen_adjust ("pen.dat");
136 #endif
137
138 #if DYN_DRV
139     /* If a dynamically loadable driver is named on the command line,
140     ** then use that driver instead of the standard one.
141     */
142     if (argc > 1) {
143         mouse_name = argv[1];
144     } else {
145         /* Output a warning about the standard driver that is needed. */
146         DoWarning ();
147         mouse_name = mouse_stddrv;
148     }
149
150     /* Load and install the driver. */
151     CheckError ("mouse_load_driver",
152                 mouse_load_driver (&mouse_def_callbacks, mouse_name));
153 #else
154     /* Install the driver. */
155     CheckError ("mouse_install",
156                 mouse_install (&mouse_def_callbacks,
157 #  ifdef MOUSE_DRIVER
158                                MOUSE_DRIVER
159 #  else
160                                mouse_static_stddrv
161 #  endif
162                                ));
163 #endif
164
165     /* Get the initial bounding box. */
166     mouse_getbox (&full_box);
167
168     screensize (&width, &height);
169
170 top:
171     clrscr ();
172
173     /* Print a help line */
174     cputs (" d)ebug  h)ide   q)uit   s)how   j)ail");
175
176     /* Put a cross at the center of the screen. */
177     gotoxy (width / 2 - 3, height / 2 - 1);
178 #if defined(__CBM__)
179     cprintf ("%3u,%3u\r\n%*s\xDB", width / 2 * 8 + 4, height / 2 * 8 + 4,
180              width / 2, "");
181 #else
182     cprintf ("%3u,%3u\r\n%*s+", width / 2 * 8 + 4, height / 2 * 8 + 4,
183              width / 2, "");
184 #endif
185
186     /* Test loop */
187     ShowState (Jailed, Invisible);
188     do {
189         /* Get the current co-ordinates and button states; and, print them. */
190         mouse_info (&info);
191         gotoxy (0, 2);
192         cprintf (" X  = %3d\r\n", info.pos.x);
193         cprintf (" Y  = %3d\r\n", info.pos.y);
194         cprintf (" B1 = %c\r\n", (info.buttons & MOUSE_BTN_LEFT) ?
195 #ifdef __CBM__
196                  0x5F
197 #else
198                  'v'
199 #endif
200                  : '^');
201         cprintf (" B2 = %c", (info.buttons & MOUSE_BTN_RIGHT) ?
202 #ifdef __CBM__
203                  0x5F
204 #else
205                  'v'
206 #endif
207                  : '^');
208
209         /* Handle user input */
210         if (kbhit ()) {
211             cclearxy (1, 9, 23);
212             switch (tolower (C = cgetc ())) {
213                 case 'd':
214                     BREAK();
215
216                     /* The debugger might have changed the colors.
217                     ** Restore them.
218                     */
219 #ifdef __CBM__
220                     (void) bordercolor (COLOR_GRAY2);
221                     (void) bgcolor (COLOR_WHITE);
222                     (void) textcolor (COLOR_GRAY1);
223 #else
224                     (void) bordercolor (COLOR_BLUE);
225                     (void) bgcolor (COLOR_WHITE);
226                     (void) textcolor (COLOR_BLACK);
227 #endif
228
229                     /* The debugger changed the screen; restore it. */
230                     goto top;
231
232                 case 'h':
233                     mouse_hide ();
234                     ShowState (Jailed, ++Invisible);
235                     break;
236
237                 case 'j':
238                     if (Jailed) {
239                         mouse_setbox (&full_box);
240                         Jailed = false;
241                     } else {
242                         small_box.minx = max (info.pos.x - 10, full_box.minx);
243                         small_box.miny = max (info.pos.y - 10, full_box.miny);
244                         small_box.maxx = min (info.pos.x + 10, full_box.maxx);
245                         small_box.maxy = min (info.pos.y + 10, full_box.maxy);
246                         mouse_setbox (&small_box);
247                         Jailed = true;
248                     }
249                     ShowState (Jailed, Invisible);
250                     break;
251
252                 case 's':
253                     mouse_show ();
254                     if (Invisible) {
255                         ShowState (Jailed, --Invisible);
256                     }
257                     break;
258
259                 case 'q':
260                     Done = true;
261                     break;
262
263                 default:
264                     gotoxy (1, 9);
265                     cprintf ("Spurious character: $%02X", C);
266             }
267         }
268     } while (!Done);
269
270 #if DYN_DRV
271     /* Uninstall and unload the driver. */
272     CheckError ("mouse_unload", mouse_unload ());
273 #else
274     /* Uninstall the static driver. */
275     CheckError ("mouse_uninstall", mouse_uninstall ());
276 #endif
277
278     /* Say goodbye */
279     cputsxy (0, height / 2 + 3, "Goodbye!");
280     return EXIT_SUCCESS;
281 }