/*-
 * Copyright (c) 2001
 * Tatsuya Kudoh(CDR/TK),ROYALPANDA.    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 THE 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 THE 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.
 *
 */

#include<stdlib.h>
#include<sys/bios.h>

#pragma noregalo

#include"ks.h"

#define CONSOLE_X	18
#define CONSOLE_Y	14

#define COLUMNS	10
#define LINES	4

static int Cursor;
static int FontOfLine;

void KSInitializeConsole( void )
{
	unsigned buf[COLUMNS*LINES];
	int i;

	for( i = 0 ; i < COLUMNS*LINES ; i++ ){
		buf[i] = KS_PLT_WINDOW | KS_FONT_CONSOLE+i;
		KSSetFontData(1,KS_FONT_CONSOLE+i,0);
	}
	screen_set_char(0,CONSOLE_X,CONSOLE_Y,COLUMNS,LINES,buf);
	Cursor = 0;
	FontOfLine = KS_FONT_CONSOLE + (LINES-1) * COLUMNS;
}

static void scroll( void )
{
	unsigned buf[COLUMNS*LINES];
	int i;
	int f,x,y;

	FontOfLine += COLUMNS;
	if( FontOfLine == KS_FONT_CONSOLE + COLUMNS*LINES )
		FontOfLine = KS_FONT_CONSOLE; 
	for( i = 0 ; i < COLUMNS ; i++ )
		KSSetFontData(1,FontOfLine+i,0);

	i = 0;
	f = FontOfLine + COLUMNS;
	for( y = 0 ; y < LINES ; y++ ){
		if( f == KS_FONT_CONSOLE + COLUMNS*LINES )
			f = KS_FONT_CONSOLE; 
		for( x = 0 ; x < COLUMNS ; x++ ){
			buf[i++] = KS_PLT_WINDOW | f++;
		}
	}
	screen_set_char(0,CONSOLE_X,CONSOLE_Y,COLUMNS,LINES,buf);

	Cursor = 0;
}


void KSWriteConsole( unsigned code )
{
	if( Cursor == COLUMNS )
		scroll();

	if( code == KEYCODE_RETURN ){
		scroll();
	}else{
		KSSetFontData(1,FontOfLine+Cursor,code);
		Cursor++;
	}
}


void KSWriteStringToConsole( unsigned char *str )
{
	unsigned code;

	if( str == NULL )
		return;

	while( *str ){
		code = *str++;
		if( code & 0x80 )
			code = code << 8 | *str++;
		if( (code & 0xff) == 0 )
			break;
		KSWriteConsole(code);
	}
}


void KSWriteFarStringToConsole( unsigned char far *str )
{
	unsigned code;

	if( str == NULL )
		return;

	while( *str ){
		code = *str++;
		if( code & 0x80 )
			code = code << 8 | *str++;
		if( (code & 0xff) == 0 )
			break;
		KSWriteConsole(code);
	}
}

void KSWriteWideStringToConsole( unsigned *str )
{
	if( str == NULL )
		return;

	while( *str )
		KSWriteConsole(*str++);
}

void KSWriteFarWideStringToConsole( unsigned far *str )
{
	if( str == NULL )
		return;

	while( *str )
		KSWriteConsole(*str++);
}
