Mercurial > hg > octave-lyh
annotate scripts/plot/private/__gnuplot_ginput__.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 6f91ca83d2be |
children | 3cce6b4e0f7c |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12793
diff
changeset
|
1 ## Copyright (C) 2004-2012 Petr Mikulik |
7673 | 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) |
10549 | 84 fprintf (ostream, "set print \"%s\";\n", gpin_name); |
85 fflush (ostream); | |
86 [gpin, err] = fopen (gpin_name, "r"); | |
87 if (err != 0) | |
88 error ("ginput: Can not open fifo (%s)", msg); | |
89 endif | |
90 fputs (ostream, "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"); | |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
92 |
10549 | 93 ## Close output file, to force it to be flushed |
94 fputs (ostream, "set print;\n"); | |
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 |
10549 | 97 ## Now read from fifo. |
98 [x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C"); | |
99 fclose (gpin); | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
100 else |
10549 | 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"); | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
105 |
10549 | 106 ## Close output file, to force it to be flushed |
107 fputs (ostream, "set print;\n"); | |
108 fflush (ostream); | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
109 |
10549 | 110 str = {}; |
111 while (isempty (str)) | |
112 str = char (fread (istream)'); | |
113 if (isempty (str)) | |
114 sleep (0.05); | |
115 else | |
12462
e4dbfe3019b1
Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
116 str = regexp (str, 'OCTAVE:\s+[-+.\d]+\s+[-+.\d]+\s+\d*', 'match'); |
10549 | 117 endif |
118 fclear (istream); | |
119 endwhile | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
120 [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
|
121 endif |
7673 | 122 |
123 if ([x(k), y(k), button(k)] == [0, 0, -1]) | |
10549 | 124 ## Mousing not active (no plot yet). |
125 break; | |
7673 | 126 endif |
127 | |
7674
52d8d50e74c1
Add gtext, waitforbuttonpress. Attempt to get ginput working under windows
David Bateman <dbateman@free.fr>
parents:
7673
diff
changeset
|
128 if (nargin > 1) |
10549 | 129 ## Input argument n was given => stop when k == n. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
130 if (k == n) |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
131 break; |
10549 | 132 endif |
7673 | 133 else |
10549 | 134 ## Input argument n not given => stop when hitting a return key. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
135 ## if (button(k) == 0x0D || button(k) == 0x0A) |
10549 | 136 ## ## hit Return or Enter |
137 if (button(k) == 0x0D) | |
138 ## hit Return | |
139 x(k:end) = []; | |
140 y(k:end) = []; | |
141 button(k:end) = []; | |
142 break; | |
143 endif | |
7673 | 144 endif |
145 endwhile | |
146 | |
147 unwind_protect_cleanup | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
148 if (use_mkfifo) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
149 unlink (gpin_name); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7674
diff
changeset
|
150 endif |
7673 | 151 end_unwind_protect |
152 | |
153 endfunction | |
154 |