Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gpReadMouseTest.c
Go to the documentation of this file.
1 /*
2 
3  gpReadMouseTest.c
4 
5  Test piped communication and mouse readback with gnuplot.
6  This code is published under the GNU Public License.
7 
8  This example demonstrates how to wait for a mouse press and get the
9  mouse button number and mouse position, from a graph in gnuplot.
10 
11 */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 
20 #define GPFIFO "/tmp/gpio" /* any unique name */
21 
22 int main(int argc, char *argv[])
23 {
24  FILE *gp, *gpin;
25  float mx = 0.0, my = 0.0;
26  int mb = 0;
27  int n;
28  char buf[80];
29 
30 /* Create a FIFO we later use for communication gnuplot => our program. */
31  if(mkfifo(GPFIFO, 0600))
32  {
33  if(errno != EEXIST)
34  {
35  perror(GPFIFO);
36  unlink(GPFIFO);
37 
38  return(1);
39  }
40  }
41 
42  if((gp = popen("gnuplot", "w")) == NULL)
43  {
44  perror("gnuplot");
45  pclose(gp);
46 
47  return(1);
48  }
49 
50  puts("Connected to gnuplot.\n");
51 
52 /* Init mouse and redirect all gnuplot printings to our FIFO */
53  fprintf(gp, "set mouse; set print \"%s\"\n", GPFIFO);
54  fflush(gp);
55 
56  fprintf(gp, "set terminal x11\n");
57  fflush(gp);
58 
59 /* Open the FIFO (where gnuplot is writing to) for reading. */
60  if((gpin = fopen(GPFIFO,"r")) == NULL)
61  {
62  perror(GPFIFO);
63  pclose(gp);
64 
65  return(1);
66  }
67 
68  puts("FIFO open for reading.\n");
69 
70 /* Now do the work. Make a sin() plot */
71 
72  fprintf(gp, "plot sin(x)\n");
73  fflush(gp);
74 
75 /* Now wait for mouse clicks; Do it 5 times. */
76  for(n=0;n<5;n++)
77  {
78  printf("%i/5. -- Click anywhere in the graph by mouse button 1.\n", n+1);
79  fprintf(gp, "pause mouse\n");
80  fflush(gp);
81 
82  /* Let gnuplot write to coordinates values (to the FIFO). */
83  fprintf(gp, "print MOUSE_X, MOUSE_Y, MOUSE_BUTTON\n");
84  fflush(gp);
85 
86  /* Read from the FIFO. */
87  fscanf(gpin, "%f %f %d", &mx, &my, &mb);
88  printf("You pressed mouse button: %d @ x=%f y=%f\n", mb, mx, my);
89  }
90 
91  fclose(gpin);
92  pclose(gp);
93  unlink (GPFIFO);
94 
95  return(0);
96 }
int main(void)
Definition: gsl_try_2.c:176
#define GPFIFO
FILE * gpin
Definition: xgraphic.c:118