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 >
C/C++ Source or Header  |  1994-11-29  |  7KB  |  125 lines

  1. /*****************************************************************************\
  2. **                                                                           **
  3. **  WW     WW IIIIIIII NNN   NN DDDDDDD  BBBBBBB     AA     SSSSSS EEEEEEEE  **
  4. **  WW  W  WW    II    NNNN  NN DD    DD BB    BB  AA  AA  SS      EE        **
  5. **  WW  W  WW    II    NN NN NN DD    DD BBBBBBB  AAAAAAAA  SSSSSS EEEEEE    **
  6. **   WW W WW     II    NN  NNNN DD    DD BB    BB AA    AA      SS EE        **
  7. **    WWWWW   IIIIIIII NN   NNN DDDDDDD  BBBBBBB  AA    AA SSSSSS  EEEEEEEE  **
  8. **                                                                           **
  9. **   SSSSSS  OOOOOO  FFFFFFFF TTTTTTTT WW     WW    AA    RRRRRRR  EEEEEEEE  **
  10. **  SS      OO    OO FF          TT    WW  W  WW  AA  AA  RR    RR EE        **
  11. **   SSSSS  OO    OO FFFFF       TT    WW  W  WW AAAAAAAA RRRRRRR  EEEEEE    **
  12. **       SS OO    OO FF          TT     WW W WW  AA    AA RR   RR  EE        **
  13. **  SSSSSS   OOOOOO  FF          TT      WWWWW   AA    AA RR    RR EEEEEEEE  **
  14. **                                                                           **
  15. *********** NOTICE ************************************************************
  16. **        This file contains valuable trade secrets and proprietary          **
  17. **        assets of Windbase Software Inc.  Embodying substantial            **
  18. **        creative efforts and confidential information.  Unauthorized       **
  19. **        use, copying, decompiling, translating, disclosure or              **
  20. **        transfer, of any kind, is strictly prohibited.                     **
  21. **                                                                           **
  22. **        COPYRIGHT (C) 1992, 1993, 1994.  Windbase Software Inc.            **
  23. **        ALL RIGHTS RESERVED.                                               **
  24. \*****************************************************************************/
  25.  
  26. /*************************************************************************\
  27. **** Queue Demo **********************************************************
  28. **
  29. **  The following program is an example of using a queue as a ring buffer
  30. **  for storing key presses.  When a stack, queue or dequeue is opened
  31. **  as an array it internally works as a ring buffer.  The other option
  32. **  for opening a queue is to open the queue using linked list allocation.
  33. **  With linked list allocation there is no imposed limit to the queue.
  34. **  The only limit is to the amount of memory available to the queue.
  35. **  With linked list allocation the queue grows and shrinks dynamically
  36. **  using only the amount of memory it needs.
  37. **
  38. **  This program simply stores up to 256 key presses in a queue, using
  39. **  array allocation.  As the user presses keys it will print the key
  40. **  code and the name of the key.  If while attempting to return a key
  41. **  press for printing, another key is pressed, it will not return any
  42. **  of the keys in the queue until all keys are collected and the
  43. **  keyboard is idle.  As the system approaches a keyboard idle
  44. **  condition the GetKey() function will start returning keys.
  45. **
  46. \*************************************************************************/
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50.  
  51. #include "../../memsl.h"
  52. #include "getkey.h"
  53.  
  54. #define MAXKEYS 256    /* The maximum number of keys allowed in the queue */
  55.  
  56.   /*
  57.   **  The key names that are printed
  58.   */
  59. char *keystrs[] = { "KEY_UP", "KEY_PGUP", "KEY_LEFT", "KEY_RIGHT", "KEY_DN",
  60.                     "KEY_PGDN", "KEY_EXIT", "KEY_UPDATE", "KEY_INSERT",
  61.                     "KEY_DELETE", "KEY_BACKSPACE", "KEY_HOME", "KEY_END",
  62.                     "KEY_ENTER", "KEY_F1", "KEY_F2", "KEY_F3", "KEY_F4",
  63.                     "KEY_F5", "KEY_F6", "KEY_F7", "KEY_F8", "KEY_F9",
  64.                     "KEY_F10", "KEY_F11", "KEY_F12", "KEY_SF1", "KEY_SF2",
  65.                     "KEY_SF3", "KEY_SF4", "KEY_SF5", "KEY_SF6", "KEY_SF7",
  66.                     "KEY_SF8", "KEY_SF9", "KEY_SF10", "KEY_SF11", "KEY_SF12",
  67.                     "KEY_CF1", "KEY_CF2", "KEY_CF3", "KEY_CF4", "KEY_CF5",
  68.                     "KEY_CF6", "KEY_CF7", "KEY_CF8", "KEY_CF9", "KEY_CF10",
  69.                     "KEY_CF11", "KEY_CF12", "KEY_AF1", "KEY_AF2", "KEY_AF3",
  70.                     "KEY_AF4", "KEY_AF5", "KEY_AF6", "KEY_AF7", "KEY_AF8",
  71.                     "KEY_AF9", "KEY_AF10", "KEY_AF11", "KEY_AF12",
  72.                     "KEY_CPGDN", "KEY_CPGUP", "KEY_CLEFT", "KEY_CRIGHT",
  73.                     "KEY_CINSERT", "KEY_CDELETE", "KEY_SUP", "KEY_SPGUP",
  74.                     "KEY_SDN", "KEY_SPGDN", "KEY_SCPGDN", "KEY_SCPGUP",
  75.                     "KEY_SINSERT", "KEY_SDELETE", "KEY_SLEFT", "KEY_SRIGHT",
  76.                     "KEY_SHOME", "KEY_SEND", "KEY_TAB" };
  77.  
  78. /*************************************************************************\
  79. ** main() simply opens the queue and starts calling GetKey() until the
  80. ** KEY_EXIT (<ESC>) key is pressed.  main() then closes the queue and
  81. ** exits.  While keys are being pressed main() will print the key value,
  82. ** the defined key code and the number of keys currently sitting in the
  83. ** queue waiting to be processed.
  84. \*************************************************************************/
  85.  
  86. #ifdef WBSTDC
  87.   int main(void)
  88. #else
  89.   int main()
  90. #endif
  91.   {
  92.     WBQUEUE *keybuff;
  93.     int ch;
  94.  
  95.     printf("\n\nThe keydemo program is an example of using a queue for a ring\n");
  96.     printf("buffer.  The keybuff queue defined in the keydemo.c file is a\n");
  97.     printf("queue opened with the following call:\n\n");
  98.     printf("         keybuff = WBQueueOpen(NULL,sizeof(int),MAXKEYS,1);\n\n");
  99.     printf("The queue is opened with sizeof(int)*MAXKEYS size storage.  The\n");
  100.     printf("last argument defines whether the item or the items address will\n");
  101.     printf("be copied into the storage space allocated.  If the MAXKEYS argument\n");
  102.     printf("were 0, then the queue would use a linked list instead of an array\n");
  103.     printf("giving the queue an unlimited amount of storage (based on available\n");
  104.     printf("memory).\n\n");
  105.     printf("The object of this demo is to demonstrate the ring buffer capabilities\n");
  106.     printf("of a queue as it is used for buffering the keyboard.\n\n");
  107.     printf("Press any key or number of keys to run the buffer up to its limit.\n");
  108.     printf("Press <ESC> when done.\n\n");
  109.  
  110.     if ((keybuff = WBQueueOpen(NULL,sizeof(int),MAXKEYS,1)) != NULL)
  111.       {
  112.         while ((ch = GetKey(keybuff)) != KEY_EXIT)
  113.           if (ch > 31 && ch < 127)
  114.             printf("key pressed: \"%c\"  keyval: %d  Number of keys in buffer: %ld\n",(char)ch,ch,WBQueueNumItems(keybuff));
  115.           else if (ch > 299)
  116.             printf("key pressed: \"%s\"  keyval: %d  Number of keys in buffer %ld\n",keystrs[ch-300],ch,WBQueueNumItems(keybuff));
  117.           else
  118.             printf("key pressed: \"Unknown\"  keyval: %d  Number of keys in buffer %ld\n",ch,WBQueueNumItems(keybuff));
  119.  
  120.         WBQueueClose(keybuff);
  121.       }
  122.     exit(0);
  123.     return(0);
  124.   }
  125.