#include    <stdio.h>
#include    <egb.h>
#include    <fmc.h>
#include    "CONSOL.H"
#include    "OAKLIB.H"
#include    "OAK2.H"
#include    "LEDIT.H"
#include    "GRAPHIC.H"

extern int cur_x;
extern int cur_y;
extern int xposit;
extern int yposit;

int kb_act;


void INKEY_start(void)
{
	kb_act = 1;
	 KYB_clrbuf();
	putmode(0,0,NULL);
}

void INKEY_end(void)
{
	kb_act = 0;
	EGB_printf(71, 24, 0, 7, "         ");
}

void put_put(int x, int y, int len, int pos, char *str, char *att)
{
	int  i, col, fc;
	char tmp[4];
	static int kan_col[]={ 15,1,2,3,4,5,14,7,0,9,10,11,12,13,14,8 };

	for ( i = 0 ; i < len ; ) {
		col = kan_col[*att];
		if ( i == pos )
		col = 9;

		fc=15;
		if ( col == 14 ) fc=0;
		if ( col == 15 ) fc=0;

		color(fc, col);

		if ( iskanji(*str) && iskanji2(*str+1) ) {
			tmp[0] = *(str++);
			tmp[1] = *(str++);
			tmp[2] = '\0';
			if ( (x + 16) > 640 ) {
				x = 0;
				y += 19;
			}
			put_str(x/8, y/19, tmp);
			x += 16;
			att += 2;
			i += 2;
		} else {
			tmp[0] = *(str++);
			tmp[1] = '\0';
			if ( (x + 8) > 640 ) {
				x = 0;
				y += 19;
			}
			put_str(x/8, y/19, tmp);
			x += 8;
			att++;
			i++;
		}
	}			/* for */
}

void putmode(int md, int sf, char *str)
{
	static char *sts_str=NULL;

	if ( str != NULL ) sts_str = str;

	if ( sts_str != NULL && kb_act != 0 ) EGB_printf(71, 24, 15, 1, sts_str);
	csr_posit(xposit, yposit);
}

void putsys(int len, char *str, char *att)
{
	static BLOCK *save=NULL;

	if ( save != NULL ) {
		DSP_pop_vram(save);
		save = NULL;
	}

	if ( len > 0 ) {
		cursor_off;
		save = DSP_push_vram(0, 464, len*8, 479);
		cursor_on;
		put_put(0, 464, len, 256, str, att);
	}
}

void putstr(int pos, int len, char *str, char *att)
{
	static BLOCK *save1=NULL;
	static BLOCK *save2=NULL;
	int n;

	cursor_off;
	if ( save1 != NULL ) {
		DSP_pop_vram(save1);
		save1 = NULL;
	}
	if ( save2 != NULL ) {
		DSP_pop_vram(save2);
		save2 = NULL;
	}

	if ( len > 0 ) {
		if ( (n = cur_x+len*8) > 640 ) {
			n -= (624);
			save1 = DSP_push_vram(cur_x,cur_y,639,cur_y+15);
			save2 = DSP_push_vram(0,cur_y+19,n,cur_y+34);
		} else
			save1 = DSP_push_vram(cur_x,cur_y,n,cur_y+15);

		put_put(cur_x, cur_y, len, pos, str, att);
	}
	else {
		csr_posit(xposit, yposit);	/* 文字が無い時はｶｰｿﾙは、元の位置 */
	}
	cursor_on;
}

