home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d832 / term.lha / Term / term-3.1-Source.lha / termXEM.c < prev    next >
C/C++ Source or Header  |  1993-02-05  |  8KB  |  463 lines

  1. /*
  2. **    termXEM.c
  3. **
  4. **    External emulation support routines
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* xem_swrite():
  13.      *
  14.      *    Send a few bytes across the serial line.
  15.      */
  16.  
  17. LONG __saveds __asm
  18. xem_swrite(register __a0 STRPTR Buffer,register __d0 LONG Size)
  19. {
  20.     if(WriteRequest)
  21.     {
  22.         SerWrite(Buffer,Size);
  23.  
  24.         return(0);
  25.     }
  26.     else
  27.         return(-1);
  28. }
  29.  
  30.     /* xem_sbreak():
  31.      *
  32.      *    Send a break signal across the serial line.
  33.      */
  34.  
  35. LONG __asm __saveds
  36. xem_sbreak(VOID)
  37. {
  38.     if(!WriteRequest)
  39.         return(-1);
  40.     else
  41.     {
  42.         WriteRequest -> IOSer . io_Command = SDCMD_BREAK;
  43.  
  44.         return((LONG)DoIO(WriteRequest));
  45.     }
  46. }
  47.  
  48.     /* xem_sstart():
  49.      *
  50.      *    Restart serial read activity.
  51.      */
  52.  
  53. VOID __asm __saveds
  54. xem_sstart(VOID)
  55. {
  56.     if(ReadRequest)
  57.     {
  58.         ReadRequest -> IOSer . io_Command    = CMD_READ;
  59.         ReadRequest -> IOSer . io_Data        = ReadBuffer;
  60.         ReadRequest -> IOSer . io_Length    = 1;
  61.  
  62.         SetSignal(0,SIG_SERIAL);
  63.  
  64.         BeginIO(ReadRequest);
  65.     }
  66. }
  67.  
  68.     /* xem_sstop():
  69.      *
  70.      *    Stop serial read activity.
  71.      */
  72.  
  73. LONG __asm __saveds
  74. xem_sstop(VOID)
  75. {
  76.     if(ReadRequest)
  77.     {
  78.         if(!CheckIO(ReadRequest))
  79.             AbortIO(ReadRequest);
  80.  
  81.         WaitIO(ReadRequest);
  82.     }
  83.  
  84.     return(0);
  85. }
  86.  
  87.     /* xem_tgets(STRPTR Prompt,STRPTR Buffer,ULONG Size):
  88.      *
  89.      *    Get a string from the user.
  90.      */
  91.  
  92. LONG __saveds __asm
  93. xem_tgets(register __a0 STRPTR Prompt,register __a1 STRPTR Buffer,register __d0 ULONG Size)
  94. {
  95.     return(xpr_gets(Prompt,Buffer));
  96. }
  97.  
  98.     /* xem_tbeep(ULONG Times,ULONG Delay):
  99.      *
  100.      *    Beep the terminal display.
  101.      */
  102.  
  103. VOID __saveds __asm
  104. xem_tbeep(register __d0 ULONG Times,register __d1 ULONG Delay)
  105. {
  106.     WORD i;
  107.  
  108.     for(i = 0 ; i < Times ; i++)
  109.     {
  110.             /* Handle the visual part. */
  111.  
  112.         if(Config -> TerminalConfig -> BellMode != BELL_AUDIBLE)
  113.         {
  114.             if(StatusProcess)
  115.                 Signal(StatusProcess,SIG_BELL);
  116.         }
  117.  
  118.             /* Let it beep. */
  119.  
  120.         if(Config -> TerminalConfig -> BellMode == BELL_AUDIBLE || Config -> TerminalConfig -> BellMode == BELL_BOTH)
  121.             Beep();
  122.     }
  123. }
  124.  
  125.     /* xem_macrodispatch(struct XEmulatorMacroKey *XEM_MacroKey):
  126.      *
  127.      *    Dispatch a macro key call.
  128.      */
  129.  
  130. LONG __saveds __asm
  131. xem_macrodispatch(register __a0 struct XEmulatorMacroKey *XEM_MacroKey)
  132. {
  133.     VOID (*Routine)(VOID);
  134.  
  135.         /* If a routine to call is available (most likely xON or xOFF),
  136.          * make a call to it, else process the macro key data.
  137.          */
  138.  
  139.     if(Routine = (VPTR)XEM_MacroKey -> xmk_UserData)
  140.         (*Routine)();
  141.     else
  142.         SerialCommand(MacroKeys -> Keys[XEM_MacroKey -> xmk_Qualifier][XEM_MacroKey -> xmk_Code - 0x50]);
  143.  
  144.     return(0);
  145. }
  146.  
  147.     /* SetEmulatorOptions(BYTE Mode):
  148.      *
  149.      *    Save or load the emulator options.
  150.      */
  151.  
  152. BYTE
  153. SetEmulatorOptions(BYTE Mode)
  154. {
  155.     BYTE Success = FALSE;
  156.  
  157.         /* Is the library available and running? */
  158.  
  159.     if(XEmulatorBase && XEM_IO)
  160.     {
  161.             /* Are we using the new library code? */
  162.  
  163.         if(XEmulatorBase -> lib_Version >= 4)
  164.         {
  165.                 /* Get the name of the library. */
  166.  
  167.             strcpy(SharedBuffer,FilePart(XEmulatorBase -> lib_Node . ln_Name));
  168.  
  169.                 /* Does it have any name? */
  170.  
  171.             if(SharedBuffer[0])
  172.             {
  173.                 UBYTE    OtherBuffer[50];
  174.                 WORD    i;
  175.  
  176.                     /* Strip the `.library' bit. */
  177.  
  178.                 for(i = strlen(SharedBuffer) - 1 ; i >= 0 ; i--)
  179.                 {
  180.                     if(SharedBuffer[i] == '.')
  181.                     {
  182.                         SharedBuffer[i] = 0;
  183.  
  184.                         break;
  185.                     }
  186.                 }
  187.  
  188.                     /* What are we to do? */
  189.  
  190.                 if(Mode == XEM_PREFS_LOAD)
  191.                 {
  192.                         /* Restore settings... */
  193.  
  194.                     strcpy(OtherBuffer,"ENV:");
  195.  
  196.                     if(AddPart(OtherBuffer,SharedBuffer,50))
  197.                     {
  198.                             /* If we can't load them,
  199.                              * reset to defaults.
  200.                              */
  201.  
  202.                         if(!XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  203.                             XEmulatorPreferences(XEM_IO,NULL,XEM_PREFS_RESET);
  204.                         else
  205.                             Success = TRUE;
  206.                     }
  207.                 }
  208.                 else
  209.                 {
  210.                         /* Save settings to ENV: */
  211.  
  212.                     strcpy(OtherBuffer,"ENV:");
  213.  
  214.                     if(AddPart(OtherBuffer,SharedBuffer,50))
  215.                     {
  216.                         if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  217.                             Success = TRUE;
  218.                     }
  219.  
  220.                     if(Success)
  221.                     {
  222.                         Success = FALSE;
  223.  
  224.                             /* Save settings to ENVARC: */
  225.  
  226.                         strcpy(OtherBuffer,"ENVARC:");
  227.  
  228.                         if(AddPart(OtherBuffer,SharedBuffer,50))
  229.                         {
  230.                             if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  231.                                 Success = TRUE;
  232.                         }
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.     }
  238.  
  239.         /* Return result. */
  240.  
  241.     return(Success);
  242. }
  243.  
  244.     /* SetupEmulator(BYTE OpenConsole):
  245.      *
  246.      *    Initialize the XEM_IO structure.
  247.      */
  248.  
  249. STATIC BYTE
  250. SetupEmulator(VOID)
  251. {
  252.     if(!XEM_IO)
  253.     {
  254.         if(XEM_IO = (struct XEM_IO *)AllocVec(sizeof(struct XEM_IO),MEMF_ANY|MEMF_CLEAR))
  255.         {
  256.             XEM_IO -> xem_window        = Window;
  257.             XEM_IO -> xem_font        = CurrentFont;
  258.             XEM_IO -> xem_signal        = &XEM_Signal;
  259.             XEM_IO -> xem_screendepth    = Window -> WScreen -> RastPort . BitMap -> Depth;
  260.  
  261.             XEM_IO -> xem_sread        = xpr_sread;
  262.             XEM_IO -> xem_swrite        = xem_swrite;
  263.             XEM_IO -> xem_sflush        = xpr_sflush;
  264.             XEM_IO -> xem_sbreak        = xem_sbreak;
  265.             XEM_IO -> xem_squery        = xpr_squery;
  266.             XEM_IO -> xem_sstart        = xem_sstart;
  267.             XEM_IO -> xem_sstop        = xem_sstop;
  268.  
  269.             XEM_IO -> xem_tbeep        = xem_tbeep;
  270.             XEM_IO -> xem_tgets        = xem_tgets;
  271.             XEM_IO -> xem_toptions        = xpr_options;
  272.  
  273.             XEM_IO -> xem_process_macrokeys    = xem_macrodispatch;
  274.  
  275.             return(TRUE);
  276.         }
  277.     }
  278.     else
  279.         return(FALSE);
  280.  
  281.     return(FALSE);
  282. }
  283.  
  284.     /* CloseEmulator():
  285.      *
  286.      *    Close the emulation library.
  287.      */
  288.  
  289. VOID
  290. CloseEmulator()
  291. {
  292.     if(XEmulatorBase)
  293.     {
  294.         if(XEM_IO)
  295.         {
  296.             XEmulatorMacroKeyFilter(XEM_IO,NULL);
  297.             XEmulatorCloseConsole(XEM_IO);
  298.             XEmulatorCleanup(XEM_IO);
  299.  
  300.             FreeVec(XEM_IO);
  301.  
  302.             XEM_IO = NULL;
  303.         }
  304.  
  305.         CloseLibrary(XEmulatorBase);
  306.  
  307.         strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  308.  
  309.         XEmulatorBase    = NULL;
  310.         XEM_Signal    = NULL;
  311.  
  312.         RasterEnabled = TRUE;
  313.  
  314.         ClearCursor();
  315.  
  316.         Reset();
  317.  
  318.         DrawCursor();
  319.     }
  320. }
  321.  
  322.     /* OpenEmulator(STRPTR Name):
  323.      *
  324.      *    Open an emulation library.
  325.      */
  326.  
  327. BYTE
  328. OpenEmulator(STRPTR Name)
  329. {
  330.     CloseEmulator();
  331.  
  332.     XEM_HostData . Source        = NULL;
  333.     XEM_HostData . Destination    = NULL;
  334.     XEM_HostData . InESC        = FALSE;
  335.     XEM_HostData . InCSI        = FALSE;
  336.  
  337.     if(XEmulatorBase = OpenLibrary(Name,0))
  338.     {
  339.         ClearCursor();
  340.  
  341.         Reset();
  342.  
  343.         if(SetupEmulator())
  344.         {
  345.             SetWrMsk(RPort,DepthMask);
  346.  
  347.             if(XEmulatorSetup(XEM_IO))
  348.             {
  349.                 SetEmulatorOptions(XEM_PREFS_LOAD);
  350.  
  351.                 if(XEmulatorOpenConsole(XEM_IO))
  352.                 {
  353.                     STRPTR LibName = FilePart(Name);
  354.  
  355.                     strcpy(EmulationName,&LibName[3]);
  356.  
  357.                     EmulationName[strlen(EmulationName) - 8] = 0;
  358.  
  359.                     SetupXEM_MacroKeys(MacroKeys);
  360.  
  361.                     return(TRUE);
  362.                 }
  363.             }
  364.         }
  365.  
  366.         DrawCursor();
  367.  
  368.         CloseLibrary(XEmulatorBase);
  369.  
  370.         strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  371.  
  372.         XEmulatorBase    = NULL;
  373.         XEM_Signal    = NULL;
  374.     }
  375.  
  376.     return(FALSE);
  377. }
  378.  
  379.     /* XOff():
  380.      *
  381.      *    Small local routine, complements XOn() in Serial.c
  382.      */
  383.  
  384. STATIC VOID
  385. XOff(VOID)
  386. {
  387.     if(Status == STATUS_HOLDING)
  388.     {
  389.         UBYTE c = XOF;
  390.  
  391.         SerWrite(&c,1);
  392.  
  393.         Status = STATUS_READY;
  394.     }
  395. }
  396.  
  397.     /* SetupXEM_MacroKeys(struct MacroKeys *Keys):
  398.      *
  399.      *    Sets up the internal representation of the macro key
  400.      *    data to fit the XEM specification.
  401.      */
  402.  
  403. VOID
  404. SetupXEM_MacroKeys(struct MacroKeys *Keys)
  405. {
  406.         /* Are we allowed to do what we want to do? */
  407.  
  408.     if(XEM_MacroKeys && XEmulatorBase && Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  409.     {
  410.         WORD i,j,k = 0;
  411.  
  412.             /* Clear the macro list. */
  413.  
  414.         NewList(&XEM_MacroList);
  415.  
  416.             /* Run down the list of qualifiers. */
  417.  
  418.         for(i = XMKQ_NONE ; i <= XMKQ_CONTROL ; i++)
  419.         {
  420.                 /* Run down the function keys. */
  421.  
  422.             for(j = 0 ; j < 10 ; j++)
  423.             {
  424.                     /* If the key has no data attached,
  425.                      * don't use it in the list.
  426.                      */
  427.  
  428.                 if(Keys -> Keys[i][j][0])
  429.                 {
  430.                     XEM_MacroKeys[k] . xmk_Type        = XMKT_RAWKEY;
  431.                     XEM_MacroKeys[k] . xmk_Qualifier    = i;
  432.                     XEM_MacroKeys[k] . xmk_Code        = 0x50 + j;
  433.                     XEM_MacroKeys[k] . xmk_UserData        = NULL;
  434.  
  435.                     AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  436.                 }
  437.             }
  438.         }
  439.  
  440.             /* Take care of the rest, add support for the xON key. */
  441.  
  442.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  443.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  444.         XEM_MacroKeys[k] . xmk_Code        = XON;
  445.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)DoxON;
  446.  
  447.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  448.  
  449.             /* Take care of the xOFF key. */
  450.  
  451.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  452.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  453.         XEM_MacroKeys[k] . xmk_Code        = XOF;
  454.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)XOff;
  455.  
  456.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k]);
  457.  
  458.             /* Make the emulator notice the new settings. */
  459.  
  460.         XEmulatorMacroKeyFilter(XEM_IO,&XEM_MacroList);
  461.     }
  462. }
  463.