home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 500-599 / ff589.lza / Term / TermSrc.lha / termXEM.c < prev    next >
C/C++ Source or Header  |  1991-12-01  |  6KB  |  324 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1991 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: TermXEM.c
  6.  *    Created ..: Saturday 03-Nov-91 11:17
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    03-Nov-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. #include "termGlobal.h"
  16.  
  17.     /* Local terminal emulation data. */
  18.  
  19. STATIC    struct sharedvars     sv;
  20. STATIC    struct hostfuncs     hf;
  21. STATIC    BPTR             EmulatorSegment;
  22. STATIC    struct MsgPort        *EmulatorPort;
  23. STATIC    UWORD             ScreenDepth;
  24.  
  25.     /* Two less important dummy functions. */
  26.  
  27. STATIC BOOL __saveds
  28. TrueDummy()
  29. {
  30.     return(TRUE);
  31. }
  32.  
  33. STATIC VOID __saveds
  34. VoidDummy()
  35. {
  36. }
  37.  
  38.     /* xem_sbreak():
  39.      *
  40.      *    Send a break signal across the serial line.
  41.      */
  42.  
  43. STATIC BOOL __saveds
  44. xem_sbreak()
  45. {
  46.     if(!WriteRequest)
  47.         return(-1);
  48.  
  49.     WriteRequest -> IOSer . io_Command = SDCMD_BREAK;
  50.  
  51.     return((LONG)DoIO(WriteRequest));
  52. }
  53.  
  54.     /* xem_swrite(UBYTE *Buffer,ULONG Size):
  55.      *
  56.      *    Write a few bytes to the serial line.
  57.      */
  58.  
  59. STATIC BOOL __saveds
  60. xem_swrite(UBYTE *Buffer,ULONG Size)
  61. {
  62.     return((BOOL)xpr_swrite(Buffer,Size));
  63. }
  64.  
  65.     /* xem_sread(UBYTE *Buffer,ULONG Size,LONG Timeout):
  66.      *
  67.      *    Read a few bytes fro the serial line.
  68.      */
  69.  
  70. STATIC LONG __saveds
  71. xem_sread(UBYTE *Buffer,ULONG Size,LONG Timeout)
  72. {
  73.     return((LONG)xpr_sread(Buffer,Size,Timeout));
  74. }
  75.  
  76.     /* xem_tgets(UBYTE *Prompt,UBYTE *Buffer,ULONG Size):
  77.      *
  78.      *    Get a string from the user.
  79.      */
  80.  
  81. STATIC BOOL __saveds
  82. xem_tgets(UBYTE *Prompt,UBYTE *Buffer,ULONG Size)
  83. {
  84.     return((BOOL)xpr_gets(Prompt,Buffer));
  85. }
  86.  
  87.     /* xem_tbeep(ULONG Times,ULONG Delay):
  88.      *
  89.      *    Beep the terminal display.
  90.      */
  91.  
  92. STATIC VOID __saveds
  93. xem_tbeep(ULONG Times,ULONG Delay)
  94. {
  95.     WORD i;
  96.  
  97.     for(i = 0 ; i < Times ; i++)
  98.         RealBeep();
  99. }
  100.  
  101.     /* LoadEmulator(struct sharedvars *sv,UBYTE *Name):
  102.      *
  103.      *    Load emulator code and start it.
  104.      */
  105.  
  106. STATIC BYTE __regargs
  107. LoadEmulator(struct sharedvars *sv,UBYTE *Name)
  108. {
  109.         /* Reload emulator code if none present. */
  110.  
  111.     if(!EmulatorSegment)
  112.         EmulatorSegment = LoadSeg(Name);
  113.  
  114.     if(EmulatorSegment)
  115.     {
  116.         struct MsgPort *ReplyPort;
  117.  
  118.             /* Create a reply port. */
  119.  
  120.         if(ReplyPort = (struct MsgPort *)CreateMsgPort())
  121.         {
  122.                 /* Create the emulator process. */
  123.  
  124.             if(EmulatorPort = (struct MsgPort *)CreateProc(FilePart(Name),0,EmulatorSegment,4000))
  125.             {
  126.                 struct EmuMsg    msg;
  127.                 BYTE        GotIt = FALSE;
  128.                 ULONG        SignalSet;
  129.  
  130.                     /* Set up handshake message. */
  131.  
  132.                 memset(&msg,0,sizeof(struct EmuMsg));
  133.  
  134.                 msg . em_msg . mn_Node . ln_Name    = EMULATOR_CURRENT_VERSION;
  135.                 msg . em_arg                = (APTR)sv;
  136.                 msg . em_msg . mn_ReplyPort        = ReplyPort;
  137.                 msg . em_msg . mn_Length        = sizeof(struct EmuMsg);
  138.  
  139.                     /* Set up for timeout. */
  140.  
  141.                 TimeRequest -> tr_node . io_Command    = TR_ADDREQUEST;
  142.                 TimeRequest -> tr_time . tv_secs    = 3;
  143.                 TimeRequest -> tr_time . tv_micro    = 0;
  144.  
  145.                 SendIO(TimeRequest);
  146.  
  147.                     /* Transfer handshake message. */
  148.  
  149.                 PutMsg(EmulatorPort,(struct Message *)&msg);
  150.  
  151.                     /* Wait for handshake. */
  152.  
  153.                 while(!GotIt)
  154.                 {
  155.                     SignalSet = Wait(SIG_TIMER | (1 << ReplyPort -> mp_SigBit));
  156.  
  157.                         /* Got a reply? */
  158.  
  159.                     if(SignalSet & (1 << ReplyPort -> mp_SigBit))
  160.                     {
  161.                         GetMsg(ReplyPort);
  162.  
  163.                         GotIt = TRUE;
  164.  
  165.                         if(!(SignalSet & SIG_TIMER))
  166.                             AbortIO(TimeRequest);
  167.  
  168.                         break;
  169.                     }
  170.  
  171.                         /* Hit by timeout? */
  172.  
  173.                     if(SignalSet & SIG_TIMER)
  174.                         break;
  175.                 }
  176.  
  177.                     /* Wait for timer request to return. */
  178.  
  179.                 WaitIO(TimeRequest);
  180.  
  181.                     /* Successful initialization? */
  182.  
  183.                 if(GotIt)
  184.                 {
  185.                         /* Does the module respond to our version number? */
  186.  
  187.                     if(!strcmp(msg . em_msg . mn_Node . ln_Name,CURRENT_VERSION_ACCEPTED))
  188.                     {
  189.                             /* Extract callback structure. */
  190.  
  191.                         if(Console = (struct confuncs *)msg . em_arg)
  192.                         {
  193.                                 /* Allocate resources. */
  194.  
  195.                             if(Console -> alloc_resources((APTR)Console))
  196.                             {
  197.                                     /* Open the console. */
  198.  
  199.                                 if(!(Console -> open_console((APTR)Console)))
  200.                                 {
  201.                                     Console -> close_console((APTR)Console);
  202.  
  203.                                     Console -> free_resources((APTR)Console);
  204.  
  205.                                     Console = NULL;
  206.  
  207.                                     EmulationSetupError = EM_CONSOLE;
  208.                                 }
  209.                                 else
  210.                                 {
  211.                                     EmulationSetupError = 0;
  212.  
  213.                                     return(TRUE);
  214.                                 }
  215.                             }
  216.                             else
  217.                             {
  218.                                 Console -> free_resources((APTR)Console);
  219.  
  220.                                 Console = NULL;
  221.  
  222.                                 EmulationSetupError = EM_RESOURCES;
  223.                             }
  224.                         }
  225.                         else
  226.                             EmulationSetupError = EM_DATA;
  227.                     }
  228.                     else
  229.                         EmulationSetupError = EM_VERSION;
  230.                 }
  231.                 else
  232.                     EmulationSetupError = EM_TIMEOUT;
  233.             }
  234.             else
  235.                 EmulationSetupError = EM_NOPROC;
  236.  
  237.             RemTask(EmulatorPort -> mp_SigTask);
  238.  
  239.             EmulatorPort = NULL;
  240.         }
  241.         else
  242.             EmulationSetupError = EM_NOPORT;
  243.  
  244.         UnLoadSeg(EmulatorSegment);
  245.  
  246.         EmulatorSegment = NULL;
  247.     }
  248.     else
  249.         EmulationSetupError = EM_LOADFAILED;
  250.  
  251.     return(FALSE);
  252. }
  253.  
  254.     /* DeleteEmulator(BYTE UnloadIt):
  255.      *
  256.      *    Disable or unload the current terminal emulation.
  257.      */
  258.  
  259. VOID
  260. DeleteEmulator(BYTE UnloadIt)
  261. {
  262.     if(Console)
  263.     {
  264.         Console -> close_console((APTR)Console);
  265.  
  266.         Console -> free_resources((APTR)Console);
  267.  
  268.         Console = NULL;
  269.     }
  270.  
  271.     if(EmulatorPort)
  272.     {
  273.         RemTask(EmulatorPort -> mp_SigTask);
  274.  
  275.         EmulatorPort = NULL;
  276.     }
  277.  
  278.     if(EmulatorSegment && UnloadIt)
  279.     {
  280.         UnLoadSeg(EmulatorSegment);
  281.  
  282.         EmulatorSegment = NULL;
  283.     }
  284. }
  285.  
  286.     /* CreateEmulator(UBYTE *Name,BYTE ForceNew):
  287.      *
  288.      *    Set up or reload the external terminal emulator.
  289.      */
  290.  
  291. BYTE
  292. CreateEmulator(UBYTE *Name,BYTE ForceNew)
  293. {
  294.     DeleteEmulator(ForceNew);
  295.  
  296.     hf . xem_swrite        = (APTR)xem_swrite;
  297.     hf . xem_sread        = (APTR)xem_sread;
  298.     hf . xem_sbreak        = (APTR)xem_sbreak;
  299.     hf . xem_sstart        = (APTR)VoidDummy;
  300.     hf . xem_sstop        = (APTR)TrueDummy;
  301.     hf . xem_tgets        = (APTR)xem_tgets;
  302.     hf . xem_tbeep        = (APTR)xem_tbeep;
  303.     hf . xpr_options    = (APTR)xpr_options;
  304.     hf . xpr_sflush        = (APTR)xpr_sflush;
  305.  
  306.     ScreenDepth = Screen -> RastPort . BitMap -> Depth;
  307.  
  308.     sv . hf            = &hf;
  309.     sv . scr        = &Screen;
  310.     sv . win        = &Window;
  311.     sv . rp            = &RPort;
  312.     sv . extsig        = &ExtSigs;
  313.     sv . depth        = &ScreenDepth;
  314.     sv . SysBase        = SysBase;
  315.     sv . DOSBase        = DOSBase;
  316.     sv . GfxBase        = GfxBase;
  317.     sv . IntuitionBase    = IntuitionBase;
  318.     sv . DiskFontBase    = DiskfontBase;
  319.     sv . KeymapBase        = KeymapBase;
  320.     sv . RexxSysBase    = RexxSysBase;
  321.  
  322.     return(LoadEmulator(&sv,Name));
  323. }
  324.