home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff229.lzh / Shuffle / Shuffle.c < prev    next >
C/C++ Source or Header  |  1989-07-20  |  5KB  |  102 lines

  1. /******************************************************************
  2.  *
  3.  *              Shuffle - a basic screen shuffler
  4.  *
  5.  *  This program is a simple screen shuffler program.  It redefines
  6.  *  the Left-Amiga-M sequence to push the FRONT screen to the back,
  7.  *  rather than pushing the Workbench screen to the back, as is the
  8.  *  original Intuition definition.
  9.  *
  10.  ******************************************************************/
  11. #include <intuition/intuitionbase.h>
  12. #include <devices/input.h>
  13. #include <proto/exec.h>
  14. #include <proto/intuition.h>
  15.  
  16. #define PORTNAME "-=+RBE_Shuffle+=-"
  17.  
  18. struct Interrupt handlerptr;           /* int struct to install handler */
  19. struct MsgPort *inputport, *replyport; /* reply port for kill message   */
  20. struct IOStdReq *inputreq;             /* IO req for input device       */
  21. struct Message kill;                   /* msg to kill running prog      */
  22. extern struct IntuitionBase *IntuitionBase;
  23. extern void hndcode();                 /* actual handler code (assembly) */
  24.  
  25. void _main()         /* _main instead of main to save space (Lattice) */
  26. {
  27.   inputport = FindPort(PORTNAME);
  28.   if (inputport == NULL)            /*  does port already exist?    */
  29.   {                                 /*  no - install input handler  */
  30.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  31.     inputport = CreatePort(PORTNAME,0);
  32.     if (IntuitionBase== NULL || inputport==NULL)
  33.       exit(20);                         /* bomb if can't open */
  34.     inputreq = CreateStdIO(inputport);  /* IO req to input device */
  35.     if (inputreq == NULL)
  36.        exit(21);
  37.     if (OpenDevice("input.device",0,(struct IORequest *)inputreq,0) != NULL)
  38.        exit(22);                        /* bomb if can't open input dev */
  39.  
  40.     handlerptr.is_Code = hndcode;       /* point interrupt structure to   */
  41.     handlerptr.is_Data = NULL;          /* handler code (extern asm func) */
  42.     handlerptr.is_Node.ln_Pri = 127;    /* set to maximum priority        */
  43.  
  44.     inputreq->io_Command = IND_ADDHANDLER;  /* set up IO request to install */
  45.     inputreq->io_Data = (APTR)&handlerptr;  /* input handler in the input   */
  46.     DoIO((struct IORequest *)inputreq);     /* stream                       */
  47.  
  48. /*  At this point, the input handler is installed and doing its thing.
  49.     However, we cannot leave because the actual handler code would be
  50.     unloaded and the system would crash.  So, we will patiently wait, 
  51.     possibly forever, or until someone sends us a message.             */
  52.     
  53.     ReplyMsg(WaitPort(inputport));    /*  wait for any message  */
  54.  
  55.     inputreq->io_Command = IND_REMHANDLER;  /* got one - start packing up  */
  56.     DoIO((struct IORequest *)inputreq);     /* de-install input handler    */
  57.     CloseDevice((struct IORequest *)inputreq); /*  clean up everything  */
  58.     DeleteStdIO(inputreq);
  59.     DeletePort(inputport);
  60.     CloseLibrary((struct Library *)IntuitionBase);
  61.     exit(0);                                   /*  all done - bye!  */
  62.   }
  63.  
  64.   else    /* the program already running - send message to kill it  */
  65.   {
  66.     replyport = CreatePort(0,0);        /* create reply port    */
  67.     if (replyport == NULL)
  68.       exit(20);                         /* bomb if can't create */
  69.     kill.mn_ReplyPort = replyport;      /* set reply port in kill msg    */
  70.     kill.mn_Length = 0;
  71.     PutMsg(inputport,&kill);            /* send message to first program */
  72.     while(WaitPort(replyport) != &kill) /* wait for reply                */
  73.     {}
  74.     DeletePort(replyport);              /* clean up */
  75.     exit(100);                          /* bye!     */
  76.   }
  77.     
  78. }
  79. /*****************************************************************
  80.   This code was originally used as the input handler.  I then took the
  81.   compiled code and optimized it in assembly (as hndcode()) for
  82.   maximum performance.  I leave the original C code, commented out, to
  83.   show what the handler is doing.
  84.  
  85. struct InputEvent *hndcode(ie,data)
  86. register struct InputEvent *ie;
  87. int data;
  88. {
  89.   if (ie->ie_Class == IECLASS_RAWKEY)  ** is input event a keystroke?     **
  90.     if ((ie->ie_Qualifier & IEQUALIFIER_LCOMMAND) != FALSE)   ** L-Amiga? **
  91.       if (ie->ie_Code == 0x37)         ** L-Amiga-M? **
  92.       {
  93.         Forbid();                      ** if so, push front screen to back **
  94.         ScreenToBack(IntuitionBase->FirstScreen);
  95.         Permit();
  96.         return(ie->ie_NextEvent);      ** return next event, if any        **
  97.       }
  98.   return(ie);                          ** else, pass event down the chain  **
  99. }
  100. *******************************************************************/
  101. void MemCleanup() {}     /* dummy stub to save more space (Lattice) */
  102.