Mercurial > hg > octave-lyh
annotate scripts/plot/__gnuplot_ginput__.m @ 8874:bd1b1fe9c6e9 ss-3-1-53
bump version info for snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Feb 2009 18:35:47 -0500 |
parents | a0ec02774303 |
children | 941e16338677 |
rev | line source |
---|---|
7673 | 1 ## Copyright (C) 2004, 2006, 2008 Petr Mikulik |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
19 ## -*- texinfo -*- |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} __gnuplot_ginput__ (@var{f}, @var{n}) |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
21 ## Undocumented internal function. |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
22 ## @end deftypefn |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
23 |
7673 | 24 ## This is ginput.m implementation for gnuplot and X11. |
25 ## It requires gnuplot 4.1 and later. | |
26 | |
27 ## This file initially bore the copyright statement | |
28 ## Petr Mikulik | |
29 ## History: June 2006; August 2005; June 2004; April 2004 | |
30 ## License: public domain | |
31 | |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
32 function [x, y, button] = __gnuplot_ginput__ (f, n) |
7673 | 33 |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
34 ostream = get (f, "__plot_stream__"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
35 if (numel (ostream) < 1) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
36 error ("ginput: stream to gnuplot not open"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
37 elseif (ispc ()) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
38 if (numel (ostream) == 1) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
39 error ("ginput: Need mkfifo that is not implemented under Windows"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
40 endif |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
41 use_mkfifo = false; |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
42 istream = ostream(2); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
43 ostream = ostream(1); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
44 else |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
45 use_mkfifo = true; |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
46 ostream = ostream(1); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
47 endif |
7673 | 48 |
49 if (compare_versions (__gnuplot_version__ (), "4.0", "<=")) | |
50 error ("ginput: version %s of gnuplot not supported", gnuplot_version ()); | |
51 endif | |
52 | |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
53 if (nargin == 1) |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
54 x = zeros (100, 1); |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
55 y = zeros (100, 1); |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
56 button = zeros (100, 1); |
7673 | 57 else |
58 x = zeros (n, 1); | |
59 y = zeros (n, 1); | |
60 button = zeros (n, 1); | |
61 endif | |
62 | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
63 if (use_mkfifo) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
64 gpin_name = tmpnam (); |
7673 | 65 |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
66 ##Mode: 6*8*8 == 0600 |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
67 [err, msg] = mkfifo (gpin_name, 6*8*8); |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
68 |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
69 if (err != 0) |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
70 error ("ginput: Can not open fifo (%s)", msg); |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
71 endif |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
72 endif |
7673 | 73 |
74 unwind_protect | |
75 | |
76 k = 0; | |
77 while (true) | |
78 k++; | |
79 | |
80 ## Notes: MOUSE_* can be undefined if user closes gnuplot by "q" | |
81 ## or Alt-F4. Further, this abrupt close also requires the leading | |
82 ## "\n" on the next line. | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
83 if (use_mkfifo) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
84 fprintf (ostream, "set print \"%s\";\n", gpin_name); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
85 fflush (ostream); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
86 [gpin, err] = fopen (gpin_name, "r"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
87 if (err != 0) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
88 error ("ginput: Can not open fifo (%s)", msg); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
89 endif |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
90 fputs (ostream, "pause mouse any;\n\n"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
91 fputs (ostream, "\nif (exists(\"MOUSE_KEY\") && exists(\"MOUSE_X\")) print MOUSE_X, MOUSE_Y, MOUSE_KEY; else print \"0 0 -1\"\n"); |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
92 |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
93 ## Close output file, to force it to be flushed |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
94 fputs (ostream, "set print;\n"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
95 fflush (ostream); |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
96 |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
97 ## Now read from fifo. |
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
98 [x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C"); |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
99 fclose (gpin); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
100 else |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
101 fprintf (ostream, "set print \"-\";\n"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
102 fflush (ostream); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
103 fputs (ostream, "pause mouse any;\n\n"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
104 fputs (ostream, "\nif (exists(\"MOUSE_KEY\") && exists(\"MOUSE_X\")) print \"OCTAVE: \", MOUSE_X, MOUSE_Y, MOUSE_KEY; else print \"0 0 -1\"\n"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
105 |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
106 ## Close output file, to force it to be flushed |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
107 fputs (ostream, "set print;\n"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
108 fflush (ostream); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
109 |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
110 str = {}; |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
111 while (isempty (str)) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
112 str = char (fread (istream)'); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
113 if (! isempty (str)) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
114 str = regexp (str, 'OCTAVE:\s+[\d.\+-]+\s+[\d.\+-]+\s+\d*', 'match'); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
115 endif |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
116 fclear (istream); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
117 endwhile |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
118 [x(k), y(k), button(k), count] = sscanf (str{end}(8:end), "%f %f %d", "C"); |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
119 endif |
7673 | 120 |
121 if ([x(k), y(k), button(k)] == [0, 0, -1]) | |
122 ## Mousing not active (no plot yet). | |
123 break; | |
124 endif | |
125 | |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
126 if (nargin > 1) |
7673 | 127 ## Input argument n was given => stop when k == n. |
128 if (k == n) | |
129 break; | |
130 endif | |
131 else | |
132 ## Input argument n not given => stop when hitting a return key. | |
133 ## if (button(k) == 0x0D || button(k) == 0x0A) | |
134 ## ## hit Return or Enter | |
135 if (button(k) == 0x0D) | |
136 ## hit Return | |
137 x(k:end) = []; | |
138 y(k:end) = []; | |
139 button(k:end) = []; | |
140 break; | |
141 endif | |
142 endif | |
143 endwhile | |
144 | |
145 unwind_protect_cleanup | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
146 if (use_mkfifo) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
147 unlink (gpin_name); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
148 endif |
7673 | 149 end_unwind_protect |
150 | |
151 endfunction | |
152 |