
/***************************************************************************
*
* Wincon
*
* Dumb terminal emulation module
*
***************************************************************************/
#include <windows.h>
#include "winkrm.h"
     
void NEAR DisplayBuf(char*, int);
     
/***************************************************************************
*
* connect
*
* Connect loop for Windows Kermit
*
* Entry: None.
*
* Exit: None.
*
***************************************************************************/
connect()
{
     
    int length;
    char RXBuf[RXSIZE];		/* holding buffer for display */
     
    ThisState = CONNECTSTATE;
    while ((length = GetBuf(RXBuf)) > 0) {
        HideCaret(hKermWnd);		/* hide caret, display buffer */
        DisplayBuf(RXBuf,length);
        SetCaretPos(Xpos,Ypos);		/* update caret, show caret */
        ShowCaret(hKermWnd);
    }
}
     
/***************************************************************************
*
* DisplayBuf
*
* Display the current contents of the holding buffer and update screen buffer.
*
* Entry: Buffer and its size.
*
* Exit:  Character displayed on screen and screen memory buffer updated.
*
***************************************************************************/
void NEAR DisplayBuf(Buffer, count)
char Buffer[];
int count;
{
     
    int k;
    char ch;
    static char ShortStr[2] = {0,0};	/* used for TextOut */
     
    k = 0;
    SetTextColor(hKermDC, GetSysColor(COLOR_WINDOWTEXT));
    while (count > 0) {
	ch = Buffer[k++] & 127;		/* strip parity */
	count -= 1;
	switch(ch) {
	    case 7:			/* bell */
		MessageBeep(0);
		break;
	    case 10:			/* line feed */
	        Ypos += CharHeight;
		CurrentLine += LineIncrement;
		ScreenBuf[CurrentLine + PosInLine] = NULL;
		if (PosInLine > 0) {
		    int i;
		    for (i = 0; i < PosInLine;i++)
			ScreenBuf[CurrentLine + i] = ' ';
		}
		break;
	    case 13:			/* carriage return */
	        Xpos = 0;
		PosInLine = 0;
		break;
	    case 8:			/* delete and backspace */
	    case 127:
	        if (Xpos > 0) {
	            Xpos -= CharWidth;
		    PosInLine -= 1;
		    ScreenBuf[CurrentLine + PosInLine] = NULL;
		}
		break;
	    default:
/*
 * Display other characters.  At the moment, control characters are
 * being displayed.  Need to add a 'transparent' operation to menu.
*/
	        if (ch < 32) {
		    ShortStr[0] = ch;
		    TextOut(hKermDC, Xpos, Ypos, (LPSTR)ShortStr, 1);
		    Xpos += CharWidth;
		}
		else {
		    ShortStr[0] = ch;
		    TextOut(hKermDC, Xpos, Ypos, (LPSTR)ShortStr, 1);
		    Xpos += CharWidth;
		}
		ScreenBuf[CurrentLine + PosInLine++] = ch;
		ScreenBuf[CurrentLine + PosInLine] = NULL;
		break;
	}
	if (Xpos >= MaxDevCoorLen) {
	    Xpos = 0;
	    Ypos += CharHeight;
	    PosInLine = 0;
	    CurrentLine += LineIncrement;
	    ScreenBuf[CurrentLine] = NULL;
	}
	if (CurrentLine >= MaxChars) {
	    CurrentLine = 0;
	    ScreenBuf[CurrentLine] = NULL;
	}
	if (Ypos >= YClientSize) {
	    Xpos = 0;
	    Ypos = YClientSize - CharHeight;
	    PosInLine = 0;
	    FirstLine += LineIncrement;
	    if (FirstLine >= MaxChars)
		FirstLine = 0;		    	
	    ScrollWindow(hKermWnd,0,-CharHeight, NULL, NULL);
	    UpdateWindow(hKermWnd);		/* needed after a scroll */
	}
    }
}
