# include "Ximage.h"
# define MAX_STRING 100
/* WARNING: this is defined because the HP UX installation i have does not
seem to know this structure, though it should */
# ifdef DUMMY
struct Ktimeval {
  unsigned long tv_sec;         /* seconds */
  long          tv_usec;        /* and microseconds */
};
# endif

int EventLoop (Graphic *graphic, Layout *layout) {
  
  XEvent          event;
  int             status;
  
  /* gettimeofday (&then, (struct timeval *) NULL); */

  Refresh (graphic, layout, 1);

  status = TRUE;
  while (status) {
    
    if (!CheckPipe (graphic, layout))
      return (FALSE);
    
    if (XEventsQueued (graphic[0].display, QueuedAfterFlush) < 1) {
      usleep (10000);
      continue;
    }

    if (XCheckMaskEvent (graphic[0].display, EVENT_MASK, &event)) {

      switch (event.type)  {
	
      case MotionNotify:
	if (XEventsQueued (graphic[0].display, QueuedAlready) < 2) {
	  UpdatePointer (graphic, layout, (XMotionEvent *) &event);
	}
	break;
      case ButtonPress:
	status = InterpretPresses (graphic, layout, (XButtonEvent *) &event);
	break;
	
      case Expose:
	if (XEventsQueued (graphic[0].display, QueuedAlready) < 2) {
	  Refresh (graphic, layout, 1);
	}
	break;
	/* if (event.xexpose.count == 0) */
	
      case ConfigureNotify:
	status = Reconfig (graphic, layout, &event);
	/*
	if (XEventsQueued (graphic[0].display, QueuedAlready) < 2) {
	  status = Reconfig (graphic, layout, &event);
	}
	*/
	break;
	
      case KeyPress:
	status = InterpretKeys (graphic, layout, &event);
	break;
	
      case MappingNotify:
	if (XEventsQueued (graphic[0].display, QueuedAlready) < 2) {
	  XRefreshKeyboardMapping ((XMappingEvent *) &event);
	}
	break;
	
      }
    }
  }
  return (status);
}

/*     
while (XPending (graphic[0].display) {
if (readfds && display_mask) { 
*/
/*
  while (1) {
    fprintf (stderr, "window in EventLoop: %x  %d\n", graphic[0].display, graphic[0].window);
    XNextEvent (graphic[0].display, &event);
    if (event.type == ClientMessage) {
      fprintf (stderr, "choice 1\n");
    }
    
    if (event.xclient.send_event) {
      fprintf (stderr, "choice 2\n");
    }
  }
*/
  
