home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 367.lha / netkeys_v2.0 / snetkeys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-02  |  5.7 KB  |  225 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1990 The Software Distillery.  All Rights Reserved.*/
  3. /* |. o.| || This program may not be distributed without the permission of   */
  4. /* | .  | || the authors.                                                    */
  5. /* | o  | ||                                                                 */
  6. /* |  . |// Written by Doug Walker                                           */
  7. /* ======          BBS:(919)-471-6436      VOICE:(919)-467-4764              */ 
  8. /*                 BIX: djwalker           USENET: ...!mcnc!rti!sas!walker   */
  9. /*                 405 B3 Gooseneck Dr, Cary, NC 27513, USA                  */
  10. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  11.  
  12. /* * * * * * * * * INCLUDE FILES * * * * * * * * * * * */
  13.  
  14. #include "netkeys.h"
  15. #include <proto/intuition.h>
  16.  
  17. struct DChannel *chan;
  18.  
  19. struct MsgPort *lisport;
  20.  
  21. int InitDevice(void);
  22. int TermDevice(void);
  23.  
  24. void _main(char *);
  25.  
  26. void _main(x)
  27. char *x;
  28. {
  29.    short swidth;
  30.    short exit;
  31.    int state;
  32.    struct Screen wbdat;
  33.    struct MsgPort  *inputDevPort = NULL;
  34.    struct IOStdReq *inputRequestBlock = NULL;
  35.    struct InputEvent ie;
  36.    struct IntuitionBase *IntuitionBase = NULL;
  37.    struct GfxBase *GfxBase = NULL;
  38.    UWORD *NoSprData = NULL;
  39.    ULONG rc;
  40.  
  41.    if (
  42.        InitDevice()                                               ||
  43.  
  44.        ((inputDevPort = CreatePort(0,0)) == NULL)                           ||
  45.  
  46.        ((inputRequestBlock = 
  47.                CreateIOReq(inputDevPort, sizeof(struct IOStdReq))) == NULL) ||
  48.  
  49.        !(IntuitionBase = (struct IntuitionBase *)
  50.                                        OpenLibrary("intuition.library", 0)) ||
  51.  
  52.        !(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0))   ||
  53.  
  54.        !(NoSprData = AllocMem(12, MEMF_PUBLIC|MEMF_CHIP|MEMF_CLEAR))        ||
  55.  
  56.       OpenDevice("input.device",0,(struct IORequest *)inputRequestBlock,0))
  57.    {
  58.       goto abort;
  59.    }
  60.  
  61.    NoSprData[0] = 0xFE00;
  62.    NoSprData[1] = 0xFF00;
  63.    
  64.  
  65.    /* If we have an appropriate version of intuition, ask for info */
  66.    /* about the workbench screen                                   */
  67.    if(IntuitionBase->LibNode.lib_Version < 33
  68.       || !(GetScreenData((char *)&wbdat, sizeof(struct Screen), 
  69.                               WBENCHSCREEN, NULL)))
  70.    {
  71.       /* Either we're running on an old version of the system, or the */
  72.       /* GetScreenData call failed for some reason.  In either case,  */
  73.       /* simply assume a 640x200 screen and be done with it.          */
  74.       swidth  = 639;
  75.    }
  76.    else
  77.    {
  78.       /* Use the wb width, and STDSCREENHEIGHT. */
  79.       swidth  = wbdat.Width-1;
  80.    }
  81.  
  82.    inputRequestBlock->io_Command = IND_WRITEEVENT;
  83.    inputRequestBlock->io_Flags   = 0;
  84.    inputRequestBlock->io_Length  = IESZ;
  85.    inputRequestBlock->io_Data    = (APTR)&ie;
  86.  
  87.    state = 0;
  88.    mshow(GfxBase, NoSprData, 0);
  89.    while(1)
  90.    {
  91.       rc = Wait(SIGBREAKF_CTRL_C | (1 << chan->port.mp_SigBit));
  92.  
  93.       if(rc & SIGBREAKF_CTRL_C)          /* Received ctrl-c */
  94.          goto abort;
  95.  
  96.       if(DRead(chan, (char *)&ie, IESZ) != IESZ)
  97.          goto abort;
  98.  
  99.       if(ie.ie_Class == IECLASS_NKCONTROL) /* Special 'control' record */
  100.       {
  101.          exit = ie.ie_Code;
  102.          if(ie.ie_Code & IECODE_NKHOTQUIT)
  103.          {
  104.             mshow(GfxBase, NoSprData, 0);
  105.             state = 0;
  106.             continue;
  107.          }
  108.  
  109.          if(!(ie.ie_Code & IECODE_NKHOTKEY))
  110.          {
  111.             IntuitionBase->MouseX = (ie.ie_X == 0 ? swidth-1 : 1);
  112.             IntuitionBase->MouseY = ie.ie_Y;
  113.          }
  114.  
  115.          state = 1;
  116.          mshow(GfxBase, NoSprData, 1);
  117.          continue;
  118.       }
  119.       else if(state == 0)
  120.          continue;
  121.  
  122.       if(
  123.          ie.ie_Class == IECLASS_RAWMOUSE &&
  124.          (ie.ie_Qualifier & IEQUALIFIER_RELATIVEMOUSE) &&
  125.          ie.ie_Code == IECODE_NOBUTTON &&
  126.          ( 
  127.            (
  128.               (exit & IECODE_NKRIGHT)          &&
  129.               ie.ie_X < 0                     &&
  130.               IntuitionBase->MouseX == 0
  131.            )
  132.            ||
  133.            (
  134.               (exit & IECODE_NKLEFT)           &&
  135.               ie.ie_X > 0                     &&
  136.               IntuitionBase->MouseX == swidth
  137.            )
  138.          )
  139.         )
  140.       {
  141.          mshow(GfxBase, NoSprData, 0);
  142.          ie.ie_X = IntuitionBase->MouseX;
  143.          ie.ie_Y = IntuitionBase->MouseY;
  144.          ie.ie_Class = IECLASS_NKCONTROL;
  145.          DWrite(chan, (char *)&ie, IESZ);
  146.          state = 0;
  147.          continue;
  148.       }
  149.  
  150.       ie.ie_NextEvent = NULL;
  151.       DoIO((struct IORequest *)inputRequestBlock);
  152.    }
  153.  
  154. abort:
  155.    if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  156.  
  157.    if(GfxBase)
  158.    {
  159.       if(NoSprData)
  160.       {
  161.          mshow(GfxBase, NoSprData, 1);
  162.          FreeMem(NoSprData, 12);
  163.       }
  164.       CloseLibrary((struct Library *)GfxBase);
  165.    }
  166.  
  167.    TermDevice();
  168.  
  169.    if (inputRequestBlock != NULL)
  170.    {
  171.       if (inputRequestBlock->io_Device != NULL)
  172.          CloseDevice((struct IORequest *)inputRequestBlock);
  173.       DeleteIOReq(inputRequestBlock);
  174.    }
  175.  
  176.    if (inputDevPort != NULL)  DeletePort(inputDevPort);
  177. }
  178.  
  179. int InitDevice(void)
  180. {
  181.    struct MsgPort *myport;
  182.    struct Message *msg;
  183.  
  184.    chan = NULL;
  185.    lisport = NULL;
  186.  
  187.    myport = &(((struct Process *)FindTask(0L))->pr_MsgPort);
  188.    lisport = DListen(PORT_NETKEYS);
  189.    do
  190.    {
  191.       WaitPort(myport);
  192.    }
  193.    while(!(msg=GetMsg(myport)));
  194.  
  195.    ReplyMsg(msg);
  196.  
  197.    if(!lisport) return(1);
  198.  
  199.    Wait(1<<lisport->mp_SigBit);
  200.    
  201.    if(!(chan = (struct DChannel *)DAccept(lisport)))
  202.       return(1);
  203.  
  204.    return(0);
  205. }
  206.  
  207. int TermDevice(void)
  208. {
  209.    if(chan)
  210.    {
  211.       DClose(chan);
  212.       chan = NULL;
  213.    }
  214.  
  215.    if(lisport)
  216.    {
  217.       DUnListen(lisport);
  218.       lisport = NULL;
  219.    }
  220.  
  221.    return(0);
  222. }
  223.  
  224. void MemCleanup(void);
  225. void MemCleanup(){}