]> git.sur5r.net Git - cc65/blob - testcode/lib/mouse-test.c
Add testcode/lib/mouse-test.c. Rename samples/mousetest.c to mousedemo.c.
[cc65] / testcode / lib / mouse-test.c
1 /*
2 ** Test program for mouse drivers.
3 ** Supportsthe C64/C128/CBM510/Atari/Apple2.
4 **
5 ** 2001-09-13, Ullrich von Bassewitz
6 ** 2013-09-05, Greg King
7 **
8 ** Compile with "-DSTATIC_MOUSE" to statically link all available drivers.
9 ** Compile with "-DMOUSE_DRIVER=<driver_sym>" to statically link the given driver.
10 **     E.g., -DMOUSE_DRIVER=atrsts_mou to just link with the Atari ST mouse driver.
11 */
12
13
14
15 #include <stdbool.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <mouse.h>
19 #include <pen.h>
20 #include <conio.h>
21 #include <ctype.h>
22 #include <dbg.h>
23 #include <cc65.h>
24
25 #define max(a,b)  (((a) > (b)) ? (a) : (b))
26 #define min(a,b)  (((a) < (b)) ? (a) : (b))
27
28 extern int getsp(void);
29
30 #define NO_DEBUG
31 #define NO_JAIL
32
33 #ifdef __ATARI__
34 extern const struct mouse_callbacks mouse_pm_callbacks;
35 extern const struct mouse_callbacks mouse_txt_callbacks;
36 //#define MOUSE_CALLBACK mouse_def_callbacks
37 #define MOUSE_CALLBACK mouse_pm_callbacks
38 //#define MOUSE_CALLBACK mouse_txt_callbacks
39 #else
40 #define MOUSE_CALLBACK mouse_def_callbacks
41 #endif
42
43 #if defined(MOUSE_DRIVER) || defined(STATIC_MOUSE)
44
45 /* A statically linked driver was named on the compiler's command line.
46 ** Make sure that it is used instead of a dynamic one.
47 */
48 #  undef DYN_DRV
49 #  define DYN_DRV       0
50 #else
51
52 /* Use a dynamically loaded driver, by default. */
53 #  ifndef DYN_DRV
54 #    define DYN_DRV     1
55 #  endif
56 #endif
57
58
59
60 #ifdef __CBM__
61
62 /* Set dark-on-light colors. */
63 const unsigned char mouse_def_pointercolor = COLOR_BLACK;
64
65 #endif
66
67
68
69 static void __fastcall__ CheckError (const char* S, unsigned char Error)
70 {
71     if (Error != MOUSE_ERR_OK) {
72         cprintf ("\n%s: %s(%u)\r\n", S, mouse_geterrormsg (Error), Error);
73
74         /* Wait for a key-press, so that some platforms can show the error
75         ** message before they remove the current screen.
76         */
77         if (doesclrscrafterexit ()) {
78             cgetc ();
79         }
80         exit (EXIT_FAILURE);
81     }
82 }
83
84
85
86 #if DYN_DRV
87
88 /* Points to the dynamic driver's name. */
89 static const char *mouse_name;
90
91
92 static void DoWarning (void)
93 /* Warn the user that a driver is needed for this program. */
94 {
95     cprintf ("Warning: This program needs\r\n"
96              "the driver with the name\r\n"
97              "    %s\r\n"
98              "on a disk! Press 'y' if you have it;\r\n"
99              "or, any other key to exit.\r\n", mouse_stddrv);
100     if (tolower (cgetc ()) != 'y') {
101         exit (EXIT_SUCCESS);
102     }
103     cprintf ("OK. Please wait patiently...\r\n");
104 }
105
106 #else
107
108 unsigned char *mouse_drv_use;
109 #endif
110
111
112 #ifdef __ATARI__
113 #ifdef __ATARIXL__
114 #define MSENAME_EXT "X"
115 #define MSESTAT_0 atrxjoy_mou
116 #define MSESTAT_1 atrxst_mou
117 #define MSESTAT_2 atrxami_mou
118 #define MSESTAT_3 atrxtrk_mou
119 #define MSESTAT_4 atrxtt_mou
120 #else
121 #define MSENAME_EXT ""
122 #define MSESTAT_0 atrjoy_mou
123 #define MSESTAT_1 atrst_mou
124 #define MSESTAT_2 atrami_mou
125 #define MSESTAT_3 atrtrk_mou
126 #define MSESTAT_4 atrtt_mou
127 #endif
128 #define MSENAME_0 "ATR" MSENAME_EXT "JOY.MOU"
129 #define MSENAME_1 "ATR" MSENAME_EXT "ST.MOU"
130 #define MSENAME_2 "ATR" MSENAME_EXT "AMI.MOU"
131 #define MSENAME_3 "ATR" MSENAME_EXT "TRK.MOU"
132 #define MSENAME_4 "ATR" MSENAME_EXT "TT.MOU"
133 #elif defined(__C64__) || defined(__C128__)
134 #ifdef __C64__
135 #define MSENAME_EXT "c64-"
136 #define MSESTAT_0 c64_joy_mou
137 #define MSESTAT_1 c64_1351_mou
138 #define MSESTAT_2 c64_inkwell_mou
139 #define MSESTAT_3 c64_pot_mou
140 #else
141 #define MSENAME_EXT "c128-"
142 #define MSESTAT_0 c128_joy_mou
143 #define MSESTAT_1 c128_1351_mou
144 #define MSESTAT_2 c128_inkwell_mou
145 #define MSESTAT_3 c128_pot_mou
146 #endif
147 #define MSENAME_0 MSENAME_EXT "joy.mou"
148 #define MSENAME_1 MSENAME_EXT "1351.mou"
149 #define MSENAME_2 MSENAME_EXT "inkwell.mou"
150 #define MSENAME_3 MSENAME_EXT "pot.mou"
151 #endif
152
153
154 static void __fastcall__ ShowState (unsigned char Jailed, unsigned char Invisible)
155 /* Display jail and cursor states. */
156 {
157     cclearxy (0, 7, 32);
158     gotoxy (0, 7);
159     cprintf ("Pointer is %svisible%s.", Invisible? "in" : "", Jailed? " and jailed" : "");
160 }
161
162 #ifdef __ATARIXL__
163 extern char _HIDDEN_RAM_SIZE__, _HIDDEN_RAM_LAST__, _HIDDEN_RAM_START__;
164 #endif
165
166 #if DYN_DRV
167 int main (int argc, char *argv[])
168 #else
169 int main (void)
170 #endif
171 {
172     struct mouse_info info;
173     struct mouse_box full_box, small_box;
174     unsigned char width, height;
175     char C;
176     bool Invisible = true, Done = false, Jailed = false;
177
178 #ifdef __ATARIXL__
179     cprintf ("adding heap: $%04X bytes at $%04X\r\n",
180              &_HIDDEN_RAM_SIZE__ - (&_HIDDEN_RAM_LAST__ - &_HIDDEN_RAM_START__),
181              &_HIDDEN_RAM_LAST__);
182
183     _heapadd (&_HIDDEN_RAM_LAST__, (size_t)(&_HIDDEN_RAM_SIZE__ - (&_HIDDEN_RAM_LAST__ - &_HIDDEN_RAM_START__)));
184     cgetc ();
185 #endif
186
187 #ifndef NO_DEBUG
188     /* Initialize the debugger */
189     DbgInit (0);
190 #endif
191
192     /* Set dark-on-light colors.  Clear the screen. */
193 #ifdef __CBM__
194     (void) bordercolor (COLOR_GRAY2);
195     (void) bgcolor (COLOR_WHITE);
196     (void) textcolor (COLOR_GRAY1);
197 #else
198     (void) bordercolor (COLOR_BLUE);
199     (void) bgcolor (COLOR_WHITE);
200     (void) textcolor (COLOR_BLACK);
201 #endif
202     cursor (0);
203     clrscr ();
204
205     /* If a lightpen driver is installed, then it can get a calibration value
206     ** from this file (if it exists).  Or, the user can adjust the pen; and,
207     ** the value will be put into this file, for the next time.
208     ** (Other drivers will ignore this.)
209     */
210 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
211     pen_adjust ("pen.dat");
212 #endif
213
214 #if DYN_DRV
215     /* If a dynamically loadable driver is named on the command line,
216     ** then use that driver instead of the standard one.
217     */
218     if (argc > 1) {
219         mouse_name = argv[1];
220     } else {
221 #if defined(__ATARI__) || defined(__C64__) || defined(__C128__)
222         char selection, flag = 0;
223         cprintf ("Select mouse driver:\r\n"
224                  "  0 - Joystick\r\n"
225 #ifdef __ATARI__
226                  "  1 - ST Mouse\r\n"
227                  "  2 - Amiga Mouse\r\n"
228                  "  3 - Atari Trakball\r\n"
229                  "  4 - Atari TouchPad\r\n"
230 #else
231                  "  1 - 1351 Mouse\r\n"
232                  "  2 - Inkwell Mouse\r\n"
233                  "  3 - Paddle\r\n"
234 #endif
235                  "Enter selection: ");
236         while (1) {
237             switch (selection = cgetc ()) {
238             case '0': mouse_name = MSENAME_0; flag = 1; break;
239             case '1': mouse_name = MSENAME_1; flag = 1; break;
240             case '2': mouse_name = MSENAME_2; flag = 1; break;
241             case '3': mouse_name = MSENAME_3; flag = 1; break;
242 #ifdef __ATARI__
243             case '4': mouse_name = MSENAME_4; flag = 1; break;
244 #endif
245             }
246             if (flag) break;
247         }
248         cprintf ("%c\r\nOK, loading \"%s\",\r\nplease wait patiently...\r\n", selection, mouse_name);
249 #else
250         /* Output a warning about the standard driver that is needed. */
251         DoWarning ();
252         mouse_name = mouse_stddrv;
253 #endif
254     }
255
256     /* Load and install the driver. */
257     CheckError ("mouse_load_driver",
258                 mouse_load_driver (&MOUSE_CALLBACK, mouse_name));
259 #else  /* not DYN_DRV */
260 #if !defined(MOUSE_DRIVER) && (defined(__ATARI__) || defined(__C64__) || defined(__C128__))
261     {
262         char selection, flag = 0;
263         cprintf ("Select mouse driver:\r\n"
264                  "  0 - Joystick\r\n"
265 #ifdef __ATARI__
266                  "  1 - ST Mouse\r\n"
267                  "  2 - Amiga Mouse\r\n"
268                  "  3 - Atari Trakball\r\n"
269                  "  4 - Atari TouchPad\r\n"
270 #else
271                  "  1 - 1351 Mouse\r\n"
272                  "  2 - Inkwell Mouse\r\n"
273                  "  3 - Paddle\r\n"
274 #endif
275                  "Enter selection: ");
276         while (1) {
277             switch (selection = cgetc ()) {
278             case '0': mouse_drv_use = MSESTAT_0; flag = 1; break;
279             case '1': mouse_drv_use = MSESTAT_1; flag = 1; break;
280             case '2': mouse_drv_use = MSESTAT_2; flag = 1; break;
281             case '3': mouse_drv_use = MSESTAT_3; flag = 1; break;
282 #ifdef __ATARI__
283             case '4': mouse_drv_use = MSESTAT_4; flag = 1; break;
284 #endif
285             }
286             if (flag) break;
287         }
288     }
289 #else
290     mouse_drv_use = mouse_static_stddrv;
291 #endif
292
293     /* Install the driver. */
294     CheckError ("mouse_install",
295                 mouse_install (&MOUSE_CALLBACK,
296 #  ifdef MOUSE_DRIVER
297                                MOUSE_DRIVER
298 #  else
299 #if defined(__ATARI__) || defined(__C64__) || defined(__C128__)
300                                mouse_drv_use
301 #else
302                                mouse_static_stddrv
303 #endif
304 #  endif
305                                ));
306 #endif
307
308 #ifndef NO_JAIL
309     /* Get the initial bounding box. */
310     mouse_getbox (&full_box);
311 #endif
312
313     screensize (&width, &height);
314
315 top:
316     clrscr ();
317
318     /* Print a help line */
319     cputs (" d)ebug  h)ide   q)uit   s)how   j)ail");
320
321     gotoxy (1, 20);
322     cprintf ("SP: $%04X", getsp());
323
324     /* Put a cross at the center of the screen. */
325     gotoxy (width / 2 - 3, height / 2 - 1);
326 #if defined(__CBM__)
327     cprintf ("%3u,%3u\r\n%*s\xDB", width / 2 * 8 + 4, height / 2 * 8 + 4,
328              width / 2, "");
329 #else
330     cprintf ("%3u,%3u\r\n%*s+", width / 2 * 8 + 4, height / 2 * 8 + 4,
331              width / 2, "");
332 #endif
333
334     /* Test loop */
335     ShowState (Jailed, Invisible);
336     do {
337         /* Get the current co-ordinates and button states; and, print them. */
338         mouse_info (&info);
339         gotoxy (0, 2);
340         cprintf (" X  = %3d\r\n", info.pos.x);
341         cprintf (" Y  = %3d\r\n", info.pos.y);
342         cprintf (" B1 = %c\r\n", (info.buttons & MOUSE_BTN_LEFT) ?
343 #ifdef __CBM__
344                  0x5F
345 #else
346                  'v'
347 #endif
348                  : '^');
349         cprintf (" B2 = %c", (info.buttons & MOUSE_BTN_RIGHT) ?
350 #ifdef __CBM__
351                  0x5F
352 #else
353                  'v'
354 #endif
355                  : '^');
356
357         /* Handle user input */
358         if (kbhit ()) {
359             cclearxy (1, 9, 23);
360             switch (tolower (C = cgetc ())) {
361 #ifndef NO_DEBUG
362                 case 'd':
363                     BREAK();
364
365                     /* The debugger might have changed the colors.
366                     ** Restore them.
367                     */
368 #ifdef __CBM__
369                     (void) bordercolor (COLOR_GRAY2);
370                     (void) bgcolor (COLOR_WHITE);
371                     (void) textcolor (COLOR_GRAY1);
372 #else
373                     (void) bordercolor (COLOR_BLUE);
374                     (void) bgcolor (COLOR_WHITE);
375                     (void) textcolor (COLOR_BLACK);
376 #endif
377
378                     /* The debugger changed the screen; restore it. */
379                     goto top;
380 #endif
381                 case 'h':
382                     mouse_hide ();
383                     ShowState (Jailed, ++Invisible);
384                     break;
385
386 #ifndef NO_JAIL
387                 case 'j':
388                     if (Jailed) {
389                         mouse_setbox (&full_box);
390                         Jailed = false;
391                     } else {
392                         small_box.minx = max (info.pos.x - 10, full_box.minx);
393                         small_box.miny = max (info.pos.y - 10, full_box.miny);
394                         small_box.maxx = min (info.pos.x + 10, full_box.maxx);
395                         small_box.maxy = min (info.pos.y + 10, full_box.maxy);
396                         mouse_setbox (&small_box);
397                         Jailed = true;
398                     }
399                     ShowState (Jailed, Invisible);
400                     break;
401 #endif
402                 case 's':
403                     mouse_show ();
404                     if (Invisible) {
405                         ShowState (Jailed, --Invisible);
406                     }
407                     break;
408
409                 case 'q':
410                     Done = true;
411                     break;
412
413                 default:
414                     gotoxy (1, 9);
415                     cprintf ("Spurious character: $%02X", C);
416             }
417         }
418     } while (!Done);
419
420 #if DYN_DRV
421     /* Uninstall and unload the driver. */
422     CheckError ("mouse_unload", mouse_unload ());
423 #else
424     /* Uninstall the static driver. */
425     CheckError ("mouse_uninstall", mouse_uninstall ());
426 #endif
427
428     /* Say goodbye */
429     cputsxy (0, height / 2 + 3, "Goodbye!");
430     return EXIT_SUCCESS;
431 }