home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_12 / 8n12064a < prev    next >
Text File  |  1990-01-10  |  3KB  |  128 lines

  1.  
  2.  
  3. /*      TITLE:      Mouse Event Queue;
  4.  
  5.     FUNCTION:   Handles construction and destruction of an event
  6.             queue to ease MicroSoft-compatible mouse event
  7.             management;
  8.  
  9.     FILENAME:   MOUSEQ.C;
  10.  
  11.     COMPILER:   TURBO C V. 1.5+ Small Memory Model;
  12.  
  13.     REQUIRES:   MOUSEQ.H;
  14.  
  15.     VERSION:    1.0;
  16.     DATE:       12/31/89;
  17.     AUTHOR:     Michael Kelly;
  18.  
  19. */
  20.  
  21.  
  22. #include <stdlib.h>
  23. #include "mouseq.h"
  24.  
  25. static unsigned int num_links = 0;
  26. EVENT *head = NULL, *tail = NULL;
  27.  
  28. /*
  29.  *  int set_que(unsigned int que_entries);
  30.  *
  31.  *  attempts to create a circular, singly linked list large
  32.  *  enough to store "que_entries" number of mouse events
  33.  *
  34.  *  on success:     head and tail point to first link in list
  35.  *                  local variable num_links equals que_entries
  36.  *
  37.  *  returns:        1
  38.  *
  39.  *  on failure:     calls free_que() to free memory allocated
  40.  *                  before an allocation failure, if any
  41.  *
  42.  *  returns:        0
  43.  */
  44. int set_que(unsigned int que_entries)
  45. {
  46.     if(que_entries < 3)
  47.     return 0;
  48.  
  49.     head = (EVENT *) malloc(sizeof(EVENT));
  50.     if(! head)
  51.     return(0);
  52.  
  53.     tail = head;
  54.     head->next = NULL;
  55.     head->xcoord = 0;
  56.     head->ycoord = 0;
  57.     head->buttonbits = 0;
  58.     head->maskbits = 0;
  59.     head->valid = 0;
  60.  
  61.     num_links = 1;
  62.  
  63.     while(--que_entries)  {
  64.     tail->next = NULL;
  65.     tail->next = (EVENT *) malloc(sizeof(EVENT));
  66.     if(! tail->next)  {
  67.         free_que();
  68.         return 0;
  69.     }
  70.     else
  71.         ++num_links;
  72.  
  73.     /*
  74.      *  could have used calloc() to zero members of EVENT structure
  75.      *  but for this example it is done explicitly
  76.      */
  77.     tail = tail->next;
  78.     tail->xcoord = 0;
  79.     tail->ycoord = 0;
  80.     tail->buttonbits = 0;
  81.     tail->maskbits = 0;
  82.     tail->valid = 0;
  83.     }
  84.  
  85.     /*
  86.      *  make queue circular, then point head and tail
  87.      *  to first link in list
  88.      *  if handler() sees tail->next == head, it assumes the
  89.      *  event queue is full, and returns to the mouse driver
  90.      */
  91.     tail->next = head;
  92.     tail = tail->next;
  93.  
  94.     return 1;
  95. }
  96.  
  97. void free_que()
  98. {
  99.     EVENT *linkptr = NULL, *prev_linkptr = NULL;
  100.  
  101.     if(! num_links)     /* num_links set by set_que() */
  102.     return;
  103.  
  104.  
  105.     linkptr = head->next;
  106.  
  107.     /*
  108.      *  save link pointer int prev_linkptr
  109.      *  bump link pointer to next position
  110.      *  then free the previous link pointer
  111.      */
  112.     do  {
  113.  
  114.     prev_linkptr = linkptr;
  115.     linkptr = linkptr->next;
  116.     if(prev_linkptr)
  117.         free(prev_linkptr);
  118.  
  119.     }
  120.     while(--num_links);
  121.  
  122.     if(head)
  123.     free(head);
  124.  
  125.      head = tail = NULL;
  126. }
  127.  
  128.