]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/private/__gnuplot_ginput__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / private / __gnuplot_ginput__.m
1 ## Copyright (C) 2004-2012 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
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} __gnuplot_ginput__ (@var{f}, @var{n})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
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
32 function [x, y, button] = __gnuplot_ginput__ (f, n)
33
34   ostream = get (f, "__plot_stream__");
35   if (numel (ostream) < 1)
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
48
49   if (compare_versions (__gnuplot_version__ (), "4.0", "<="))
50     error ("ginput: version %s of gnuplot not supported", gnuplot_version ());
51   endif
52
53   if (nargin == 1)
54     x = zeros (100, 1);
55     y = zeros (100, 1);
56     button = zeros (100, 1);
57   else
58     x = zeros (n, 1);
59     y = zeros (n, 1);
60     button = zeros (n, 1);
61   endif
62
63   if (use_mkfifo)
64     gpin_name = tmpnam ();
65
66     ##Mode: 6*8*8 ==  0600
67     [err, msg] = mkfifo (gpin_name, 6*8*8);
68
69     if (err != 0)
70       error ("ginput: Can not open fifo (%s)", msg);
71     endif
72   endif
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.
83       if (use_mkfifo)
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");
92
93         ## Close output file, to force it to be flushed
94         fputs (ostream, "set print;\n");
95         fflush (ostream);
96
97         ## Now read from fifo.
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             sleep (0.05);
115           else
116             str = regexp (str, 'OCTAVE:\s+[-+.\d]+\s+[-+.\d]+\s+\d*', 'match');
117           endif
118           fclear (istream);
119         endwhile
120         [x(k), y(k), button(k), count] = sscanf (str{end}(8:end), "%f %f %d", "C");
121       endif
122
123       if ([x(k), y(k), button(k)] == [0, 0, -1])
124         ## Mousing not active (no plot yet).
125         break;
126       endif
127
128       if (nargin > 1)
129         ## Input argument n was given => stop when k == n.
130         if (k == n)
131           break;
132         endif
133       else
134         ## Input argument n not given => stop when hitting a return key.
135         ## if (button(k) == 0x0D || button(k) == 0x0A)
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
144       endif
145     endwhile
146
147   unwind_protect_cleanup
148     if (use_mkfifo)
149       unlink (gpin_name);
150     endif
151   end_unwind_protect
152
153 endfunction
154