Mercurial > hg > octave-lyh
comparison scripts/plot/__gnuplot_ginput__.m @ 7680:a0ec02774303
Use popen2 for communication with gnuplot
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 02 Apr 2008 13:29:19 -0400 |
parents | 52d8d50e74c1 |
children | 941e16338677 |
comparison
equal
deleted
inserted
replaced
7679:3c16e2414564 | 7680:a0ec02774303 |
---|---|
29 ## History: June 2006; August 2005; June 2004; April 2004 | 29 ## History: June 2006; August 2005; June 2004; April 2004 |
30 ## License: public domain | 30 ## License: public domain |
31 | 31 |
32 function [x, y, button] = __gnuplot_ginput__ (f, n) | 32 function [x, y, button] = __gnuplot_ginput__ (f, n) |
33 | 33 |
34 persistent have_mkfifo = ! ispc (); | 34 ostream = get (f, "__plot_stream__"); |
35 | 35 if (numel (ostream) < 1) |
36 stream = get (f, "__plot_stream__"); | 36 error ("ginput: stream to gnuplot not open"); |
37 elseif (ispc ()) | |
38 if (numel (ostream) == 1) | |
39 error ("ginput: Need mkfifo that is not implemented under Windows"); | |
40 endif | |
41 use_mkfifo = false; | |
42 istream = ostream(2); | |
43 ostream = ostream(1); | |
44 else | |
45 use_mkfifo = true; | |
46 ostream = ostream(1); | |
47 endif | |
37 | 48 |
38 if (compare_versions (__gnuplot_version__ (), "4.0", "<=")) | 49 if (compare_versions (__gnuplot_version__ (), "4.0", "<=")) |
39 error ("ginput: version %s of gnuplot not supported", gnuplot_version ()); | 50 error ("ginput: version %s of gnuplot not supported", gnuplot_version ()); |
40 endif | 51 endif |
41 | 52 |
47 x = zeros (n, 1); | 58 x = zeros (n, 1); |
48 y = zeros (n, 1); | 59 y = zeros (n, 1); |
49 button = zeros (n, 1); | 60 button = zeros (n, 1); |
50 endif | 61 endif |
51 | 62 |
52 gpin_name = tmpnam (); | 63 if (use_mkfifo) |
64 gpin_name = tmpnam (); | |
53 | 65 |
54 if (have_mkfifo) | 66 ##Mode: 6*8*8 == 0600 |
55 ## Use pipes if not on Windows. Mode: 6*8*8 == 0600 | 67 [err, msg] = mkfifo (gpin_name, 6*8*8); |
56 [err, msg] = mkfifo(gpin_name, 6*8*8); | |
57 | 68 |
58 if (err != 0) | 69 if (err != 0) |
59 error ("ginput: Can not open fifo (%s)", msg); | 70 error ("ginput: Can not open fifo (%s)", msg); |
60 endif | 71 endif |
61 endif | 72 endif |
63 unwind_protect | 74 unwind_protect |
64 | 75 |
65 k = 0; | 76 k = 0; |
66 while (true) | 77 while (true) |
67 k++; | 78 k++; |
68 fprintf (stream, "set print \"%s\";\n", gpin_name); | 79 |
69 fflush (stream); | 80 ## Notes: MOUSE_* can be undefined if user closes gnuplot by "q" |
70 if (have_mkfifo) | 81 ## or Alt-F4. Further, this abrupt close also requires the leading |
82 ## "\n" on the next line. | |
83 if (use_mkfifo) | |
84 fprintf (ostream, "set print \"%s\";\n", gpin_name); | |
85 fflush (ostream); | |
71 [gpin, err] = fopen (gpin_name, "r"); | 86 [gpin, err] = fopen (gpin_name, "r"); |
72 if (err != 0) | 87 if (err != 0) |
73 error ("ginput: Can not open fifo (%s)", msg); | 88 error ("ginput: Can not open fifo (%s)", msg); |
74 endif | 89 endif |
75 endif | 90 fputs (ostream, "pause mouse any;\n\n"); |
76 fputs (stream, "pause mouse any;\n\n"); | 91 fputs (ostream, "\nif (exists(\"MOUSE_KEY\") && exists(\"MOUSE_X\")) print MOUSE_X, MOUSE_Y, MOUSE_KEY; else print \"0 0 -1\"\n"); |
77 | 92 |
78 ## Notes: MOUSE_* can be undefined if user closes gnuplot by "q" | 93 ## Close output file, to force it to be flushed |
79 ## or Alt-F4. Further, this abrupt close also requires the leading | 94 fputs (ostream, "set print;\n"); |
80 ## "\n" on the next line. | 95 fflush (ostream); |
81 fputs (stream, "\nif (exists(\"MOUSE_KEY\") && exists(\"MOUSE_X\")) print MOUSE_X, MOUSE_Y, MOUSE_KEY; else print \"0 0 -1\"\n"); | |
82 | 96 |
83 ## Close output file, otherwise all blocks (why?!). | |
84 fputs (stream, "set print;\n"); | |
85 fflush (stream); | |
86 | |
87 if (! have_mkfifo) | |
88 while (exist (gpin_name, "file") == 0) | |
89 endwhile | |
90 [gpin, msg] = fopen (gpin_name, "r"); | |
91 | |
92 if (gpin < 0) | |
93 error ("ginput: Can not open file (%s)", msg); | |
94 endif | |
95 | |
96 ## Now read from file | |
97 count = 0; | |
98 while (count == 0) | |
99 [xk, yk, buttonk, count] = fscanf (gpin, "%f %f %d", "C"); | |
100 endwhile | |
101 x(k) = xk; | |
102 y(k) = yk; | |
103 button (k) = buttonk; | |
104 else | |
105 ## Now read from fifo. | 97 ## Now read from fifo. |
106 [x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C"); | 98 [x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C"); |
99 fclose (gpin); | |
100 else | |
101 fprintf (ostream, "set print \"-\";\n"); | |
102 fflush (ostream); | |
103 fputs (ostream, "pause mouse any;\n\n"); | |
104 fputs (ostream, "\nif (exists(\"MOUSE_KEY\") && exists(\"MOUSE_X\")) print \"OCTAVE: \", MOUSE_X, MOUSE_Y, MOUSE_KEY; else print \"0 0 -1\"\n"); | |
105 | |
106 ## Close output file, to force it to be flushed | |
107 fputs (ostream, "set print;\n"); | |
108 fflush (ostream); | |
109 | |
110 str = {}; | |
111 while (isempty (str)) | |
112 str = char (fread (istream)'); | |
113 if (! isempty (str)) | |
114 str = regexp (str, 'OCTAVE:\s+[\d.\+-]+\s+[\d.\+-]+\s+\d*', 'match'); | |
115 endif | |
116 fclear (istream); | |
117 endwhile | |
118 [x(k), y(k), button(k), count] = sscanf (str{end}(8:end), "%f %f %d", "C"); | |
107 endif | 119 endif |
108 fclose (gpin); | |
109 | 120 |
110 if ([x(k), y(k), button(k)] == [0, 0, -1]) | 121 if ([x(k), y(k), button(k)] == [0, 0, -1]) |
111 ## Mousing not active (no plot yet). | 122 ## Mousing not active (no plot yet). |
112 break; | 123 break; |
113 endif | 124 endif |
130 endif | 141 endif |
131 endif | 142 endif |
132 endwhile | 143 endwhile |
133 | 144 |
134 unwind_protect_cleanup | 145 unwind_protect_cleanup |
135 unlink (gpin_name); | 146 if (use_mkfifo) |
147 unlink (gpin_name); | |
148 endif | |
136 end_unwind_protect | 149 end_unwind_protect |
137 | 150 |
138 endfunction | 151 endfunction |
139 | 152 |