home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / term23_2.lha / Source_Code / termSource / termXEM.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  8KB  |  455 lines

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