1 ## Copyright (C) 2004-2012 Petr Mikulik
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} __gnuplot_ginput__ (@var{f}, @var{n})
21 ## Undocumented internal function.
24 ## This is ginput.m implementation for gnuplot and X11.
25 ## It requires gnuplot 4.1 and later.
27 ## This file initially bore the copyright statement
29 ## History: June 2006; August 2005; June 2004; April 2004
30 ## License: public domain
32 function [x, y, button] = __gnuplot_ginput__ (f, n)
34 ostream = get (f, "__plot_stream__");
35 if (numel (ostream) < 1)
36 error ("ginput: stream to gnuplot not open");
38 if (numel (ostream) == 1)
39 error ("ginput: Need mkfifo that is not implemented under Windows");
49 if (compare_versions (__gnuplot_version__ (), "4.0", "<="))
50 error ("ginput: version %s of gnuplot not supported", gnuplot_version ());
56 button = zeros (100, 1);
60 button = zeros (n, 1);
64 gpin_name = tmpnam ();
67 [err, msg] = mkfifo (gpin_name, 6*8*8);
70 error ("ginput: Can not open fifo (%s)", msg);
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.
84 fprintf (ostream, "set print \"%s\";\n", gpin_name);
86 [gpin, err] = fopen (gpin_name, "r");
88 error ("ginput: Can not open fifo (%s)", msg);
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");
93 ## Close output file, to force it to be flushed
94 fputs (ostream, "set print;\n");
97 ## Now read from fifo.
98 [x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C");
101 fprintf (ostream, "set print \"-\";\n");
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");
106 ## Close output file, to force it to be flushed
107 fputs (ostream, "set print;\n");
111 while (isempty (str))
112 str = char (fread (istream)');
116 str = regexp (str, 'OCTAVE:\s+[-+.\d]+\s+[-+.\d]+\s+\d*', 'match');
120 [x(k), y(k), button(k), count] = sscanf (str{end}(8:end), "%f %f %d", "C");
123 if ([x(k), y(k), button(k)] == [0, 0, -1])
124 ## Mousing not active (no plot yet).
129 ## Input argument n was given => stop when k == n.
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)
147 unwind_protect_cleanup