home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 299.lha / Snap_v1.32 / openclose.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-12-06  |  6.1 KB  |  216 lines

  1. #include "snap.h"
  2.  
  3. struct IOClipReq *CreateExtIO();
  4. void DeleteExtIO();
  5.  
  6. IMPORT LONGBITS startsignal, insertsignal, cancelsignal, donesignal;
  7. IMPORT LONGBITS movesignal, clicksignal, timersignal, initsignal;
  8. IMPORT ULONG startsignum, insertsignum, cancelsignum, donesignum;
  9. IMPORT ULONG movesignum, clicksignum, timersignum, initsignum;
  10.  
  11. /* program */
  12. IMPORT struct MsgPort *port;
  13. IMPORT struct Task *MyTask;
  14.  
  15. /* Snap state machine */
  16. IMPORT WORD action;
  17. IMPORT WORD state;
  18.  
  19. /* clipboard */
  20. IMPORT struct IOClipReq *ClipReq;
  21. IMPORT struct MsgPort *ClipPort;
  22.  
  23. /* input device */
  24. IMPORT struct MsgPort *inputDevPort;
  25. IMPORT struct Interrupt handlerStuff;
  26. IMPORT struct IOStdReq *inputRequestBlock;
  27. IMPORT WORD Priority;
  28.  
  29. IMPORT UBYTE *CharData;
  30.  
  31. /* console */
  32. IMPORT struct MsgPort *ConPort;
  33. IMPORT struct IOStdReq *ConIOR;
  34. IMPORT struct KeyMap keymap;
  35.  
  36. /* windows */
  37. IMPORT struct MsgPort *Sharedport;
  38. IMPORT SHORT Sharedrefs;
  39.  
  40. /* libraries */
  41. IMPORT struct IntuitionBase *IntuitionBase;
  42. IMPORT struct GfxBase       *GfxBase;
  43. IMPORT struct LayersBase    *LayersBase;
  44.  
  45. /* graphics */
  46. IMPORT struct RastPort TempRp;
  47. IMPORT struct BitMap TempBM;
  48. IMPORT UBYTE *TempRaster;
  49.  
  50. VOID CloseStuff()
  51. {
  52.     SafeRestore();
  53.     if (TempRaster)         FreeRaster(TempRaster, 16L, 16L);
  54.     if (CharData)           FreeMem(CharData, 256L*32);
  55.     if (inputRequestBlock) {
  56.         if (inputRequestBlock->io_Device) {
  57.             inputRequestBlock->io_Command = IND_REMHANDLER;  /* Remove handler */
  58.             inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  59.             DoIO(inputRequestBlock);
  60.             CloseDevice(inputRequestBlock);
  61.         }
  62.         DeleteStdIO(inputRequestBlock);
  63.     }
  64.     if (inputDevPort)       DeletePort(inputDevPort);
  65.     if (ConIOR) {
  66.         CloseDevice(ConIOR);
  67.         DeleteStdIO(ConIOR);
  68.     }
  69.     if (ConPort)            DeletePort(ConPort);
  70.     if (ClipReq) {
  71.         if (ClipReq->io_Device) {
  72.             CloseDevice(ClipReq);
  73.         }
  74.         DeleteExtIO(ClipReq);
  75.     }
  76.     if (ClipPort)           DeletePort(ClipPort);
  77.     if (startsignum != -1)  FreeSignal(startsignum);
  78.     if (donesignum != -1)   FreeSignal(donesignum);
  79.     if (cancelsignum != -1) FreeSignal(cancelsignum);
  80.     if (movesignum != -1)   FreeSignal(movesignum);
  81.     if (insertsignum != -1) FreeSignal(insertsignum);
  82.     if (clicksignum != -1)  FreeSignal(clicksignum);
  83.     if (timersignum != -1)  FreeSignal(timersignum);
  84.     if (initsignum != -1)   FreeSignal(initsignum);
  85.     if (port) {
  86.         DeletePort(port);
  87.     }
  88.     if (IntuitionBase)      CloseLibrary(IntuitionBase);
  89.     if (GfxBase)            CloseLibrary(GfxBase);
  90.     if (LayersBase)         CloseLibrary(LayersBase);
  91.  
  92.     exit(0);
  93. }
  94.  
  95. VOID OpenStuff()
  96. {
  97.     action = noaction;
  98.     state  = waiting;
  99.     inputRequestBlock = NULL;
  100.  
  101.     Sharedrefs = 0;
  102.     Sharedport = NULL;
  103.  
  104.     Forbid();
  105.     if (port = FindPort(SNAPPORT)) {
  106.         Signal(port->mp_SigTask, SIGBREAKF_CTRL_C);
  107.         Permit();
  108.         port = NULL;  /* Don't touch his port */
  109.         CloseStuff();
  110.     }
  111.  
  112.     Permit();
  113.  
  114.     /* OK, we're here to stay. Set up everything we need. */
  115.  
  116.     /* libraries */
  117.  
  118.     if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L)))
  119.         CloseStuff();
  120.     if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0L)))
  121.         CloseStuff();
  122.     if (!(LayersBase = (struct LayersBase *)OpenLibrary("layers.library", 0L)))
  123.         CloseStuff();
  124.  
  125.     /* public port */
  126.  
  127.     if (!(port = CreatePort(SNAPPORT, 0L)))
  128.         CloseStuff();
  129.  
  130.     /* signals */
  131.  
  132.     if ((startsignum = AllocSignal(-1L)) == -1L)
  133.         CloseStuff();
  134.     if ((insertsignum = AllocSignal(-1L)) == -1L)
  135.         CloseStuff();
  136.     if ((cancelsignum = AllocSignal(-1L)) == -1L)
  137.         CloseStuff();
  138.     if ((donesignum = AllocSignal(-1L)) == -1L)
  139.         CloseStuff();
  140.     if ((movesignum = AllocSignal(-1L)) == -1L)
  141.         CloseStuff();
  142.     if ((clicksignum = AllocSignal(-1L)) == -1L)
  143.         CloseStuff();
  144.     if ((timersignum = AllocSignal(-1L)) == -1L)
  145.         CloseStuff();
  146.     if ((initsignum = AllocSignal(-1L)) == -1L)
  147.         CloseStuff();
  148.     MyTask = FindTask(0L);                   /* Find myself to Signal me.  */
  149.     startsignal  = 1L << startsignum;         /* No good to use bit numbers */
  150.     insertsignal = 1L << insertsignum;
  151.     cancelsignal = 1L << cancelsignum;
  152.     donesignal   = 1L << donesignum;
  153.     movesignal   = 1L << movesignum;
  154.     clicksignal  = 1L << clicksignum;
  155.     timersignal  = 1L << timersignum;
  156.     initsignal   = 1L << initsignum;
  157.  
  158.     /* clipboard device */
  159.  
  160.     if (!(ClipPort = CreatePort(0L, 0L)))
  161.         CloseStuff();
  162.     if (!(ClipReq = CreateExtIO(ClipPort, (LONG)sizeof(*ClipReq))))
  163.         CloseStuff();
  164.     if (OpenDevice("clipboard.device", 0L, ClipReq, 0L))
  165.         CloseStuff();
  166.     ClipReq->io_ClipID = 0L;
  167.  
  168.     /* console device */
  169.  
  170.     if (!(ConPort = CreatePort(0L, 0L)))
  171.         CloseStuff();
  172.     if (!(ConIOR = CreateStdIO(ConPort)))
  173.         CloseStuff();
  174.     if (OpenDevice("console.device", -1L, ConIOR, 0L))
  175.         CloseStuff();
  176.  
  177.     /* input devive */
  178.  
  179.     if (!(inputDevPort = CreatePort(0L, 0L)))
  180.         CloseStuff();
  181.     if (!(inputRequestBlock = CreateStdIO(inputDevPort)))
  182.         CloseStuff();
  183.     if (OpenDevice("input.device", 0L, inputRequestBlock, 0L))
  184.         CloseStuff();
  185.  
  186.     /* input handler */
  187.  
  188.     handlerStuff.is_Data = 0x534E4150;       /* Set up for installation of */
  189.     handlerStuff.is_Code = myhandler;        /* myhandler.                 */
  190.     handlerStuff.is_Node.ln_Pri = Priority;  /* Ahead of intuition, please */
  191.     handlerStuff.is_Node.ln_Name = "Snap Input Handler";
  192.  
  193.     inputRequestBlock->io_Command = IND_ADDHANDLER;
  194.     inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  195.  
  196.     DoIO(inputRequestBlock);  /* Add me. */
  197.  
  198.  
  199.     /* Aligned font bitmap to use when matching */
  200.  
  201.     if (!(CharData = AllocRaster(16L, 256L*16))) {
  202.         CloseStuff();
  203.     }
  204.  
  205.     /* temporary raster */
  206.  
  207.     if (!(TempRaster = AllocRaster(16L, 16L)))
  208.         CloseStuff();
  209.     InitRastPort(&TempRp);                   /* Init RastPort used for */
  210.     InitBitMap(&TempBM, 1L, 16L, 16L);       /* Locating position of   */
  211.     TempBM.Planes[0] = TempRaster;           /* first character.       */
  212.     TempRp.BitMap = &TempBM;
  213.  
  214.  
  215. }
  216.