#include "kdefines.h"
#include "scan.h"
#include "myscan.h"
#include "shifts.h"
#include "scn_tbl.h"
#include "msc_tbl.h"
#include <assert.h>


#include "tcodes.h" /* safeguard - avoid conflicting types */

/* i know this is ugly, but it improves readability and saves keystrokes */
#define FIX_PAD(x, y)	\
	case SCAN_PAD_##x##: \
	case SCAN_CTRL_PAD_##x##: \
		if(numlock && a != 224){ \
			t = MYSCAN_PAD_##x##; \
		}else{ \
			t = MYSCAN_##y##; \
		} \
		break

/* scan code to tcode conversion - solves scan code overlaying problem */
t_tcode scan_to_tcode(t_scan s, t_ascii a, t_flags f){
	t_tcode t = MYSCAN_NONE;
	char numlock = 0;
	char finished = 0;
  
/*	assert(s >= 0); */
	assert(s < 256);
/*	assert(a >= 0); */
	assert(a < 256);
	if(!s && !a && MATCH_ALL(f, MASK_CTRL|MASK_SCROLL_PRESS)) /* Ctrl-Break */
		return MYSCAN_PAUSE;
	if(!s) /* Plain ascii code */
		return MYSCAN_PLAIN;
	/* Fix numlocks + NUM part of keyb. */
	numlock = MATCH_ANY(f, MASK_NUM_LOCK | MASK_NUM_PRESS);
	finished = 1;
	switch(s){
		FIX_PAD(0, INS);
		FIX_PAD(1, END);
		FIX_PAD(2, DOWN);
		FIX_PAD(3, PGDN);
		FIX_PAD(4, LEFT);
		FIX_PAD(5, NONE);
		FIX_PAD(6, RIGHT);
		FIX_PAD(7, HOME);
		FIX_PAD(8, UP);
		FIX_PAD(9, PGUP);
		FIX_PAD(DOT, DEL);
		case SCAN_PAD_SLASH:
/*		case SCAN_PAD_ENTER:*/
/*		case SCAN_CTRL_PAD_ENTER:*/
			if(a == '/'){
				t = MYSCAN_PAD_SLASH;
			}else{
				t = MYSCAN_PAD_ENTER;
			}
			break;
		case SCAN_CTRL_PAD_SLASH:
		case SCAN_ALT_PAD_SLASH:
			t = MYSCAN_PAD_SLASH;
			break;
		case SCAN_ALT_PAD_ENTER:
			t = MYSCAN_PAD_ENTER;
			break;
		case SCAN_PAD_STAR:
		case SCAN_CTRL_PAD_STAR:
/*		case SCAN_ALT_PAD_STAR:*/
			/* Warn: 'ALT','' + '*' possibly overlaps with prtscr ! */
			t = MYSCAN_PAD_STAR;
			break;
		case SCAN_PAD_MINUS:
		case SCAN_CTRL_PAD_MINUS:
/*		case SCAN_ALT_PAD_MINUS:*/
			t = MYSCAN_PAD_MINUS;
			break;
		case SCAN_PAD_PLUS:
		case SCAN_CTRL_PAD_PLUS:
/*		case SCAN_ALT_PAD_PLUS:*/
			t = MYSCAN_PAD_PLUS;
			break;
		default:
			finished = 0;
			break;
	}
	if(finished)
		return t;
	/* OK, We have finished numlocks + NUM part of keyb. ... */

	/* Let's finish the job ... */
	t = scancodes_table[s];
	if(t != MYSCAN_NONE)
		return t;
	assert(! "Problem converting to tcode ... :( ");
	return t; /* never reached, gcc, shut up! */
}

/* tcode to ascii conversion */
char *tcode_to_ascii(t_tcode t){
	return myscan_asc_table[t];
}

/* flags to ascii conversion */
char *flags_to_ascii(t_flags f){
	static char flgs[9];

	flgs[0] = MATCH_ANY(f, MASK_RSHIFT|MASK_LSHIFT) ? 's' : '-';
	flgs[1] = MATCH_ANY(f, MASK_CTRL) ? 'c' : '-';
	flgs[2] = MATCH_ANY(f, MASK_ALT) ? 'a' : '-';
	flgs[3] = MATCH_ANY(f, MASK_SCROLL_LOCK|MASK_SCROLL_PRESS) ? 'S' : '-';
	flgs[4] = MATCH_ANY(f, MASK_NUM_LOCK|MASK_NUM_PRESS) ? 'N' : '-';
	flgs[5] = MATCH_ANY(f, MASK_CAPS_LOCK|MASK_CAPS_PRESS) ? 'C' : '-';
	flgs[6] = MATCH_ANY(f, MASK_INSERT) ? 'i' : '-';
	flgs[7] = MATCH_ANY(f, MASK_SYSRQ_PRESS) ? 's' : '-';
	flgs[8] = 0;
	return flgs;
}

/* vim: set ts=4 cindent shiftwidth=4 tw=0 : */
