home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff274.lzh / Snap / snap.c < prev    next >
C/C++ Source or Header  |  1989-11-16  |  9KB  |  290 lines

  1. /***********************************************\
  2. *                                               *
  3. *                     Snap                      *
  4. *           © Mikael Karlsson 1988              *
  5. *                                               *
  6. \***********************************************/
  7.  
  8. #include "snap.h"
  9.  
  10. /* signals */
  11. LONGBITS startsignal, insertsignal, cancelsignal, donesignal;
  12. LONGBITS movesignal, clicksignal, timersignal, initsignal;
  13. ULONG startsignum = -1L;
  14. ULONG insertsignum = -1L;
  15. ULONG cancelsignum = -1L;
  16. ULONG donesignum = -1L;
  17. ULONG movesignum = -1L;
  18. ULONG clicksignum = -1L;
  19. ULONG timersignum = -1L;
  20. ULONG initsignum = -1L;
  21. ULONG WaitSignal;
  22.  
  23. /* program */
  24. struct MsgPort *port = NULL;
  25. struct Task *MyTask;
  26.  
  27. /* Snap state machine */
  28. WORD action;
  29. WORD state;
  30. WORD StartUnit = UNIT_FRAME;
  31.  
  32. /* clipboard */
  33. struct IOClipReq *ClipReq = NULL;
  34. struct MsgPort *ClipPort = NULL;
  35.  
  36. /* input device */
  37. struct MsgPort *inputDevPort = NULL;
  38. struct Interrupt handlerStuff;
  39. struct IOStdReq *inputRequestBlock = NULL;
  40. struct InputEvent SimEvent;
  41. WORD Priority = 51; /* Default priority for input handler */
  42. WORD textqual = IEQUALIFIER_LCOMMAND;
  43. WORD gfxqual = IEQUALIFIER_RCOMMAND;
  44. WORD insertkey = 23;
  45. WORD xerox = 0;
  46.  
  47. UBYTE *CharData = NULL;
  48.  
  49. /* console */
  50. struct MsgPort *ConPort = NULL;
  51. struct IOStdReq *ConIOR = NULL;
  52. struct KeyMap keymap;
  53. LONG linedelay = 0;
  54. LONG chardelay = 0;
  55.  
  56. /* windows */
  57. struct MsgPort *Sharedport = NULL;
  58. SHORT Sharedrefs;
  59.  
  60. /* libraries */
  61. struct IntuitionBase *IntuitionBase = NULL;
  62. struct GfxBase       *GfxBase = NULL;
  63. struct LayersBase    *LayersBase = NULL;
  64. /* struct DosLibrary    *DosBase = NULL; */
  65.  
  66. /* graphics */
  67. struct Screen *theScreen;
  68. struct Layer *theLayer;
  69. struct Layer_Info *LockedLayerInfo = NULL;
  70. struct RastPort rp, TempRp, MyRP;
  71. struct BitMap TempBM, MyBM;
  72. UBYTE *TempRaster = NULL;
  73. UWORD CrawlPtrn = 0x7777;
  74. WORD FrameMask = 1;
  75.  
  76. /* detaching */
  77. ULONG _BackGroundIO = 0;
  78. ULONG _stack = 4096L;
  79. ULONG _priority = 4L;
  80. char *_procname = "Snap";
  81.  
  82. int isdigit(c)
  83. REGISTER char c;
  84. {
  85.     return (c>='0' && c<='9');
  86. }
  87.  
  88. long dectoint(str)
  89. REGISTER char *str;
  90. {
  91.     REGISTER long val = 0;
  92.     REGISTER char c;
  93.     while (isdigit(c = *str)) {
  94.         val = (((val<<2)+val)<<1) + c-'0';
  95.         str++;
  96.     }
  97.     return(val);
  98. }
  99.  
  100. long hextoint(str)
  101. REGISTER char *str;
  102. {
  103.     REGISTER long val = 0;
  104.     REGISTER char c;
  105.     while (c = *str) {
  106.         val <<= 4;
  107.         val |= (c & 15) + (isdigit(c) ? 0 : 9);
  108.         str++;
  109.     }
  110.     return(val);
  111. }
  112.  
  113. void main(argc, argv)
  114. WORD argc;
  115. char *argv[];
  116. {
  117.     int i;
  118.     Enable_Abort = 0;
  119.     i = 1; /* Don't parse program name */
  120.     while (i<argc) {
  121.         char *arg = argv[i];
  122.         if (arg[0] == '-') { /* Argument coming up */
  123.             switch(arg[1]) {
  124.                 case 'p': {  /* Priority */
  125.                     int pri = dectoint(&arg[2]);
  126.                     if (pri>50 && pri<128) {
  127.                         Priority = pri;
  128.                     }
  129.                     break;
  130.                 }
  131.                 case 't': {
  132.                     textqual = hextoint(&arg[2]);
  133.                     break;
  134.                 }
  135.                 case 'g': {
  136.                     gfxqual = hextoint(&arg[2]);
  137.                     break;
  138.                 }
  139.                 case 'i': {
  140.                     insertkey = hextoint(&arg[2]);
  141.                     break;
  142.                 }
  143.                 case 'c': {
  144.                     chardelay = dectoint(&arg[2]);
  145.                     break;
  146.                 }
  147.                 case 'l': {
  148.                     linedelay = dectoint(&arg[2]);
  149.                     break;
  150.                 }
  151.                 case 'a': {
  152.                     CrawlPtrn = hextoint(&arg[2]);
  153.                     break;
  154.                 }
  155.                 case 'x': {
  156.                     xerox = 1;
  157.                 }
  158.                 case 'u': {
  159.                     switch(dectoint(&arg[2])) {
  160.                         case 1: {
  161.                             StartUnit = UNIT_CHAR;
  162.                             break;
  163.                         }
  164.                         case 0:
  165.                         default: {
  166.                             StartUnit = UNIT_FRAME;
  167.                             break;
  168.                         }
  169.                     }
  170.                     break;
  171.                 }
  172.                 case 'b': {
  173.                     FrameMask = hextoint(&arg[2]);
  174.                     break;
  175.                 }
  176.                 default: {
  177.                     break;
  178.                 }
  179.             }
  180.         }
  181.         i++; /* Next argument, please */
  182.     }
  183.  
  184.     OpenStuff();
  185.  
  186.     /* This is what we're waiting for */
  187.     WaitSignal = startsignal | insertsignal | initsignal | cancelsignal |
  188.                  SIGBREAKF_CTRL_C | 1L << port->mp_SigBit;
  189.  
  190.     FOREVER {
  191.         REGISTER LONGBITS sig =
  192.           Wait(WaitSignal |
  193.                (Sharedport ? (1L << Sharedport->mp_SigBit) : 0L));
  194.         if (Sharedport) {
  195.             REGISTER SHORT CloseTime = 0;
  196.             REGISTER struct IntuiMessage *Msg;
  197.             REGISTER struct Window *MyWin;
  198.             while (Msg = (struct IntuiMessage *)GetMsg(Sharedport)) {
  199.                 if (Msg->Class == CLOSEWINDOW) {  /* Want to close? */
  200.                     CloseTime = 1;                /* Set a flag */
  201.                     MyWin = Msg->IDCMPWindow;     /* Window to close*/
  202.                 }
  203.                 ReplyMsg(Msg);
  204.             }
  205.             if (CloseTime) {
  206.                 closesharedwindow(MyWin);
  207.             }
  208.         }
  209.         if (sig & SIGBREAKF_CTRL_C) {
  210.             /* This is my cue. Exit if there are no open windows depending on us */
  211.             if (Sharedrefs) {
  212.                 DisplayBeep(NULL);
  213.             } else {
  214.                 CloseStuff();
  215.             }
  216.         }
  217.         if (sig & initsignal) {
  218.             SafePatch();                   /* Patch dangerous functions */
  219.         }
  220.         if (sig & cancelsignal) {
  221.             SafeRestore();
  222.         }
  223.         if (sig & startsignal) { /* The handler wants a word in. */
  224.             if (action == snapgfx) {       /* Check user action */
  225.                 HandleGfx();               /* Get the picture :-) */
  226.             } else if (action == snaptext) {
  227.                 if (HandleChars()) {             /* Snap some chars */
  228.                     if (xerox) {
  229.                         sig |= insertsignal;
  230.                     }
  231.                 }
  232.             } else {
  233.                   /* Previous snap wasn't finished when this one started. */
  234.                 SetSignal(0L,
  235.                   movesignal|cancelsignal|donesignal|clicksignal|timersignal);
  236.                 DisplayBeep(NULL);
  237.                 action = noaction;
  238.             }
  239.             if (!(sig & insertsignal)) {
  240.                 SafeRestore();             /* Layers unlocked - all safe */
  241.             }
  242.         }
  243.  
  244.         if (sig & insertsignal) {
  245.             LONG i;
  246.             UBYTE *SnapSpace;
  247.             ULONG ascii;
  248.             action = insert;
  249.             if (SnapSpace = FetchClip()) {  /* Get clipboard data */
  250.                   /* Not necessary to patch here but it guarantees
  251.                      that all inserted chars end up in the same window. */
  252.                 SafePatch();
  253.                   /* get the current keymap  */
  254.                 ConIOR->io_Command =  CD_ASKDEFAULTKEYMAP;
  255.                 ConIOR->io_Length  = sizeof (struct KeyMap);
  256.                 ConIOR->io_Data    = (APTR) &keymap;
  257.                 ConIOR->io_Flags   = 1;    /* no IOQuick   */
  258.                 DoIO(ConIOR);
  259.                   /* Set up an input request */
  260.                 inputRequestBlock->io_Command = IND_WRITEEVENT;
  261.                 inputRequestBlock->io_Flags   = 0L;
  262.                 inputRequestBlock->io_Length  = (long)sizeof(struct InputEvent);
  263.                 inputRequestBlock->io_Data    = (APTR)&SimEvent;
  264.                   /* Translate chars in SnapSpace and insert them
  265.                      into the input stream. */
  266.                 for (i=4; SnapSpace[i] && (action == insert); i++) {
  267.                     ascii = SnapSpace[i];
  268.                     if (ascii == 10) {
  269.                         ascii = 13;     /* WYSIWYG? Hah! */
  270.                         if (linedelay) {           /* Almost missed this one */
  271.                             Delay(linedelay);      /* Give the man some air */
  272.                         }
  273.                     }
  274.                     InvertKeyMap(ascii, &SimEvent, &keymap);
  275.                     DoIO(inputRequestBlock);
  276.                     if (chardelay) {
  277.                         Delay(chardelay);
  278.                     }
  279.                 }
  280.                 SafeRestore();  /* "Depatch" */
  281.                   /* Free memory given to us by FetchClip() */
  282.                 FreeMem(SnapSpace, *(ULONG *)SnapSpace);
  283.             }
  284.             action = noaction;
  285.         }
  286.     }
  287.  
  288.     CloseStuff();  /* Guess what */
  289. }
  290.