/*

The compilation of software known as FreeBSD GraphicBoot is distributed under
the following terms:

Copyright (C) 2005 Matthew Holder. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

$SixXGate: motifapps/graphicboot/console.c,v 1.3 2005/07/27 22:07:53 matthew Exp $

*/

#define _CONSOLE_C_1700E96A_FE1A_11D9_A94B_0050FC0DCA2A

#include "graphicboot.h"

XmString szGeneric;

/**
 * Attaches the console to a pseudo-terminal and opens that terminal
 * to read messages.
 *
 * @param (in) The widget to attach the pseudo-terminal to
 * @return True on success or False on error
 */
CONSOLECALL int OpenConsole()
{
	if (GetPTerm(&nTTYFile, &nPTYFile))
	{
		input_id = XtAppAddInput
		(
			XtWidgetToApplicationContext(txtConsole),
			nPTYFile, (XtPointer) XtInputReadMask,
			(XtInputCallbackProc)ProcessLine, txtConsole
		);
		return True;
	} else return False;
}

/**
 * Searches for an availible Pseudo-Terminal and opens the two devices files
 * for it.
 *
 * @param (out) The file descriptor of the Teletype Terminal file
 * @param (out) The file descriptor of the Pseudo Terminal file
 * @return True on success false on error
 */
CONSOLECALL int GetPTerm(int *pnTTY, int *pnPTY)
{
	char szTTYDev[11], szPTYDev[11], *szTerm;
	int nOn = 1;
	for (szTerm = "0123456789abcdef"; *szTerm != 0; szTerm++)
	{
		sprintf(szTTYDev, "/dev/ttyp%c", *szTerm);
		sprintf(szPTYDev, "/dev/ptyp%c", *szTerm);
		if
		(
			(*pnPTY = open(szPTYDev, O_RDWR)) >= 0 &&
			(*pnTTY = open(szTTYDev, O_RDWR)) >= 0 &&
			ioctl(*pnPTY, TIOCCONS, (char*)&nOn) != -1
		) return True;	
		
		if (*pnTTY >= 0) close(*pnTTY);
		if (*pnPTY >= 0) close(*pnPTY);
	}
	return False;
}

/**
 * Gets a line of input from a file as it is writen(received).
 */
CONSOLECALL void ProcessLine (Widget pWidget, int *pnFile, XtInputId *pID)
{
	char szBuffer[MAX_LINE_SIZE];
	char *sz;
	int nBytes;
	XmTextPosition nTextPos;

	nBytes = read(*pnFile, szBuffer, MAX_LINE_SIZE);
	if (nBytes != 0)
	{
		for (sz = szBuffer; *sz != 0; sz++) if (*sz == '\r') *sz = ' ';
		if (memcmp(szBuffer, "_XGB_STAT: ", 11) == 0)
		{	/* Change the status message */
			SetStatusMessage(szBuffer + 11);
		}
		else if (memcmp(szBuffer, "_XGB_EGB", 8) == 0)
		{
			/* End the graphical boot */
			CleanupAndExit();
		}
		else
		{
			/* Add the line to the text widget */
			szBuffer[nBytes] = '\0';
			nTextPos = XmTextGetLastPosition(pWidget);
			XmTextInsert(pWidget, nTextPos, szBuffer);
			nTextPos = XmTextGetLastPosition(pWidget);
			XtVaSetValues
			(
				pWidget,
				XmNcursorPosition, nTextPos,
				NULL
			);
			XmTextShowPosition(pWidget, nTextPos);
		}			
		fflush(stdout);
	} else XtRemoveInput(*pID);
}

CONSOLECALL int SetStatusMessage(char *szMessage)
{
	szGeneric = XmStringCreateLocalized(szMessage);
	XtVaSetValues
	(
		lblStatus,
		XmNlabelString, szGeneric,
		NULL
	);
	XmUpdateDisplay(lblStatus);
	XmStringFree(szGeneric);
	
	return TRUE;
}


syntax highlighted by Code2HTML, v. 0.9.1