home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d511 / onekey.lha / OneKey / onekey.c < prev    next >
C/C++ Source or Header  |  1991-07-20  |  7KB  |  277 lines

  1. ;/* onekey.c - Execute me to compile me with Lattice 5.04
  2.  
  3. LC -b0 -cfistq -v -y -j73 onekey.c
  4. Blink FROM LIB:Astartup.obj,handint.o,onekey.o TO onekey LIBRARY LIB:Amiga.lib,LIB:LC.lib SMALLCODE BATCH ND
  5. quit
  6.  
  7. NOTE - if you use c.o startup, link with LC.lib first, and include stdio.h
  8.  
  9. */
  10.  
  11. #include <exec/types.h>
  12.  
  13. #include <exec/types.h>
  14. #include <exec/ports.h>
  15. #include <exec/memory.h>
  16. #include <exec/io.h>
  17. #include <exec/tasks.h>
  18. #include <exec/interrupts.h>
  19. #include <exec/devices.h>
  20. #include <devices/input.h>
  21. #include <devices/inputevent.h>
  22. #include <libraries/dos.h>
  23. #include <libraries/dosextens.h>
  24. #include <workbench/startup.h>
  25.  
  26. #ifdef LATTICE
  27. #include <clib/exec_protos.h>
  28. #include <clib/dos_protos.h>
  29. #include <clib/intuition_protos.h>
  30. #include <clib/graphics_protos.h>
  31. #include <clib/alib_protos.h>
  32. #include <clib/alib_stdio_protos.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  36. int chkabort(void) { return(0); }  /* really */
  37. #endif
  38.  
  39.  
  40. #define MINARGS 1
  41.  
  42. UBYTE *vers = "\0$VER: onekey 36.11";
  43. UBYTE *Copyright = 
  44.   "onekey v36.11\nFreely Redistributable, by Carolyn Scheppner - CATS";
  45. UBYTE *usage = "Usage: [run >NIL:] onekey (toggles on/off)\nInput handler for one-key-at-a-time typists\n";
  46.  
  47. /**********************************************************************/
  48. /* Debug control                                                      */
  49. /*                                                                    */
  50. /* The first define converts any printfs that got in by mistake into  */
  51. /* kprintfs. If you are debuging to the console you can change        */
  52. /* kprintfs into printfs.                                             */
  53. /* The D1(x) define controls debugging in the standard modules. Use   */
  54. /* The D(x) macro for debugging in the app.c module.                  */
  55. /**********************************************************************/
  56. void kprintf(char *,...);
  57.  
  58. /*
  59. #define DEBUG
  60. */
  61. #ifdef DEBUG
  62. #define bug kprintf
  63. #define D1(x) x
  64. #define D(x)  x
  65. #else
  66. #define D1(x) ;
  67. #define D(x)  ;
  68. #endif /* NO DEBUG */
  69.  
  70.  
  71. BOOL FromWb;
  72.  
  73. #define KCNT    7
  74. #define CODE_LSHIFT    0x60
  75. #define CODE_RSHIFT    0x61
  76. #define CODE_CONTROL    0x63
  77. #define CODE_LALT    0x64
  78. #define CODE_RALT    0x65
  79. #define CODE_LAMIGA    0x66
  80. #define CODE_RAMIGA    0x67
  81.  
  82. UWORD kcodes[] = { CODE_LSHIFT, CODE_RSHIFT,
  83.            CODE_LALT,   CODE_RALT,
  84.            CODE_LAMIGA, CODE_RAMIGA,
  85.            CODE_CONTROL };
  86.  
  87. UWORD kquals[] = { IEQUALIFIER_LSHIFT,   IEQUALIFIER_RSHIFT,
  88.            IEQUALIFIER_LALT,     IEQUALIFIER_RALT,
  89.            IEQUALIFIER_LCOMMAND, IEQUALIFIER_RCOMMAND,
  90.            IEQUALIFIER_CONTROL };
  91.  
  92.  
  93. UWORD kqual = 0;
  94.  
  95. void bye(UBYTE *s, int e);
  96. void cleanup(void);
  97. void unInstall(void);
  98.  
  99. extern void   HandlerInterface();  /* assembler interface */
  100. struct InputEvent far *myhandler(struct InputEvent *ie1, APTR data);
  101.  
  102. int    openedID = NULL;
  103. struct MsgPort   *inputPort = NULL;
  104. struct IOStdReq  *inputReq = NULL;
  105.  
  106. #define MAXIE    16
  107. int ii = 0;
  108. struct InputEvent ups[MAXIE] = {NULL};
  109.  
  110. UBYTE *progname = "OneKey";
  111. UBYTE *oldname = NULL;
  112.  
  113. struct Interrupt handlerStuff = {0};
  114.  
  115.  
  116. /* C handler routine */
  117.  
  118. struct InputEvent *myhandler(struct InputEvent *ie1, APTR data)
  119.     {
  120.     struct InputEvent    *ie, *myie, *lastie;
  121.     extern struct Library *SysBase;
  122.     UWORD  kcode;
  123.     int    k;
  124.     BOOL   IsDown, IsUp, SendEm;
  125.  
  126.  
  127.     SendEm = FALSE;
  128.  
  129.     for (ie=ie1; ie; lastie=ie, ie=ie->ie_NextEvent)
  130.     {
  131.     if (ie->ie_Class==IECLASS_RAWKEY)
  132.         {
  133.         kcode = ie->ie_Code & (~IECODE_UP_PREFIX);
  134.         for(k=0,IsUp=FALSE,IsDown=FALSE; k<KCNT; k++)
  135.         {
  136.         if(kcode == kcodes[k])
  137.             {
  138.             if(ie->ie_Code & IECODE_UP_PREFIX) IsUp = TRUE;
  139.             else IsDown = TRUE;
  140.             break;
  141.             }
  142.         }
  143.  
  144.         D(bug("got code $%lx, IsUp=%ld, IsDown=%ld\n",kcode,IsUp,IsDown));
  145.         if(IsDown)    /* is a gathered key - gather the quals */
  146.             {
  147.             D(bug("got down  k=%ld\n",k));
  148.             kqual |= kquals[k];
  149.            }
  150.         else if(IsUp)    /* release of a gathered key - we'll eat it */
  151.             {
  152.             D(bug("got up  k=%ld\n",k));
  153.  
  154.             myie = &ups[ii];        /* use my next empty event */
  155.         *myie = *ie;            /* clone this key up event  */
  156.         if(ii < (MAXIE-1)) ii++;    /* set up for next */
  157.             ie->ie_Class = IECLASS_NULL;    /* null out the real event */
  158.             }
  159.         else if(kqual)    /* got a non-qualifier key and we have gathered */
  160.             {
  161.             D(bug("got key  code=%lx, gathered=$%lx\n",kcode,kqual));
  162.             ie->ie_Qualifier |= kqual;    /* apply the qualifiers */
  163.  
  164.             /* if key release, set up fake saved keyups now too, clear gather */
  165.             if(ie->ie_Code & IECODE_UP_PREFIX)
  166.             {
  167.             /* update time stamps and link our list */
  168.             for(k=0; k<ii; k++)
  169.             {
  170.             ups[k].ie_TimeStamp.tv_secs  = ie->ie_TimeStamp.tv_secs;
  171.             ups[k].ie_TimeStamp.tv_micro = ie->ie_TimeStamp.tv_micro+k;
  172.             if(k<(ii-1)) ups[k].ie_NextEvent = &ups[k+1];
  173.             else ups[k].ie_NextEvent = NULL;
  174.             }
  175.             SendEm = TRUE;
  176.  
  177.             kqual = 0;
  178.             }
  179.         }
  180.         }
  181.     }
  182.     if(SendEm)
  183.     {
  184.     lastie->ie_NextEvent = &ups[0];
  185.     ii = 0;
  186.     }
  187.     return(ie1);
  188.     }
  189.  
  190.  
  191. #define MYNAME "cas_cbm_onekey"
  192.  
  193. void main(int argc, char **argv)
  194.     {
  195.     struct Task *task;
  196.     int  error;
  197.  
  198.     FromWb = argc ? FALSE : TRUE;
  199.  
  200.     if(((argc)&&(argc<MINARGS))||(argv[argc-1][0]=='?'))
  201.     {
  202.     printf("%s\n%s\n",Copyright,usage);
  203.     bye("",RETURN_OK);
  204.     }
  205.  
  206.     if(task=FindTask(MYNAME))
  207.     {
  208.     if(!FromWb) printf("Signalling OneKey to remove itself\n");
  209.     Signal(task,SIGBREAKF_CTRL_C);
  210.     bye("",RETURN_OK);
  211.     }
  212.  
  213.     task = FindTask(NULL);
  214.     oldname = task->tc_Node.ln_Name;
  215.     task->tc_Node.ln_Name = MYNAME;
  216.  
  217.  
  218.    /* Get ready to add our handler */
  219.    if(!(inputPort=(struct MsgPort *)CreatePort(0,0)))
  220.       bye("Can't create port\n",RETURN_FAIL);
  221.  
  222.    if(!(inputReq=(struct IOStdReq *)CreateStdIO(inputPort)))
  223.       bye("Can't create IORequest\n",RETURN_FAIL);
  224.  
  225.  
  226.  
  227.    handlerStuff.is_Data = NULL;              /* no passed data pointer */
  228.    handlerStuff.is_Code = HandlerInterface;  /* assem entry */
  229.    handlerStuff.is_Node.ln_Pri = 51;         /* above Intuition */
  230.  
  231.    if(error = OpenDevice("input.device",0,inputReq,0))
  232.       bye("Can't open input device\n",RETURN_FAIL);
  233.    openedID = 1;
  234.  
  235.    inputReq->io_Command = IND_ADDHANDLER;
  236.    inputReq->io_Data = (APTR)&handlerStuff;
  237.       
  238.    DoIO(inputReq);
  239.  
  240.    if(argc) printf("%s installed\n",progname);
  241.  
  242.    Wait(SIGBREAKF_CTRL_C);
  243.  
  244.    bye("",0L);
  245.    }
  246.  
  247.  
  248. void bye(UBYTE *s, int e)
  249.    {
  250.    if(!FromWb) printf(s);
  251.    unInstall();
  252.    exit(e);
  253.    }
  254.  
  255. void unInstall()
  256.    {
  257.    if (openedID)
  258.       {
  259.       /* remove the handler from the chain */
  260.       inputReq->io_Command = IND_REMHANDLER;
  261.       inputReq->io_Data = (APTR)&handlerStuff;
  262.       DoIO(inputReq);
  263.       /* close the input device */
  264.       CloseDevice(inputReq);
  265.       }
  266.  
  267.    /* delete the IO request, port, FreeSignal */
  268.    if(inputReq)        DeleteStdIO(inputReq);
  269.    if(inputPort)    DeletePort(inputPort);
  270.    if(oldname)        FindTask(NULL)->tc_Node.ln_Name = oldname;
  271.    if((openedID)&&(!FromWb)) printf("%s removed\n",progname);
  272.    }
  273.  
  274.  
  275.  
  276.  
  277.