diff -cr /tmp/gnuplot-3.7.1/Makefile.in ./Makefile.in
*** /tmp/gnuplot-3.7.1/Makefile.in	Wed Oct 27 13:15:10 1999
--- ./Makefile.in	Wed Nov  3 16:52:32 1999
***************
*** 202,208 ****
  COREOBJS = alloc.$(O) binary.$(O) bitmap.$(O) command.$(O) contour.$(O)\
  	datafile.$(O) eval.$(O) fit.$(O) graphics.$(O) graph3d.$(O) help.$(O)\
  	hidden3d.$(O) internal.$(O) interpol.$(O) matrix.$(O) misc.$(O)\
! 	parse.$(O) plot.$(O) plot2d.$(O) plot3d.$(O) readline.$(O)\
  	scanner.$(O) set.$(O) show.$(O) specfun.$(O) standard.$(O) stdfn.$(O)\
  	term.$(O) time.$(O) util.$(O) util3d.$(O)
  
--- 202,208 ----
  COREOBJS = alloc.$(O) binary.$(O) bitmap.$(O) command.$(O) contour.$(O)\
  	datafile.$(O) eval.$(O) fit.$(O) graphics.$(O) graph3d.$(O) help.$(O)\
  	hidden3d.$(O) internal.$(O) interpol.$(O) matrix.$(O) misc.$(O)\
! 	mouse.$(O) parse.$(O) plot.$(O) plot2d.$(O) plot3d.$(O) readline.$(O)\
  	scanner.$(O) set.$(O) show.$(O) specfun.$(O) standard.$(O) stdfn.$(O)\
  	term.$(O) time.$(O) util.$(O) util3d.$(O)
  
***************
*** 212,223 ****
  
  CSOURCE = alloc.c binary.c bitmap.c command.c contour.c datafile.c eval.c \
  	  fit.c graphics.c graph3d.c help.c hidden3d.c internal.c interpol.c \
! 	  matrix.c misc.c parse.c plot.c plot2d.c plot3d.c readline.c \
  	  scanner.c set.c show.c specfun.c standard.c stdfn.c term.c time.c \
  	  util.c util3d.c version.c
  
  HEADERS = alloc.h ansichek.h binary.h bitmap.h fit.h fnproto.h help.h \
! 	  matrix.h national.h plot.h protos.h setshow.h stdfn.h syscfg.h term.h
  
  CORETERM = $(T)aed.trm $(T)ai.trm $(T)amiga.trm $(T)apollo.trm \
  	   $(T)atariaes.trm $(T)atarivdi.trm $(T)be.trm $(T)cgi.trm \
--- 212,223 ----
  
  CSOURCE = alloc.c binary.c bitmap.c command.c contour.c datafile.c eval.c \
  	  fit.c graphics.c graph3d.c help.c hidden3d.c internal.c interpol.c \
! 	  matrix.c misc.c mouse.c parse.c plot.c plot2d.c plot3d.c readline.c \
  	  scanner.c set.c show.c specfun.c standard.c stdfn.c term.c time.c \
  	  util.c util3d.c version.c
  
  HEADERS = alloc.h ansichek.h binary.h bitmap.h fit.h fnproto.h help.h \
! 	  matrix.h mouse.h national.h plot.h protos.h setshow.h stdfn.h syscfg.h term.h
  
  CORETERM = $(T)aed.trm $(T)ai.trm $(T)amiga.trm $(T)apollo.trm \
  	   $(T)atariaes.trm $(T)atarivdi.trm $(T)be.trm $(T)cgi.trm \
diff -cr /tmp/gnuplot-3.7.1/gplt_x11.c ./gplt_x11.c
*** /tmp/gnuplot-3.7.1/gplt_x11.c	Fri Oct 15 18:04:50 1999
--- ./gplt_x11.c	Wed Nov  3 16:52:32 1999
***************
*** 225,230 ****
--- 225,231 ----
  
  
  struct plot_struct plot_array[MAX_WINDOWS];
+ struct plot_struct *current_plot=NULL;
  
  
  Display *dpy;
***************
*** 254,259 ****
--- 255,262 ----
  double xscale, yscale, pointsize;
  #define X(x) (int) ((x) * xscale)
  #define Y(y) (int) ((4095-(y)) * yscale)
+ #define RevX(x) ((x)/xscale)
+ #define RevY(y) (4095-(y)/yscale)
  
  #define Nbuf 1024
  char buf[Nbuf], **commands = (char **) 0;
***************
*** 754,763 ****
--- 757,769 ----
  		FPRINTF((stderr, "plot for window number %d\n", plot_number));
  		plot = plot_array + plot_number;
  		prepare_plot(plot, plot_number);
+ 		current_plot = plot;
  		continue;
  	    }
  	case 'E':		/* leave graphics mode / suspend */
  	    display(plot);
+ 	    printf("E\n");  /* acknowledgement back to gnuplot itself, for synchronization */
+ 	    fflush(stdout);
  	    return 1;
  	case 'R':		/* leave x11 mode */
  	    reset_cursor();
***************
*** 794,799 ****
--- 800,806 ----
  	    FPRINTF((stderr, "plot for window number %d\n", plot_number));
  	    plot = plot_array + plot_number;
  	    prepare_plot(plot, plot_number);
+ 	    current_plot = plot;
  	    break;
  	}
      case 'E':			/* leave graphics mode */
***************
*** 1190,1198 ****
--- 1197,1237 ----
  	handle_selection_event(event);
  	break;
  #endif
+     case ButtonPress:
+ 	{
+ 	    plot_struct *plot = find_plot(event->xbutton.window);
+ 	    if (plot!=current_plot) break;
+ 	    printf("P %f %f %i\n",RevX((float)event->xbutton.x),RevY((float)event->xbutton.y),event->xbutton.button);
+ 	    fflush(stdout);
+ 	    break;
+ 	}
+     case ButtonRelease:
+ 	{
+ 	    plot_struct *plot = find_plot(event->xbutton.window);
+ 	    if (plot!=current_plot) break;
+ 	    printf("R %f %f %i\n",RevX((float)event->xbutton.x),RevY((float)event->xbutton.y),event->xbutton.button);
+ 	    fflush(stdout);
+ 	    break;
+ 	}
+     case MotionNotify:
+ 	{
+ 	    int root_x,root_y,pos_x,pos_y;
+ 	    unsigned int keys_buttons;
+ 	    Window child,root;
+ 	    plot_struct *plot = find_plot(event->xbutton.window);
+ 	    if (plot!=current_plot) break;
+ 	    if (!XQueryPointer(dpy,event->xmotion.window,&root,&child,&root_x,&root_y,&pos_x,&pos_y,&keys_buttons)) break;
+ 	    printf("M %f %f\n",RevX((float)pos_x),RevY((float)pos_y));
+ 	    fflush(stdout);
+ 	    break;
+ 	}
      }
  }
  
+ 
+ 
+ 
+ 
  /*-----------------------------------------------------------------------------
   *   preset - determine options, open display, create window
   *---------------------------------------------------------------------------*/
***************
*** 1716,1722 ****
      }
      XStoreName(dpy, win, ((title) ? title : Class));
  
!     XSelectInput(dpy, win, KeyPressMask | StructureNotifyMask);
      XMapWindow(dpy, win);
  
      return win;
--- 1755,1764 ----
      }
      XStoreName(dpy, win, ((title) ? title : Class));
  
! /* somehow, PointerHintMask doesn't seem to work correctly when plotting a large number of points
!     XSelectInput(dpy, win, KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | PointerMotionHintMask);
! */
!     XSelectInput(dpy, win, KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask );
      XMapWindow(dpy, win);
  
      return win;
diff -cr /tmp/gnuplot-3.7.1/graph3d.c ./graph3d.c
*** /tmp/gnuplot-3.7.1/graph3d.c	Tue Oct 19 15:31:49 1999
--- ./graph3d.c	Wed Nov  3 16:52:33 1999
***************
*** 485,493 ****
  static int key_size_left;	/* distance from x to left edge of box */
  static int key_size_right;	/* distance from x to right edge of box */
  
! void do_3dplot(plots, pcount)
  struct surface_points *plots;
  int pcount;			/* count of plots in linked list */
  {
      struct termentry *t = term;
      int surface;
--- 485,494 ----
  static int key_size_left;	/* distance from x to left edge of box */
  static int key_size_right;	/* distance from x to right edge of box */
  
! void do_3dplot(plots, pcount, quick)
  struct surface_points *plots;
  int pcount;			/* count of plots in linked list */
+ int quick;			/* !=0 means plot only axes etc., for quick rotation */
  {
      struct termentry *t = term;
      int surface;
***************
*** 647,653 ****
      }
  
  #ifndef LITE
!     if (hidden3d && draw_surface) {
  	init_hidden_line_removal();
  	reset_hidden_line_removal();
  	hidden_active = TRUE;
--- 648,654 ----
      }
  
  #ifndef LITE
!     if (hidden3d && draw_surface && !quick) {
  	init_hidden_line_removal();
  	reset_hidden_line_removal();
  	hidden_active = TRUE;
***************
*** 740,746 ****
      /* DRAW SURFACES AND CONTOURS */
  
  #ifndef LITE
!     if (hidden3d && draw_surface)
  	plot3d_hidden(plots, pcount);
  #endif /* not LITE */
  
--- 741,747 ----
      /* DRAW SURFACES AND CONTOURS */
  
  #ifndef LITE
!     if (hidden3d && draw_surface && !quick)
  	plot3d_hidden(plots, pcount);
  #endif /* not LITE */
  
***************
*** 782,787 ****
--- 783,789 ----
     yl -= key_entry_height
  
      this_plot = plots;
+   if (!quick)
      for (surface = 0;
  	 surface < pcount;
  	 this_plot = this_plot->next_sp, surface++) {
diff -cr /tmp/gnuplot-3.7.1/graphics.c ./graphics.c
*** /tmp/gnuplot-3.7.1/graphics.c	Wed Sep 15 17:30:29 1999
--- ./graphics.c	Wed Nov  3 16:52:34 1999
***************
*** 129,134 ****
--- 129,135 ----
  
  static double LogScale __PROTO((double coord, int is_log, double log_base_log,
  				char *what, char *axis));
+ static double RevLogScale __PROTO((double coord, int is_log, double log_base_log));
  static double dbl_raise __PROTO((double x, int y));
  static void boundary __PROTO((int scaling, struct curve_points * plots,
  				int count));
***************
*** 296,301 ****
--- 297,311 ----
  }
  /*}}} */
  
+ static double RevLogScale(coord, is_log, log_base_log)
+ double coord;			/* the value */
+ TBOOLEAN is_log;		/* is this axis in logscale? */
+ double log_base_log;		/* if so, the log of its base */
+ {
+     if (is_log) return exp(coord*log_base_log);
+     else return coord;
+ }
+ 
  /*{{{  graph_error() */
  /* handle errors during graph-plot in a consistent way */
  void graph_error(text)
***************
*** 4518,4520 ****
--- 4528,4554 ----
      }
  }
  /*}}} */
+ 
+ 
+ void print_position(x, y)
+ float x, y;
+ {
+     double xx,yy;
+     if (x<xleft || x>xright || y<ybot || y>ytop) return;
+     xx=(x-xleft)/scale[FIRST_X_AXIS]+min_array[FIRST_X_AXIS];
+     yy=(y-ybot)/scale[FIRST_Y_AXIS]+min_array[FIRST_Y_AXIS];
+     printf("Coordinates: X=%-12g Y=%-12g",
+ 	RevLogScale(xx,log_array[FIRST_X_AXIS],log_base_array[FIRST_X_AXIS]),
+ 	RevLogScale(yy,log_array[FIRST_Y_AXIS],log_base_array[FIRST_Y_AXIS]));
+     if ((x2tics&TICS_MASK) != NO_TICS) {
+ 	xx=(x-xleft)/scale[SECOND_X_AXIS]+min_array[SECOND_X_AXIS];
+ 	printf(" X2=%-12g",
+ 	    RevLogScale(xx,log_array[SECOND_X_AXIS],log_base_array[SECOND_X_AXIS]));
+     }
+     if ((y2tics&TICS_MASK) != NO_TICS) {
+ 	yy=(y-ybot)/scale[SECOND_Y_AXIS]+min_array[SECOND_Y_AXIS];
+ 	printf(" Y2=%-12g",
+ 	    RevLogScale(yy,log_array[SECOND_Y_AXIS],log_base_array[SECOND_Y_AXIS]));
+     }
+     printf("\n");
+ }
diff -cr /tmp/gnuplot-3.7.1/mouse.c ./mouse.c
*** /tmp/gnuplot-3.7.1/mouse.c	Wed Nov  3 17:06:21 1999
--- ./mouse.c	Wed Nov  3 16:52:35 1999
***************
*** 0 ****
--- 1,112 ----
+ /* GNUPLOT - mouse.c */
+ 
+ /*
+ Documentation for the GNUPLOT mouse interface
+ ---------------------------------------------
+ (19991017, Pieter-Tjerk de Boer, ptdeboer@cs.utwente.nl)
+ 
+ Outline:
+ --------
+ A terminal driver can now provide a 'waitforinput' function, that will be
+ called every time GNUPLOT is waiting for input from the keyboard.
+ In this function the terminal driver can check whether there is relevant
+ input from the mouse, and if so, call one of the mouseevent_* functions.
+ Furthermore, the terminal driver needs to be modified to call
+ mouseevent_plotcomplete() upon completion of a drawing.
+ 
+ Description of waitforinput:
+ ------------------------------
+ The TERMENTRY structure contains a pointer by this name, which can either
+ be NULL (default), or point to an appropriate function. In the latter case,
+ this function will be called every time GNUPLOT is waiting for input from
+ the keyboard (stdin) in readline.c. To be precise, it is called just before
+ getc() is called.
+ The function should do the following:
+ - return when input on stdin becomes available (or earlier)
+ - until then, check for input from the mouse and call mouseevent() accordingly
+ 
+ Description of mouseevent_*:
+ ----------------------------
+ - mouseevent_buttonpress():
+     must be called when a button is pressed; arguments are the coordinates
+     of the cursor and a number >=1 indicating which button is pressed.
+ - mouseevent_buttonrelease():
+     like mouseevent_buttonpress(), but called upon release of button.
+ - mouseevent_buttonmotion():
+     must be called when pointer moves while button is pressed ("dragging").
+ - mouseevent_buttonmotion():
+     must be called when drawing is completed. This serves for synchronization
+     on terminals where part of the drawing process is done in an external
+     program, possibly running on a different machine, e.g. the X11 window
+     system.
+ 
+ */
+ 
+ #include "plot.h"
+ #include "setshow.h"
+ #include "mouse.h"
+ 
+ 
+ 
+ static float zero_rot_x,zero_rot_z;
+ static int allowmotion = 1;
+ static int quick = 0;
+ 
+ extern struct surface_points *first_3dplot;		/* from plot3d.c */
+ extern int plot3d_num;		   			/* from plot3d.c */
+ extern TBOOLEAN is_3d_plot;				/* from command.c */
+ extern void do_3dplot(struct surface_points *,int,int);	/* from graph3d.c */
+ extern void print_position(double,double);		/* from graphics.c */
+ 
+ 
+ void mouseevent_buttonpress(x,y,button)
+ float x,y;	/* coordinates of the cursor */
+ int button;	/* number of button (starting from 1) */
+ {
+     if (is_3d_plot) {
+ 	zero_rot_z = surface_rot_z + 360*x/term->xmax;
+ 	zero_rot_x = surface_rot_x - 180*y/term->ymax;
+         quick= (button!=1);
+     } else {
+ 	print_position(x,y);
+     } 
+ }
+ 
+ 
+ void mouseevent_buttonrelease(x,y,button)
+ float x,y;	/* coordinates of the cursor */
+ int button;	/* number of button (starting from 1) */
+ {
+     if (is_3d_plot) {
+ 	quick = 0;
+ 	allowmotion = 1;
+ 	mouseevent_buttonmotion(x,y);
+     }
+ }
+ 
+ 
+ void mouseevent_buttonmotion(x,y)
+ float x,y;	/* coordinates of the cursor */
+ {
+     if (is_3d_plot) {
+ 	if (!allowmotion) return;
+ 	if (plot3d_num == 0) return;
+ 	surface_rot_x = zero_rot_x + 180*y/term->ymax;
+ 	if (surface_rot_x < 0)   surface_rot_x = 0;
+ 	if (surface_rot_x > 180) surface_rot_x = 180;
+ 	surface_rot_z = fmod(zero_rot_z - 360*x/term->xmax,360);
+ 	if (surface_rot_z < 0) surface_rot_z += 360;
+ 	do_3dplot(first_3dplot,plot3d_num,quick);
+ 	allowmotion = 0;  /* disable further motionevents until picture has been replotted */
+     }
+ }
+ 
+ 
+ 
+ void mouseevent_plotcomplete()
+ {
+     if (is_3d_plot) {
+ 	allowmotion = 1;
+     }
+ }
+ 
diff -cr /tmp/gnuplot-3.7.1/mouse.h ./mouse.h
*** /tmp/gnuplot-3.7.1/mouse.h	Wed Nov  3 17:06:22 1999
--- ./mouse.h	Wed Nov  3 16:52:35 1999
***************
*** 0 ****
--- 1,10 ----
+ 
+ /* GNUPLOT - mouse.h */
+ 
+ /* Documentation: see mouse.c */
+ 
+ void mouseevent_buttonpress __PROTO((float x,float y,int button));
+ void mouseevent_buttonrelease __PROTO((float x,float y,int button));
+ void mouseevent_buttonmotion __PROTO((float x,float y));
+ void mouseevent_plotcomplete __PROTO(());
+ 
diff -cr /tmp/gnuplot-3.7.1/plot.h ./plot.h
*** /tmp/gnuplot-3.7.1/plot.h	Tue Oct 19 15:32:17 1999
--- ./plot.h	Wed Nov  3 16:52:35 1999
***************
*** 619,624 ****
--- 619,625 ----
  	void (*resume)  __PROTO((void)); /* called before plots of multiplot */
  	void (*fillbox) __PROTO((int, unsigned int, unsigned int, unsigned int, unsigned int)); /* clear in multiplot mode */
     void (*linewidth) __PROTO((double linewidth));
+ 	void (*waitforinput) __PROTO((void));
  };
  
  #ifdef WIN16
diff -cr /tmp/gnuplot-3.7.1/plot3d.c ./plot3d.c
*** /tmp/gnuplot-3.7.1/plot3d.c	Thu Dec 10 19:30:52 1998
--- ./plot3d.c	Wed Nov  3 23:43:15 1999
***************
*** 58,63 ****
--- 58,65 ----
  struct surface_points *first_3dplot = NULL;
  static struct udft_entry plot_func;
  
+ int plot3d_num=0;
+ 
  
  extern struct udft_entry *dummy_func;
  extern int datatype[];
***************
*** 780,789 ****
--- 782,794 ----
      char *xtitle;
      char *ytitle;
  
+ 
      /* Reset first_3dplot. This is usually done at the end of this function.
       * If there is an error within this function, the memory is left allocated,
       * since we cannot call sp_free if the list is incomplete
       */
+     if (first_3dplot && plot3d_num>0) sp_free(first_3dplot);
+     plot3d_num=0;
      first_3dplot = NULL;
  
      /* put stuff into arrays to simplify access */
***************
*** 1440,1446 ****
  	print_3dtable(plot_num);
      else {
  	START_LEAK_CHECK();	/* assert no memory leaks here ! */
! 	do_3dplot(first_3dplot, plot_num);
  	END_LEAK_CHECK();
      }
  
--- 1445,1451 ----
  	print_3dtable(plot_num);
      else {
  	START_LEAK_CHECK();	/* assert no memory leaks here ! */
! 	do_3dplot(first_3dplot, plot_num, 0);
  	END_LEAK_CHECK();
      }
  
***************
*** 1450,1460 ****
  	m_capture(&replot_line, plot_token, c_token - 1);
  	plot_token = -1;
      }
!     sp_free(first_3dplot);
!     first_3dplot = NULL;
  }
  
  
  
  
  
--- 1455,1473 ----
  	m_capture(&replot_line, plot_token, c_token - 1);
  	plot_token = -1;
      }
! 
!     /* record that all went well */
!     plot3d_num=plot_num;
  }
  
  
+ 
+ void invalidate_plot3d(void)
+ {
+     if (first_3dplot && plot3d_num>0) sp_free(first_3dplot);
+     first_3dplot = NULL;
+     plot3d_num = 0;
+ }
  
  
  
diff -cr /tmp/gnuplot-3.7.1/protos.h ./protos.h
*** /tmp/gnuplot-3.7.1/protos.h	Fri Oct  1 12:37:23 1999
--- ./protos.h	Wed Nov  3 16:52:35 1999
***************
*** 103,109 ****
  
  void map3d_xy __PROTO((double x, double y, double z, unsigned int *xt, unsigned int *yt));
  int map3d_z __PROTO((double x, double y, double z));
! void do_3dplot __PROTO((struct surface_points *plots, int pcount));
  
  
  /* Prototypes from file "help.c" */
--- 103,109 ----
  
  void map3d_xy __PROTO((double x, double y, double z, unsigned int *xt, unsigned int *yt));
  int map3d_z __PROTO((double x, double y, double z));
! void do_3dplot __PROTO((struct surface_points *plots, int pcount, int quick));
  
  
  /* Prototypes from file "help.c" */
diff -cr /tmp/gnuplot-3.7.1/readline.c ./readline.c
*** /tmp/gnuplot-3.7.1/readline.c	Thu Aug 19 16:38:07 1999
--- ./readline.c	Wed Nov  3 16:52:36 1999
***************
*** 379,384 ****
--- 379,385 ----
  
      /* get characters */
      for (;;) {
+ 	if (term->waitforinput) term->waitforinput();
  	cur_char = special_getc();
  
  /*
diff -cr /tmp/gnuplot-3.7.1/set.c ./set.c
*** /tmp/gnuplot-3.7.1/set.c	Thu Aug 19 16:36:35 1999
--- ./set.c	Wed Nov  3 16:52:36 1999
***************
*** 575,580 ****
--- 575,582 ----
      encoding = ENCODING_DEFAULT;
  
      set_locale("C");		/* default */
+ 
+     invalidate_plot3d();
  }
  
  /******** The 'set' command ********/
***************
*** 599,604 ****
--- 601,608 ----
  
      if (!set_one() && !set_two() && !set_three())
  	int_error(setmess, c_token);
+     else
+ 	invalidate_plot3d();
  }
  
  /* return TRUE if a command match, FALSE if not */
diff -cr /tmp/gnuplot-3.7.1/term/README ./term/README
*** /tmp/gnuplot-3.7.1/term/README	Sun Dec  6 22:56:35 1998
--- ./term/README	Wed Nov  3 16:52:36 1999
***************
*** 65,70 ****
--- 65,72 ----
     void (*boxfill) __PROTO((int style, unsigned int x1, unsigned int y1, unsigned int width, unsigned int height)); /* clear part of multiplot */
     void (*linewidth) __PROTO((double linewidth));
     void (*pointsize) __PROTO((double pointsize));
+ 
+ 	void (*waitforinput) __PROTO((void));  /* called when main program is waiting for input; terminal can now handle e.g. mouse; should return as soon as some input is available on stdin */
  };
  
  One consequence of (1) is that we would like drivers to be backwards
diff -cr /tmp/gnuplot-3.7.1/term/x11.trm ./term/x11.trm
*** /tmp/gnuplot-3.7.1/term/x11.trm	Wed Aug 25 18:16:40 1999
--- ./term/x11.trm	Wed Nov  3 17:07:40 1999
***************
*** 59,64 ****
--- 59,65 ----
  TERM_PUBLIC int X11_justify_text __PROTO((enum JUSTIFY mode));
  TERM_PUBLIC void X11_point __PROTO((unsigned int x, unsigned int y, int number));
  TERM_PUBLIC void X11_fillbox __PROTO((int style, unsigned int x, unsigned y, unsigned int width, unsigned int height));
+ TERM_PUBLIC void X11_waitforinput __PROTO((void));
  #define X11_XMAX 4096
  #define X11_YMAX 4096
  
***************
*** 131,136 ****
--- 132,139 ----
  #define X11_nopts (sizeof(X11_opts) / sizeof(X11_opts[0]))
  
  static FILE *X11_ipc;
+ static FILE *X11_ipc_in;
+ static int X11_ipc_fd=-1;
  
  static char **xargv = (char **)NULL;
  static char *optvec[2*X11_nopts+1];
***************
*** 243,252 ****
--- 246,307 ----
      if (X11_ipc) {
  	fputs("R\n", X11_ipc);
  	fclose(X11_ipc);
+  	fclose(X11_ipc_in);
  	/* dont wait(), since they might be -persist */
  	X11_ipc = NULL;
+  	X11_ipc_in = NULL;
+  	X11_ipc_fd = -1;
      }
  }
+  
+  
+  
+ #include "mouse.h"
+ 
+ static void X11_doevent(void)
+ {
+     char s[100];
+     float x,y;
+     int button;
+ 
+     if (fgets(s,100,X11_ipc_in)==NULL) return;
+ 
+     switch (s[0]) {
+ 	case 'E':
+             mouseevent_plotcomplete();
+             break;
+ 	case 'P':
+             if (sscanf(s+1,"%g%g%d",&x,&y,&button)!=3) break;
+             mouseevent_buttonpress(x,y,button);
+             break;
+ 	case 'R':
+             if (sscanf(s+1,"%g%g%d",&x,&y,&button)!=3) break;
+             mouseevent_buttonrelease(x,y,button);
+             break;
+ 	case 'M':
+ 	    if (sscanf(s+1,"%g%g",&x,&y)!=2) break;
+             mouseevent_buttonmotion(x,y);
+             break;
+     }
+ }
+ 
+ 
+ TERM_PUBLIC void 
+ X11_waitforinput()
+ {
+     fd_set fds;
+     if (X11_ipc_fd<0) return;
+     do {
+ 	FD_ZERO(&fds);
+ 	FD_SET(0,&fds);
+ 	FD_SET(X11_ipc_fd,&fds);
+ 	select(X11_ipc_fd+1,&fds,NULL,NULL,NULL);
+ 	if (FD_ISSET(X11_ipc_fd,&fds)) X11_doevent();
+     } while (!FD_ISSET(0,&fds));
+ }
+ 
+ 
+ 
  
  TERM_PUBLIC void
  X11_init()
***************
*** 261,282 ****
  	 */
  	X11_ipc = popen(X11_command, "w");
  #else /* !(OSK || OS/2) */
! 	int fdes[2];
  
! 	if (pipe(fdes)) perror("pipe() failed:");
  
  	if (fork() == 0) {
  	    /* child */
! 	    close(fdes[1]);
! 	    dup2(fdes[0], 0);	/* stdin from pipe */
  	    execvp(X11_command, optvec);
  	    /* if we get here, something went wrong */
  	    perror("exec failed");
  	    exit(EXIT_FAILURE);
  	}
  	/* parent */
! 	close(fdes[0]);		/* read end of pipe */
! 	X11_ipc = fdopen(fdes[1], "w");
  #endif /* !(OSK || OS/2) */
      }
  
--- 316,343 ----
  	 */
  	X11_ipc = popen(X11_command, "w");
  #else /* !(OSK || OS/2) */
! 	int fdes_out[2];
! 	int fdes_in[2];
  
! 	if (pipe(fdes_out) || pipe(fdes_in)) perror("pipe() failed:");
  
  	if (fork() == 0) {
  	    /* child */
!  	    close(fdes_out[1]);
! 	    dup2(fdes_out[0], 0);	/* stdin from pipe */
!  	    close(fdes_in[0]);
!  	    dup2(fdes_in[1], 1);	/* stdout to pipe */
  	    execvp(X11_command, optvec);
  	    /* if we get here, something went wrong */
  	    perror("exec failed");
  	    exit(EXIT_FAILURE);
  	}
  	/* parent */
! 	close(fdes_out[0]);		/* read end of pipe */
! 	close(fdes_in[1]);
! 	X11_ipc = fdopen(fdes_out[1], "w");
! 	X11_ipc_in = fdopen(fdes_in[0], "r");
! 	X11_ipc_fd = fdes_in[0];
  #endif /* !(OSK || OS/2) */
      }
  
***************
*** 641,647 ****
      X11_justify_text, X11_point, do_arrow, set_font_null,
      X11_pointsize, TERM_CAN_MULTIPLOT,
      X11_text /* suspend can use same routine */ , 0 /* resume */ ,
!     X11_fillbox, X11_linewidth
  TERM_TABLE_END(x11_driver)
  
  #undef LAST_TERM
--- 702,709 ----
      X11_justify_text, X11_point, do_arrow, set_font_null,
      X11_pointsize, TERM_CAN_MULTIPLOT,
      X11_text /* suspend can use same routine */ , 0 /* resume */ ,
!     X11_fillbox, X11_linewidth,
!     X11_waitforinput
  TERM_TABLE_END(x11_driver)
  
  #undef LAST_TERM
***************
*** 656,662 ****
      X11_justify_text, X11_point, do_arrow, set_font_null,
      X11_pointsize, TERM_CAN_MULTIPLOT,
      X11_text /* suspend can use same routine */ , 0 /* resume */ ,
!     X11_fillbox, X11_linewidth
  TERM_TABLE_END(X11_driver)
  
  #undef LAST_TERM
--- 718,725 ----
      X11_justify_text, X11_point, do_arrow, set_font_null,
      X11_pointsize, TERM_CAN_MULTIPLOT,
      X11_text /* suspend can use same routine */ , 0 /* resume */ ,
!     X11_fillbox, X11_linewidth,
!     X11_waitforinput
  TERM_TABLE_END(X11_driver)
  
  #undef LAST_TERM
