選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

285 行
7.2KB

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

Powered by TurnKey Linux.