# include "Ximage.h"
# define EventMask (long) (KeyPressMask | ExposureMask | StructureNotifyMask)
# define EMask (long) (KeyPressMask | ExposureMask | StructureNotifyMask | PointerMotionMask)

int EventLoop () {
  
  XEvent          event, reconfig_event;
  int             status, Ne;
  int  NeedRefresh, NeedReconfig;
  
  Refresh (1);

  NeedReconfig = FALSE;
  NeedRefresh = FALSE;
  status = TRUE;
  while (status) {

    if (!CheckPipe ()) return (FALSE);

    if (XEventsQueued (graphic.display, QueuedAfterFlush) < 1) {
      if (NeedReconfig) status = Reconfig (&reconfig_event);
      if (NeedRefresh && !NeedReconfig) Refresh (1);
      NeedRefresh = FALSE;
      NeedReconfig = FALSE;
      usleep (10000);
      continue;
    }

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

      switch (event.type)  {

      case MotionNotify:
	break;

      case Expose:
	if (HAVE_BACKING) continue;
	NeedRefresh = TRUE;
	break;
	
      case ConfigureNotify:
	/* Ne = XEventsQueued (graphic.display, QueuedAlready); */
	reconfig_event = event;
	NeedReconfig = TRUE;
	break;
	
      case KeyPress:
	status = InterpretKeys (&event);
	break;

      case MappingNotify:
	if (XEventsQueued (graphic.display, QueuedAlready) < 2) {
	  XRefreshKeyboardMapping ((XMappingEvent *) &event);
	}
	break;
	
      }
    }
  }
  return (status);
}

