home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 424.lha / joy / joy.c < prev    next >
C/C++ Source or Header  |  1990-09-30  |  5KB  |  198 lines

  1.  
  2. #include <intuition/intuition.h>
  3. #include <intuition/intuitionbase.h>
  4. #include <exec/exec.h>
  5. #include <devices/gameport.h>
  6. #include <devices/input.h>
  7. #include <proto/intuition.h>
  8. #include <proto/exec.h>
  9.  
  10. void doinit(),qread(),dojoy(),dodie(),cleanup();
  11. void fakeevent();
  12. ULONG joymask,diemask;
  13. struct MsgPort *joyport,*iport,*diep;
  14. struct IOStdReq *joyreq,*inputreq,*diemsg;
  15. struct InputEvent jevent,fevent;
  16. struct GamePortTrigger jtrig = { GPTF_DOWNKEYS|GPTF_UPKEYS,0,1,1 };
  17. struct IntuitionBase *IntuitionBase;
  18. int error;
  19. int go=1;
  20. BYTE ctype;
  21. BYTE mytype=GPCT_RELJOYSTICK;
  22. ULONG signals;
  23. WORD x,y;
  24. BOOL fast=FALSE;
  25. BOOL down=FALSE;
  26. BOOL mnu=FALSE;
  27. ULONG tick;
  28. WORD step=3;
  29.  
  30. main(argc,argv)
  31. {
  32.     doinit();
  33.     qread();
  34.     while(go)
  35.     {
  36.         signals=Wait(joymask|diemask);
  37.         if (signals & joymask) dojoy();
  38.         if (signals & diemask) dodie();
  39.     }
  40.     cleanup(50);
  41. }
  42.  
  43.  
  44. void dojoy()
  45. {
  46.     UWORD code;
  47.     GetMsg((struct MsgPort *)joyport);
  48.     code=jevent.ie_Code;
  49.     if ((code==IECODE_LBUTTON)&(down==TRUE)) code=IECODE_NOBUTTON;
  50.     x=jevent.ie_X;
  51.     y=jevent.ie_Y;
  52.     if((jevent.ie_TimeStamp.tv_secs/60) < 4000 ) {
  53.         tick++;
  54.         if (tick==10) step=9;
  55.         if (tick==40) step=15;
  56.     }
  57.     else {
  58.         tick=0;
  59.         step=3;
  60.     }
  61.     if ((code==IECODE_LBUTTON) & (IntuitionBase->MouseY<2)) {
  62.         code=IECODE_RBUTTON;
  63.         mnu=TRUE;
  64.     }
  65.     else {
  66.         if (code==IECODE_LBUTTON+IECODE_UP_PREFIX) {
  67.             down=FALSE;
  68.             if (mnu==TRUE) {
  69.                 code=IECODE_RBUTTON+IECODE_UP_PREFIX;
  70.                 mnu=FALSE;
  71.             }
  72.         }
  73.         else if (code==IECODE_LBUTTON) down=TRUE;
  74.     }
  75.     fakeevent(code,x,y);
  76.     qread();
  77. }
  78.  
  79.  
  80. void fakeevent(xcode,xx,xy)
  81. UWORD xcode;
  82. WORD xx,xy;
  83. {
  84.     fevent.ie_Code=xcode;
  85.     fevent.ie_NextEvent=NULL;
  86.     fevent.ie_Class=IECLASS_RAWMOUSE;
  87.     fevent.ie_Qualifier=IEQUALIFIER_RELATIVEMOUSE;
  88.     fevent.ie_X=xx*step;
  89.     fevent.ie_Y=xy*step;
  90.     inputreq->io_Command=IND_WRITEEVENT;
  91.     inputreq->io_Data=(APTR)&fevent;
  92.     inputreq->io_Flags=0;
  93.     inputreq->io_Length=sizeof(struct InputEvent);
  94.     DoIO((struct IORequest *)inputreq);
  95. }
  96.  
  97. void qread()
  98. {
  99.     joyreq->io_Command=GPD_READEVENT;
  100.     joyreq->io_Data=(APTR)&jevent;
  101.     joyreq->io_Length=sizeof(struct InputEvent);
  102.     SendIO((struct IORequest *)joyreq);
  103. }
  104.  
  105. void dodie()
  106. {
  107.     struct IOStdReq *msg;
  108.     msg=(struct IORequest *)GetMsg((struct MsgPort *)diep);
  109.     ReplyMsg((struct IORequest  *)msg);
  110.     go=0;
  111. }
  112.  
  113. void cleanup(level)
  114. int level;
  115. {
  116.     BYTE utype=GPCT_NOCONTROLLER;
  117.     switch(level)
  118.     {
  119.         case 50:
  120.           DeletePort((struct MsgPort *)diep);
  121.           AbortIO((struct IORequest *)joyreq);
  122.           WaitIO((struct IORequest *)joyreq);
  123.           joyreq->io_Command=GPD_SETCTYPE;
  124.           joyreq->io_Data=(APTR)&utype;
  125.           joyreq->io_Length=1;
  126.           DoIO((struct IORequest *)joyreq);
  127.         case 45:
  128.           CloseDevice((struct IORequest *)inputreq);
  129.         case 40:
  130.           CloseDevice((struct IORequest *)joyreq);
  131.         case 35:
  132.           DeleteStdIO(inputreq);
  133.         case 30:
  134.           DeleteStdIO(joyreq);
  135.         case 25:
  136.           DeletePort((struct MsgPort *)iport);
  137.         case 20:
  138.           DeletePort((struct MsgPort *)joyport);
  139.         case 15:
  140.         case 10:
  141.           CloseLibrary((struct Library *)IntuitionBase);
  142.         case 5:
  143.           exit(1);
  144.     }
  145. }
  146.  
  147. void doinit()
  148. {
  149.     struct MsgPort *p,*mp;
  150.     struct IOStdReq *tmsg;
  151.     if(p=FindPort("pmdjoymouse")) {
  152.         if(mp=(struct MsgPort *)CreatePort("pmdjm",0)) {
  153.             if(tmsg=(struct IOStdReq *)CreateStdIO(mp)) {
  154.                 PutMsg(p,tmsg);
  155.                 WaitPort((struct MsgPort *)mp);
  156.                 DeleteStdIO(tmsg);
  157.             }
  158.             DeletePort((struct MsgPort *)mp);
  159.         }
  160.         cleanup(5);
  161.     }
  162.     else {
  163.         if(!(diep=(struct MsgPort *)CreatePort("pmdjoymouse",0))) cleanup(5);
  164.         diemask=1<<diep->mp_SigBit;
  165.     }
  166.     if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",33))) cleanup(5);
  167.     if(!(joyport=(struct MsgPort *)CreatePort(NULL,0))) cleanup(15);
  168.     if(!(iport=(struct MsgPort *)CreatePort(NULL,0))) cleanup(20);
  169.     if(!(joyreq=CreateStdIO(joyport))) cleanup(25);
  170.     if(!(inputreq=CreateStdIO(iport))) cleanup(30);
  171.     if(error=OpenDevice("gameport.device",1,(struct IORequest *)joyreq,0)) cleanup(35);
  172.     if(error=OpenDevice("input.device",0,(struct IORequest *)inputreq,0)) cleanup(40);
  173.     joyreq->io_Command=GPD_ASKCTYPE;
  174.     joyreq->io_Data=(APTR)&ctype;
  175.     joyreq->io_Length=1;
  176.     error=0;
  177.     Forbid();
  178.     DoIO((struct IORequest *)joyreq);
  179.     if (ctype==GPCT_NOCONTROLLER) {
  180.         joyreq->io_Command=GPD_SETCTYPE;
  181.         joyreq->io_Data=(APTR)&mytype;
  182.         joyreq->io_Length=1;
  183.         DoIO((struct IORequest *)joyreq);
  184.     }
  185.     else error=1;
  186.     Permit();
  187.     if (error) cleanup(45);
  188.     joyreq->io_Command=GPD_SETTRIGGER;
  189.     joyreq->io_Data=(APTR)&jtrig;
  190.     joyreq->io_Length=(LONG)sizeof(struct GamePortTrigger);
  191.     DoIO((struct IORequest *)joyreq);
  192.     joyreq->io_Command=CMD_CLEAR;
  193.     DoIO((struct IORequest *)joyreq);
  194.     joymask=1<<joyport->mp_SigBit;
  195. }
  196.  
  197.  
  198.