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

int cursor () {

  XEvent          event;
  KeySym          keysym;
  int             status, xstatus, value;
  XComposeStatus  composestatus;
  char            string[10], line[35];
  double          x, y;
  char            buffer[10];
  Layout         *layout;
  
  layout = &section[TheSection];

  while (1) {
    
    /* check the pipe for messages */
    status = read (sock, buffer, 4);
    buffer[4] = 0;
    
    switch (status) {
    case -1:
      /* no input from pipe: continue */
      break;
      
    case 0:
      fprintf (stderr, "pipe has died!\n");
      return (FALSE);
      break;

    case 4:
      if (!strcmp (buffer, "NCUR")) {  /* stop reading cursor */
	return (TRUE);
      }
      break;

    default:
      fprintf (stderr, "weird signal: too many or few bytes!  %d\n", status);
      return (TRUE);
      break;
    }

    if (XEventsQueued (graphic.display, QueuedAfterFlush) < 1){
      usleep (10000);
      continue;
    }

    /* check for x events */
    if (XCheckMaskEvent (graphic.display, EVENT_MASK, &event)) {
      
      switch (event.type)  {

      case MotionNotify:
	break;

      case Expose:
	if (HAVE_BACKING) {
	  continue;
	}
	if (XEventsQueued (graphic.display, QueuedAlready) < 2) {
	  Refresh (1);
	}
	break;
	
      case ConfigureNotify:
	status = Reconfig (&event);
	break;
	
      case MappingNotify:
	XRefreshKeyboardMapping ((XMappingEvent *) &event);
	break;
	
      case KeyPress:
	string[0] = 0;
	value = XLookupString ((XKeyEvent *) &event, string, 9, &keysym, &composestatus);
	x = (event.xkey.x - layout[0].axis[0].fx)*(layout[0].axis[0].max - layout[0].axis[0].min)/layout[0].axis[0].dfx + layout[0].axis[0].min;
	y = (event.xkey.y - layout[0].axis[1].fy)*(layout[0].axis[1].max - layout[0].axis[1].min)/layout[0].axis[1].dfy + layout[0].axis[1].min;
	if ((value > 0) && (isdigit(string[0]) || isalpha(string[0]))) {
	  sprintf (line, "%c %12.6f %12.6f \0", string[0], x, y);
	  write (sock, line, 32);
	}
	break;
      }
    }
  }
}
