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 |
|
19 ## This is ginput.m implementation for gnuplot and X11. |
|
20 ## It requires gnuplot 4.1 and later. |
|
21 |
|
22 ## This file initially bore the copyright statement |
|
23 ## Petr Mikulik |
|
24 ## History: June 2006; August 2005; June 2004; April 2004 |
|
25 ## License: public domain |
|
26 |
|
27 ## -*- texinfo -*- |
|
28 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{buttons}] =} __gnuplot_ginput__ (@var{f}, @var{n}) |
|
29 ## Undocumented internal function. |
|
30 ## @end deftypefn |
|
31 |
|
32 function [x, y, button] = __gnuplot_ginput__ (f, n) |
|
33 |
|
34 stream = get (f, "__plot_stream__"); |
|
35 |
|
36 if (compare_versions (__gnuplot_version__ (), "4.0", "<=")) |
|
37 error ("ginput: version %s of gnuplot not supported", gnuplot_version ()); |
|
38 endif |
|
39 |
|
40 if (nargin == 0) |
|
41 x = zeros (n, 1); |
|
42 y = zeros (n, 1); |
|
43 button = zeros (n, 1); |
|
44 else |
|
45 x = zeros (n, 1); |
|
46 y = zeros (n, 1); |
|
47 button = zeros (n, 1); |
|
48 endif |
|
49 |
|
50 gpin_name = tmpnam (); |
|
51 |
|
52 ## 6*8*8 == 0600 |
|
53 [err, msg] = mkfifo (gpin_name, 6*8*8); |
|
54 if (err != 0) |
|
55 error ("ginput: Can not open fifo (%s)", msg); |
|
56 endif |
|
57 |
|
58 unwind_protect |
|
59 |
|
60 k = 0; |
|
61 while (true) |
|
62 k++; |
|
63 fprintf (stream, "set print \"%s\";\n", gpin_name); |
|
64 fflush (stream); |
|
65 gpin = fopen (gpin_name, "r"); |
|
66 fputs (stream, "pause mouse any;\n\n"); |
|
67 |
|
68 ## Notes: MOUSE_* can be undefined if user closes gnuplot by "q" |
|
69 ## or Alt-F4. Further, this abrupt close also requires the leading |
|
70 ## "\n" on the next line. |
|
71 fputs (stream, "\nif (exists(\"MOUSE_KEY\") && exists(\"MOUSE_X\")) print MOUSE_X, MOUSE_Y, MOUSE_KEY; else print \"0 0 -1\"\n"); |
|
72 |
|
73 ## Close output file, otherwise all blocks (why?!). |
|
74 fputs (stream, "set print;\n"); |
|
75 fflush (stream); |
|
76 |
|
77 ## Now read from fifo. |
|
78 [x(k), y(k), button(k), count] = fscanf (gpin, "%f %f %d", "C"); |
|
79 fclose (gpin); |
|
80 |
|
81 if ([x(k), y(k), button(k)] == [0, 0, -1]) |
|
82 ## Mousing not active (no plot yet). |
|
83 break; |
|
84 endif |
|
85 |
|
86 if (nargin > 0) |
|
87 ## Input argument n was given => stop when k == n. |
|
88 if (k == n) |
|
89 break; |
|
90 endif |
|
91 else |
|
92 ## Input argument n not given => stop when hitting a return key. |
|
93 ## if (button(k) == 0x0D || button(k) == 0x0A) |
|
94 ## ## hit Return or Enter |
|
95 if (button(k) == 0x0D) |
|
96 ## hit Return |
|
97 x(k:end) = []; |
|
98 y(k:end) = []; |
|
99 button(k:end) = []; |
|
100 break; |
|
101 endif |
|
102 endif |
|
103 endwhile |
|
104 |
|
105 unwind_protect_cleanup |
|
106 unlink (gpin_name); |
|
107 end_unwind_protect |
|
108 |
|
109 endfunction |
|
110 |