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