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