/*
 * Copyright (C) 1994	Edward Der-Hua Liu, Hsin-Chu, Taiwan
 */

#include "big5con.h"

static char     inch[4];
static int      cin;

void 
init_inter_code(int usenow)
{
    cin = 0;
    if (usenow) {
	gotox(InAreaX);
	set_att(InAreaColor);
	xprintf("        ");
	set_att(NormalColor);
	xprintf("  ");
	gotox(0);
	xprintf(" ¡i¤º½X¡j");
    }
}

static int 
h2i(int x)
{
    return (x <= '9' ? x - '0' : x - 'A' + 10);
}

static char     dstr[] = "¢¯¢°¢±¢²¢³¢´¢µ¢¶¢·¢¸¢Ï¢Ð¢Ñ¢Ò¢Ó¢Ô";

feedkey_intcode(int key)
{
    int             i;
    extern int      cursor_x;

    key = toupper(key);
    if (key == VK_BackSpace || key == VK_Delete) {
	if (cin)
	    cin--;
	else
	    return 0;

	if (!cin) {
	    gotox(InAreaX);
	    xprintf("  ");
	    return 1;
	}
	goto dispIn;
    } else if (key < '0' || key > 'F' || (key > '9' && key < 'A'))
	return 0;

    if (cin == 0 && key < '8')
	return 1;
    if (cin == 1 && inch[0] == 'F' && key == 'F')
	return 1;
    if (cin == 2 && (key < '4' || (key > '7' && key < 'A')))
	return 1;
    if (cin == 3 && (inch[2] == '7' || inch[2] == 'F') && key == 'F')
	return 1;
    if (cin < 4)
	inch[cin++] = key;
dispIn:
    gotox(InAreaX);
    set_att(InAreaColor);
    for (i = 0; i < cin; i++) {
	xprintf("%c%c", dstr[h2i(inch[i]) << 1], dstr[(h2i(inch[i]) << 1) + 1]);
    }
    if (cin < 4)
	xprintf("  ");
    set_att(NormalColor);
    if (cin == 4) {
	u_char          ttt[3];
	ttt[3] = 0;
	ttt[0] = (h2i(inch[0]) << 4) + h2i(inch[1]);
	ttt[1] = (h2i(inch[2]) << 4) + h2i(inch[3]);
	sendkey_b5(ttt);
	cin = 0;
	gotox(InAreaX);
	set_att(InAreaColor);
	xprintf("        ");
	set_att(NormalColor);
    }
    return 1;
}
