home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 499.lha / GamePortDeviceToolKit / gameport.lzh / src / DeviceToolKits / GamePort / test_proto / test8.c < prev    next >
C/C++ Source or Header  |  1991-01-03  |  6KB  |  228 lines

  1. #include <stdio.h>
  2.  
  3. #include "DeviceToolKits/GamePort.h"
  4. #ifndef  TESTPROTO
  5. #define  NO_PRAGMAS  1
  6. #endif
  7. #include "DeviceToolKits/proto/GamePort.h"
  8. #include "DeviceToolKits/GamePortBase.h"
  9.  
  10. DTGamePort_t               Unit1;
  11. DTGamePort_t               Unit1Copy;
  12. BYTE                       type;
  13. struct   GamePortTrigger   trigger;
  14. struct   InputEvent        buffer;
  15. ULONG                      length;
  16.  
  17. main(argc,argv)
  18. int   argc;
  19. char  *argv[];
  20. {
  21.    long  status;
  22.  
  23. #ifdef   TESTSHARED
  24. /*  Open the GamePort library  */
  25.  
  26.    DTGamePortOpen(DTGAMEPORTREV);
  27. #endif
  28.  
  29. /*  Try to initialize unit 1 of the GamePort  */
  30.  
  31.    if ((Unit1 = DTGamePortCreate(1L,&status)) == NULL) {
  32.       fprintf(stderr,"Error initializing unit 1 = %ld.\n",status);
  33.       fflush(stderr);
  34.       goto cleanup;
  35.    }
  36.  
  37.    fprintf(stderr,"Unit1 = %lX\n",(long) Unit1);
  38.    fflush(stderr);
  39.  
  40. /*  Make a copy of the control structure  */
  41.  
  42.    if ((Unit1Copy = DTGamePortCreateLike(Unit1,&status)) == NULL) {
  43.       fprintf(stderr,"Error copying unit 1 = %ld.\n",status);
  44.       fflush(stderr);
  45.       goto cleanup;
  46.    }
  47.  
  48.    fprintf(stderr,"Unit1Copy = %lX\n",(long) Unit1Copy);
  49.    fflush(stderr);
  50.  
  51. /*  Find out what kind of controller is on unit 1  */
  52.  
  53.    if ((status = DTGamePortGetType(Unit1,&type)) != 0) {
  54.       fprintf(stderr,"Error getting unit 1 type = %ld,%ld.\n",
  55.                status,Unit1->gp_error);
  56.       fflush(stderr);
  57.       goto cleanup;
  58.    }
  59.  
  60.    printf("Unit 1 controller type = %d.\n",type);
  61.  
  62. /*  Set the controller for an absolute joystick  */
  63.  
  64.    type = GPCT_ABSJOYSTICK;
  65.    if ((status = DTGamePortSetType(Unit1,&type)) != 0) {
  66.       fprintf(stderr,"Error setting unit 1 type = %ld,%ld.\n",
  67.                status,Unit1->gp_error);
  68.       fflush(stderr);
  69.       goto cleanup;
  70.    }
  71.  
  72. /*  Find out what kind of controller is on unit 1  */
  73.  
  74.    if ((status = DTGamePortGetType(Unit1Copy,&type)) != 0) {
  75.       fprintf(stderr,"Error getting unit 1 type = %ld,%ld.\n",
  76.                status,Unit1Copy->gp_error);
  77.       fflush(stderr);
  78.       goto cleanup;
  79.    }
  80.  
  81.    printf("Unit 1 controller type = %d.\n",type);
  82.  
  83. /*  Find out what kind of trigger is on unit 1  */
  84.  
  85.    if ((status = DTGamePortGetTrigger(Unit1,&trigger)) != 0) {
  86.       fprintf(stderr,"Error getting unit 1 trigger = %ld,%ld.\n",
  87.                status,Unit1->gp_error);
  88.       fflush(stderr);
  89.       goto cleanup;
  90.    }
  91.  
  92.    printf("Keys    = %x\n",trigger.gpt_Keys);
  93.    printf("Timeout = %d\n",trigger.gpt_Timeout);
  94.    printf("XDelta  = %d\n",trigger.gpt_XDelta);
  95.    printf("YDelta  = %d\n",trigger.gpt_YDelta);
  96.  
  97. /*  Set the trigger for an absolute joystick  */
  98.  
  99.    trigger.gpt_Keys = GPTF_UPKEYS | GPTF_DOWNKEYS;
  100.    trigger.gpt_Timeout = 0;
  101.    trigger.gpt_XDelta = 1;
  102.    trigger.gpt_YDelta = 1;
  103.  
  104.    if ((status = DTGamePortSetTrigger(Unit1,&trigger)) != 0) {
  105.       fprintf(stderr,"Error setting unit 1 trigger = %ld,%ld.\n",
  106.                status,Unit1->gp_error);
  107.       fflush(stderr);
  108.       goto cleanup;
  109.    }
  110.  
  111. /*  Find out what kind of trigger is on unit 1  */
  112.  
  113.    if ((status = DTGamePortGetTrigger(Unit1Copy,&trigger)) != 0) {
  114.       fprintf(stderr,"Error getting unit 1 trigger = %ld,%ld.\n",
  115.                status,Unit1Copy->gp_error);
  116.       fflush(stderr);
  117.       goto cleanup;
  118.    }
  119.  
  120.    printf("Keys    = %x\n",trigger.gpt_Keys);
  121.    printf("Timeout = %d\n",trigger.gpt_Timeout);
  122.    printf("XDelta  = %d\n",trigger.gpt_XDelta);
  123.    printf("YDelta  = %d\n",trigger.gpt_YDelta);
  124.  
  125. /*  Sit around waiting for events  */
  126.  
  127.    if ((status = DTGamePortClear(Unit1)) != 0) {
  128.       fprintf(stderr,"Error clearing unit 1 = %ld,%ld.\n",
  129.                status,Unit1->gp_error);
  130.       fflush(stderr);
  131.       goto cleanup;
  132.    }
  133.  
  134.    if ((status = DTGamePortQueueRead(Unit1,(BYTE *) &buffer,
  135.          (ULONG) sizeof(buffer))) != 0) {
  136.       fprintf(stderr,"Error queueing unit 1 = %ld,%ld.\n",
  137.                status,Unit1->gp_error);
  138.       fflush(stderr);
  139.       goto cleanup;
  140.    }
  141.  
  142.    while (1) {
  143.       WaitPort(Unit1->gp_rport);
  144.       status = DTGamePortCheckRead(Unit1,&length);
  145.       if (status != 0) {
  146.          fprintf(stderr,"Error reading unit 1 = %ld,%ld.\n",
  147.                   status,Unit1->gp_error);
  148.          fflush(stderr);
  149.          goto cleanup;
  150.       }
  151.       if (length != 0) {
  152.          if (eventprint(&buffer) < 0) {
  153.             break;
  154.          }
  155.          if ((status = DTGamePortQueueRead(Unit1,(BYTE *) &buffer,
  156.                (ULONG) sizeof(buffer))) != 0) {
  157.             fprintf(stderr,"Error queueing unit 1 = %ld,%ld.\n",
  158.                      status,Unit1->gp_error);
  159.             fflush(stderr);
  160.             goto cleanup;
  161.          }
  162.       }
  163.    }
  164.  
  165. /*  Free the stuff  */
  166.  
  167. cleanup:
  168.    DTGamePortFree(Unit1);
  169.    DTGamePortFree(Unit1Copy);
  170. #ifdef   TESTSHARED
  171.    DTGamePortClose();
  172. #endif
  173. }
  174.  
  175. int   button_count = 0;
  176.  
  177. #ifdef   ANSI_PROTO
  178. eventprint(struct InputEvent *event)
  179. #else
  180. eventprint(event)
  181. struct   InputEvent  *event;
  182. #endif
  183.  
  184. {
  185.    if (event->ie_Code == IECODE_LBUTTON) {
  186.       printf("BUTTON PRESSED ");
  187.    }
  188.    if (event->ie_Code == (IECODE_LBUTTON | IECODE_UP_PREFIX)) {
  189.       printf("BUTTON RELEASED ");
  190.    }
  191.    switch (event->ie_Y) {
  192.       case -1:
  193.          printf("FORWARD ");
  194.          break;
  195.  
  196.       case 1:
  197.          printf("BACKWARD ");
  198.          break;
  199.    }
  200.    switch (event->ie_X) {
  201.       case -1:
  202.          printf("LEFT");
  203.          break;
  204.  
  205.       case 1:
  206.          printf("RIGHT");
  207.          break;
  208.    }
  209.    if (event->ie_X == 0 && event->ie_Y == 0) {
  210.       printf("CENTERED");
  211.    }
  212.    else {
  213.       button_count = 0;
  214.    }
  215.    printf("\n");
  216.  
  217.    if (event->ie_Code == IECODE_LBUTTON && event->ie_X == 0 &&
  218.          event->ie_Y == 0) {
  219.       button_count++;
  220.       if (button_count > 3) {
  221.          return (-1);
  222.       }
  223.    }
  224.  
  225.    return (0);
  226. }
  227.  
  228.