Category : Files from Magazines
Archive   : ITCMAR91.ZIP
Filename : KYBDBUF1.C

 
Output of file : KYBDBUF1.C contained in archive : ITCMAR91.ZIP

/****************************************************************
* KYBDBUF1.C - manipulate the keyboard buffer *
* To compile: "tcc kybdbuf1" *
****************************************************************/

#include
#include /* exit(), EXIT_FAILURE */
#include
#include
#include /* isgraph() */

/****************************************************************
* Definition of the keyboard's default ring buffer *
****************************************************************/
typedef struct
{
unsigned char ch; /* ASCII value of character (or 0) */
unsigned char sc; /* Scan code of the key pressed */
} KeyBuf;
unsigned far *HeadOffPtr, /* Pointer to head offset of buffer */
far *TailOffPtr, /* Pointer to tail of ring buffer */
far *BegOffPtr, /* Pointer to offset of buffer start*/
far *EndOffPtr; /* Pointer to offset of buffer end */
KeyBuf far *KeyData; /* Pointer to keyboard buffer data */

/****************************************************************
* FlushKeyBuff - flush the characters in the keyboard buffer *
****************************************************************/
void FlushKeyBuff(void)
{
*HeadOffPtr = *TailOffPtr;
}

/****************************************************************
* StuffKeyBuff - stuff text into the keyboard buffer *
* *
* INP: c - character to stuff into the keyboard buffer, *
* (scan code required only for special keys...) *
* OUT: 0 - if everything went well, 1 if the buffer ran *
* out of room. *
****************************************************************/
int StuffKeyBuff(int c)
{
unsigned far *tmp, NewTailOffPtr;

/* Compute address of next character slot in buffer */
tmp = MK_FP(0x0040,*TailOffPtr);

/* Point to next available slot in buffer, wrapping around */
/* to the beginning, if we overrun the end of the buffer */
NewTailOffPtr = *TailOffPtr + 2;
if (NewTailOffPtr >= *EndOffPtr) NewTailOffPtr = *BegOffPtr;

/* Is the buffer full? */
if (NewTailOffPtr == *HeadOffPtr) return 1;

/* Since there's room in the buffer, stuff it in & return */
*tmp = (unsigned int)c;
*TailOffPtr = NewTailOffPtr;
return 0;
}

/****************************************************************
* InitKeyBuff - initialize the pointers to the keyboard ring *
* buffer variables *
****************************************************************/
void InitKeyBuff(void)
{
/* Initialize the constant pointers */
HeadOffPtr = MK_FP(0x0040,0x001a);
TailOffPtr = MK_FP(0x0040,0x001c);
BegOffPtr = MK_FP(0x0040,0x0080);
EndOffPtr = MK_FP(0x0040,0x0082);
KeyData = MK_FP(0x0040,*BegOffPtr);
}

/****************************************************************
* Demonstration main() for the keyboard buffer display and *
* keyboard buffer stuff routines. *
****************************************************************/
void main(void)
{
int i;


clrscr();

InitKeyBuff();

/* Flush the keyboard buffer */
FlushKeyBuff();

/* Stuff something in the keyboard buffer */
for (i=0; i<6; i++)
StuffKeyBuff("pause\r"[i]);

/* Show the important buffer addresses */
printf( "Address of start of buffer: 0040:%04x\n"
" Address of end of buffer: 0040:%04x\n"
" Head points to address: 0040:%04x\n"
" Tail points to address: 0040:%04x\n",
*BegOffPtr, *EndOffPtr, *HeadOffPtr, *TailOffPtr);

/* Exit & perform a 'pause' statement */
}