#include <stddef.h>
#include <tos.h>
#include "jclkcook.h"

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

JCLKSTRUCT *clocky = NULL;
char *hotkey = NULL;
int ehc_id = 0;

int	cookie (long cookie, long *value);

/*
** Initialise hotkeys
*/
int initHotkeys(void)
{
	int used, i;

	/* Required version of Clocky innstalled? */
	if (cookie('JCLK', (void *)&clocky))
	{
		if (clocky->name != CLOCKY_IDENT || clocky->version < CLOCKY_VERSION)
			return FALSE; /* Needs a newer version */
	}
	else
		return FALSE;	/* Not installed at all */
	
	hotkey = (char *)(&clocky->actual_key);

	/* Get a EHC ID */
	/* <Petr Stehlik> */
	do {
		ehc_id++;
		used = FALSE;
		for(i = 0; i < 128; i++)
		{
			if (clocky->ehc_table[i] == ehc_id)
			{
				used = TRUE;	/* this id is already in use */
				break;			/* try higher mark */
			}
		}
	} while(used && (ehc_id < 128));
	/* </Petr Stehlik> */

	if (ehc_id > 127) /* Too many hotkey-clients */
		return FALSE;

	/* OK, return the EHC ID */
	return ehc_id;
}


/*
** Register a hotkey
*/

int registerHotkey(char key)
{
	/* Successfully initialised? */
	if (!ehc_id)
		return FALSE;

	/* Free key? */
	if (clocky->ehc_table[key] > 0)
		return FALSE;
		
	/* Register key */
	clocky->ehc_table[key] = ehc_id;
	return ehc_id;
}


/*
** Remove registered hotkeys
**
** key == -1 -> Remove all registered hotkeys
** otherwise remove 'key'.
*/

void removeHotkeys(int key)
{
	int teller;

	if (ehc_id != 0)
	{
		if (key == -1) /* Remove all registered keys */
		{
			for (teller = 0; teller < 128; teller++)
				if (clocky->ehc_table[teller] == ehc_id)
					clocky->ehc_table[teller] = 0;
		}
		else	/* Remove 'key' */
		{
			if (clocky->ehc_table[key] == ehc_id)
				clocky->ehc_table[key] = 0;
		}
	}
}


/*
** Check hotkeys
*/

char checkHotkeys(void)
{
	char retValue;

	/* Return ASAP if no hotkey is pressed */
	if (*hotkey == 0)
		return 0;

	/* Hotkey pressed, but is it mine? */
	if (clocky->ehc_table[*hotkey] == ehc_id)
	{
		retValue = *hotkey;
		*hotkey = 0;
		return retValue; /* Return hotkey */
	}

	/* Nope */
	return 0;
}


/*
** Scancode -> ASCII
*/

char scan2ascii(char scan)
{
	struct keytab {
		unsigned char *unshift;
		unsigned char *shift;
		unsigned char *capslock;
		} *ktab;

	ktab = (struct keytab *)Keytbl((char *)-1,(char *)-1,(char *)-1);
	return ktab->unshift[scan];
}


/*
** ASCII -> Scancode
*/

char ascii2scan(char ascii)
{
	int teller;

	/* Test all scancodes until a match is found */
	for (teller = 0; teller < 128; teller++)
		if (scan2ascii(teller) == ascii)
			return teller;

	/* Hmmm... It doesn't have a key */			
	return 0;
}


/*
** Get the value of a cookie
** Stolen from E-Gem 2.10
*/

int	cookie (long cookie, long *value)
{
	register long *cookiejar, old_stack;
	
	old_stack = Super (NULL);
	cookiejar = *((long **) 0x5a0l);
	Super ((void *) old_stack);
	
	if (cookiejar)
	{
		while (*cookiejar)
		{
			if (*cookiejar==cookie)
			{
				if (value)
					*value = *++cookiejar;
				return(TRUE);
			}
			cookiejar += 2;
		}
	}
	return(FALSE);
}
