1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright 2020 Thomas E. Dickey --
11 -- Copyright 2000-2009,2014 Free Software Foundation, Inc. --
13 -- Permission is hereby granted, free of charge, to any person obtaining a --
14 -- copy of this software and associated documentation files (the --
15 -- "Software"), to deal in the Software without restriction, including --
16 -- without limitation the rights to use, copy, modify, merge, publish, --
17 -- distribute, distribute with modifications, sublicense, and/or sell --
18 -- copies of the Software, and to permit persons to whom the Software is --
19 -- furnished to do so, subject to the following conditions: --
21 -- The above copyright notice and this permission notice shall be included --
22 -- in all copies or substantial portions of the Software. --
24 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
25 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
26 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
27 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
28 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
29 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
30 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
32 -- Except as contained in this notice, the name(s) of the above copyright --
33 -- holders shall not be used in advertising or otherwise to promote the --
34 -- sale, use or other dealings in this Software without prior written --
36 ------------------------------------------------------------------------------
37 -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
40 -- $Date: 2020/02/02 23:34:34 $
41 -- Binding Version 01.00
42 ------------------------------------------------------------------------------
43 -- Character input test
44 -- test the keypad feature
46 with ncurses2.util; use ncurses2.util;
48 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
49 with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
50 with Ada.Characters.Handling;
51 with Ada.Strings.Bounded;
53 with ncurses2.genericPuts;
55 procedure ncurses2.getch_test is
58 function mouse_decode (ep : Mouse_Event) return String;
60 function mouse_decode (ep : Mouse_Event) return String is
63 Button : Mouse_Button;
65 package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (200);
67 buf : Bounded_String := To_Bounded_String ("");
69 -- Note that these bindings do not allow
71 -- The C version can print {click-1, click-3} for example.
72 -- They also don't have the 'id' or z coordinate.
73 Get_Event (ep, Y, X, Button, State);
75 -- TODO Append (buf, "id "); from C version
77 Append (buf, Column_Position'Image (X));
79 Append (buf, Line_Position'Image (Y));
80 Append (buf, ") state");
81 Append (buf, Mouse_Button'Image (Button));
84 Append (buf, Button_State'Image (State));
85 return To_String (buf);
88 buf : String (1 .. 1024); -- TODO was BUFSIZE
91 blockflag : Timeout_Mode := Blocking;
92 firsttime : Boolean := True;
94 tmp6 : String (1 .. 6);
95 tmp20 : String (1 .. 20);
99 incount : Integer := 0;
103 tmp2 := Start_Mouse (All_Events);
104 Add (Str => "Delay in 10ths of a second (<CR> for blocking input)? ");
105 Set_Echo_Mode (SwitchOn => True);
108 Set_Echo_Mode (SwitchOn => False);
109 Set_NL_Mode (SwitchOn => False);
111 if Ada.Characters.Handling.Is_Digit (buf (1)) then
112 Get (Item => n, From => buf, Last => tmpx);
113 Set_Timeout_Mode (Mode => Delayed, Amount => n * 100);
114 blockflag := Delayed;
117 c := Character'Pos ('?');
118 Set_Raw_Mode (SwitchOn => True);
120 if not firsttime then
121 Add (Str => "Key pressed: ");
122 Put (tmp6, Integer (c), 8);
125 if c = Key_Mouse then
130 Add (Str => "KEY_MOUSE, ");
131 Add (Str => mouse_decode (event));
134 elsif c >= Key_Min then
137 -- I used tmp and got bitten by the length problem:->
139 elsif c > 16#80# then -- TODO fix, use constant if possible
141 c2 : constant Character := Character'Val (c mod 16#80#);
143 if Ada.Characters.Handling.Is_Graphic (c2) then
148 Add (Str => Un_Control ((Ch => c2,
149 Color => Color_Pair'First,
150 Attr => Normal_Video)));
152 Add (Str => " (high-half character)");
157 c2 : constant Character := Character'Val (c mod 16#80#);
159 if Ada.Characters.Handling.Is_Graphic (c2) then
161 Add (Str => " (ASCII printable character)");
164 Add (Str => Un_Control ((Ch => c2,
165 Color => Color_Pair'First,
166 Attr => Normal_Video)));
167 Add (Str => " (ASCII control character)");
172 -- TODO I am not sure why this was in the C version
173 -- the delay statement scroll anyway.
174 Get_Cursor_Position (Line => y, Column => x);
175 if y >= Lines - 1 then
176 Move_Cursor (Line => 0, Column => 0);
178 Clear_To_End_Of_Line;
182 if c = Character'Pos ('g') then
184 package p is new ncurses2.genericPuts (1024);
187 timedout : Boolean := False;
188 boundedbuf : Bounded_String;
190 Add (Str => "getstr test: ");
191 Set_Echo_Mode (SwitchOn => True);
192 -- Note that if delay mode is set
193 -- Get can raise an exception.
194 -- The C version would print the string it had so far
195 -- also TODO get longer length string, like the C version
197 myGet (Str => boundedbuf);
198 exception when Curses_Exception =>
199 Add (Str => "Timed out.");
203 -- note that the Ada Get will stop reading at 1024.
205 Set_Echo_Mode (SwitchOn => False);
206 Add (Str => " I saw '");
207 myAdd (Str => boundedbuf);
212 elsif c = Character'Pos ('s') then
214 elsif c = Character'Pos ('x') or
215 c = Character'Pos ('q') or
216 (c = Key_None and blockflag = Blocking)
219 elsif c = Character'Pos ('?') then
220 Add (Str => "Type any key to see its keypad value. Also:");
222 Add (Str => "g -- triggers a getstr test");
224 Add (Str => "s -- shell out");
226 Add (Str => "q -- quit");
228 Add (Str => "? -- repeats this help message");
234 exit when c /= Key_None;
235 if blockflag /= Blocking then
236 Put (tmp6, incount); -- argh string length!
238 Add (Str => ": input timed out");
243 Add (Str => ": input error");
247 incount := incount + 1;
252 Set_Timeout_Mode (Mode => Blocking, Amount => 0); -- amount is ignored
253 Set_Raw_Mode (SwitchOn => False);
254 Set_NL_Mode (SwitchOn => True);
257 end ncurses2.getch_test;