home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ User's Journal & Wi…eveloper's Journal Tools
/
C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso
/
windbase
/
memslc.3
/
queue
/
keydemo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-29
|
7KB
|
125 lines
/*****************************************************************************\
** **
** WW WW IIIIIIII NNN NN DDDDDDD BBBBBBB AA SSSSSS EEEEEEEE **
** WW W WW II NNNN NN DD DD BB BB AA AA SS EE **
** WW W WW II NN NN NN DD DD BBBBBBB AAAAAAAA SSSSSS EEEEEE **
** WW W WW II NN NNNN DD DD BB BB AA AA SS EE **
** WWWWW IIIIIIII NN NNN DDDDDDD BBBBBBB AA AA SSSSSS EEEEEEEE **
** **
** SSSSSS OOOOOO FFFFFFFF TTTTTTTT WW WW AA RRRRRRR EEEEEEEE **
** SS OO OO FF TT WW W WW AA AA RR RR EE **
** SSSSS OO OO FFFFF TT WW W WW AAAAAAAA RRRRRRR EEEEEE **
** SS OO OO FF TT WW W WW AA AA RR RR EE **
** SSSSSS OOOOOO FF TT WWWWW AA AA RR RR EEEEEEEE **
** **
*********** NOTICE ************************************************************
** This file contains valuable trade secrets and proprietary **
** assets of Windbase Software Inc. Embodying substantial **
** creative efforts and confidential information. Unauthorized **
** use, copying, decompiling, translating, disclosure or **
** transfer, of any kind, is strictly prohibited. **
** **
** COPYRIGHT (C) 1992, 1993, 1994. Windbase Software Inc. **
** ALL RIGHTS RESERVED. **
\*****************************************************************************/
/*************************************************************************\
**** Queue Demo **********************************************************
**
** The following program is an example of using a queue as a ring buffer
** for storing key presses. When a stack, queue or dequeue is opened
** as an array it internally works as a ring buffer. The other option
** for opening a queue is to open the queue using linked list allocation.
** With linked list allocation there is no imposed limit to the queue.
** The only limit is to the amount of memory available to the queue.
** With linked list allocation the queue grows and shrinks dynamically
** using only the amount of memory it needs.
**
** This program simply stores up to 256 key presses in a queue, using
** array allocation. As the user presses keys it will print the key
** code and the name of the key. If while attempting to return a key
** press for printing, another key is pressed, it will not return any
** of the keys in the queue until all keys are collected and the
** keyboard is idle. As the system approaches a keyboard idle
** condition the GetKey() function will start returning keys.
**
\*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "../../memsl.h"
#include "getkey.h"
#define MAXKEYS 256 /* The maximum number of keys allowed in the queue */
/*
** The key names that are printed
*/
char *keystrs[] = { "KEY_UP", "KEY_PGUP", "KEY_LEFT", "KEY_RIGHT", "KEY_DN",
"KEY_PGDN", "KEY_EXIT", "KEY_UPDATE", "KEY_INSERT",
"KEY_DELETE", "KEY_BACKSPACE", "KEY_HOME", "KEY_END",
"KEY_ENTER", "KEY_F1", "KEY_F2", "KEY_F3", "KEY_F4",
"KEY_F5", "KEY_F6", "KEY_F7", "KEY_F8", "KEY_F9",
"KEY_F10", "KEY_F11", "KEY_F12", "KEY_SF1", "KEY_SF2",
"KEY_SF3", "KEY_SF4", "KEY_SF5", "KEY_SF6", "KEY_SF7",
"KEY_SF8", "KEY_SF9", "KEY_SF10", "KEY_SF11", "KEY_SF12",
"KEY_CF1", "KEY_CF2", "KEY_CF3", "KEY_CF4", "KEY_CF5",
"KEY_CF6", "KEY_CF7", "KEY_CF8", "KEY_CF9", "KEY_CF10",
"KEY_CF11", "KEY_CF12", "KEY_AF1", "KEY_AF2", "KEY_AF3",
"KEY_AF4", "KEY_AF5", "KEY_AF6", "KEY_AF7", "KEY_AF8",
"KEY_AF9", "KEY_AF10", "KEY_AF11", "KEY_AF12",
"KEY_CPGDN", "KEY_CPGUP", "KEY_CLEFT", "KEY_CRIGHT",
"KEY_CINSERT", "KEY_CDELETE", "KEY_SUP", "KEY_SPGUP",
"KEY_SDN", "KEY_SPGDN", "KEY_SCPGDN", "KEY_SCPGUP",
"KEY_SINSERT", "KEY_SDELETE", "KEY_SLEFT", "KEY_SRIGHT",
"KEY_SHOME", "KEY_SEND", "KEY_TAB" };
/*************************************************************************\
** main() simply opens the queue and starts calling GetKey() until the
** KEY_EXIT (<ESC>) key is pressed. main() then closes the queue and
** exits. While keys are being pressed main() will print the key value,
** the defined key code and the number of keys currently sitting in the
** queue waiting to be processed.
\*************************************************************************/
#ifdef WBSTDC
int main(void)
#else
int main()
#endif
{
WBQUEUE *keybuff;
int ch;
printf("\n\nThe keydemo program is an example of using a queue for a ring\n");
printf("buffer. The keybuff queue defined in the keydemo.c file is a\n");
printf("queue opened with the following call:\n\n");
printf(" keybuff = WBQueueOpen(NULL,sizeof(int),MAXKEYS,1);\n\n");
printf("The queue is opened with sizeof(int)*MAXKEYS size storage. The\n");
printf("last argument defines whether the item or the items address will\n");
printf("be copied into the storage space allocated. If the MAXKEYS argument\n");
printf("were 0, then the queue would use a linked list instead of an array\n");
printf("giving the queue an unlimited amount of storage (based on available\n");
printf("memory).\n\n");
printf("The object of this demo is to demonstrate the ring buffer capabilities\n");
printf("of a queue as it is used for buffering the keyboard.\n\n");
printf("Press any key or number of keys to run the buffer up to its limit.\n");
printf("Press <ESC> when done.\n\n");
if ((keybuff = WBQueueOpen(NULL,sizeof(int),MAXKEYS,1)) != NULL)
{
while ((ch = GetKey(keybuff)) != KEY_EXIT)
if (ch > 31 && ch < 127)
printf("key pressed: \"%c\" keyval: %d Number of keys in buffer: %ld\n",(char)ch,ch,WBQueueNumItems(keybuff));
else if (ch > 299)
printf("key pressed: \"%s\" keyval: %d Number of keys in buffer %ld\n",keystrs[ch-300],ch,WBQueueNumItems(keybuff));
else
printf("key pressed: \"Unknown\" keyval: %d Number of keys in buffer %ld\n",ch,WBQueueNumItems(keybuff));
WBQueueClose(keybuff);
}
exit(0);
return(0);
}