ncurses2-getch_test.adb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- ncurses --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 2000-2006,2008 Free Software Foundation, Inc. --
  11. -- --
  12. -- Permission is hereby granted, free of charge, to any person obtaining a --
  13. -- copy of this software and associated documentation files (the --
  14. -- "Software"), to deal in the Software without restriction, including --
  15. -- without limitation the rights to use, copy, modify, merge, publish, --
  16. -- distribute, distribute with modifications, sublicense, and/or sell --
  17. -- copies of the Software, and to permit persons to whom the Software is --
  18. -- furnished to do so, subject to the following conditions: --
  19. -- --
  20. -- The above copyright notice and this permission notice shall be included --
  21. -- in all copies or substantial portions of the Software. --
  22. -- --
  23. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
  24. -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
  25. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
  26. -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
  27. -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
  28. -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
  29. -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
  30. -- --
  31. -- Except as contained in this notice, the name(s) of the above copyright --
  32. -- holders shall not be used in advertising or otherwise to promote the --
  33. -- sale, use or other dealings in this Software without prior written --
  34. -- authorization. --
  35. ------------------------------------------------------------------------------
  36. -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
  37. -- Version Control
  38. -- $Revision: 1.7 $
  39. -- $Date: 2008/07/26 18:46:58 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. -- Character input test
  43. -- test the keypad feature
  44. with ncurses2.util; use ncurses2.util;
  45. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  46. with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
  47. with Ada.Characters.Handling;
  48. with Ada.Strings.Bounded;
  49. with ncurses2.genericPuts;
  50. procedure ncurses2.getch_test is
  51. use Int_IO;
  52. function mouse_decode (ep : Mouse_Event) return String;
  53. function mouse_decode (ep : Mouse_Event) return String is
  54. Y : Line_Position;
  55. X : Column_Position;
  56. Button : Mouse_Button;
  57. State : Button_State;
  58. package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (200);
  59. use BS;
  60. buf : Bounded_String := To_Bounded_String ("");
  61. begin
  62. -- Note that these bindings do not allow
  63. -- two button states,
  64. -- The C version can print {click-1, click-3} for example.
  65. -- They also don't have the 'id' or z coordinate.
  66. Get_Event (ep, Y, X, Button, State);
  67. -- TODO Append (buf, "id "); from C version
  68. Append (buf, "at (");
  69. Append (buf, Column_Position'Image (X));
  70. Append (buf, ", ");
  71. Append (buf, Line_Position'Image (Y));
  72. Append (buf, ") state");
  73. Append (buf, Mouse_Button'Image (Button));
  74. Append (buf, " = ");
  75. Append (buf, Button_State'Image (State));
  76. return To_String (buf);
  77. end mouse_decode;
  78. buf : String (1 .. 1024); -- TODO was BUFSIZE
  79. n : Integer;
  80. c : Key_Code;
  81. blockflag : Timeout_Mode := Blocking;
  82. firsttime : Boolean := True;
  83. tmp2 : Event_Mask;
  84. tmp6 : String (1 .. 6);
  85. tmp20 : String (1 .. 20);
  86. x : Column_Position;
  87. y : Line_Position;
  88. tmpx : Integer;
  89. incount : Integer := 0;
  90. begin
  91. Refresh;
  92. tmp2 := Start_Mouse (All_Events);
  93. Add (Str => "Delay in 10ths of a second (<CR> for blocking input)? ");
  94. Set_Echo_Mode (SwitchOn => True);
  95. Get (Str => buf);
  96. Set_Echo_Mode (SwitchOn => False);
  97. Set_NL_Mode (SwitchOn => False);
  98. if Ada.Characters.Handling.Is_Digit (buf (1)) then
  99. Get (Item => n, From => buf, Last => tmpx);
  100. Set_Timeout_Mode (Mode => Delayed, Amount => n * 100);
  101. blockflag := Delayed;
  102. end if;
  103. c := Character'Pos ('?');
  104. Set_Raw_Mode (SwitchOn => True);
  105. loop
  106. if not firsttime then
  107. Add (Str => "Key pressed: ");
  108. Put (tmp6, Integer (c), 8);
  109. Add (Str => tmp6);
  110. Add (Ch => ' ');
  111. if c = Key_Mouse then
  112. declare
  113. event : Mouse_Event;
  114. begin
  115. event := Get_Mouse;
  116. Add (Str => "KEY_MOUSE, ");
  117. Add (Str => mouse_decode (event));
  118. Add (Ch => newl);
  119. end;
  120. elsif c >= Key_Min then
  121. Key_Name (c, tmp20);
  122. Add (Str => tmp20);
  123. -- I used tmp and got bitten by the length problem:->
  124. Add (Ch => newl);
  125. elsif c > 16#80# then -- TODO fix, use constant if possible
  126. declare
  127. c2 : constant Character := Character'Val (c mod 16#80#);
  128. begin
  129. if Ada.Characters.Handling.Is_Graphic (c2) then
  130. Add (Str => "M-");
  131. Add (Ch => c2);
  132. else
  133. Add (Str => "M-");
  134. Add (Str => Un_Control ((Ch => c2,
  135. Color => Color_Pair'First,
  136. Attr => Normal_Video)));
  137. end if;
  138. Add (Str => " (high-half character)");
  139. Add (Ch => newl);
  140. end;
  141. else
  142. declare
  143. c2 : constant Character := Character'Val (c mod 16#80#);
  144. begin
  145. if Ada.Characters.Handling.Is_Graphic (c2) then
  146. Add (Ch => c2);
  147. Add (Str => " (ASCII printable character)");
  148. Add (Ch => newl);
  149. else
  150. Add (Str => Un_Control ((Ch => c2,
  151. Color => Color_Pair'First,
  152. Attr => Normal_Video)));
  153. Add (Str => " (ASCII control character)");
  154. Add (Ch => newl);
  155. end if;
  156. end;
  157. end if;
  158. -- TODO I am not sure why this was in the C version
  159. -- the delay statement scroll anyway.
  160. Get_Cursor_Position (Line => y, Column => x);
  161. if y >= Lines - 1 then
  162. Move_Cursor (Line => 0, Column => 0);
  163. end if;
  164. Clear_To_End_Of_Line;
  165. end if;
  166. firsttime := False;
  167. if c = Character'Pos ('g') then
  168. declare
  169. package p is new ncurses2.genericPuts (1024);
  170. use p;
  171. use p.BS;
  172. timedout : Boolean := False;
  173. boundedbuf : Bounded_String;
  174. begin
  175. Add (Str => "getstr test: ");
  176. Set_Echo_Mode (SwitchOn => True);
  177. -- Note that if delay mode is set
  178. -- Get can raise an exception.
  179. -- The C version would print the string it had so far
  180. -- also TODO get longer length string, like the C version
  181. declare begin
  182. myGet (Str => boundedbuf);
  183. exception when Curses_Exception =>
  184. Add (Str => "Timed out.");
  185. Add (Ch => newl);
  186. timedout := True;
  187. end;
  188. -- note that the Ada Get will stop reading at 1024.
  189. if not timedout then
  190. Set_Echo_Mode (SwitchOn => False);
  191. Add (Str => " I saw '");
  192. myAdd (Str => boundedbuf);
  193. Add (Str => "'.");
  194. Add (ch => newl);
  195. end if;
  196. end;
  197. elsif c = Character'Pos ('s') then
  198. ShellOut (True);
  199. elsif c = Character'Pos ('x') or c = Character'Pos ('q') or
  200. (c = Key_None and blockflag = Blocking) then
  201. exit;
  202. elsif c = Character'Pos ('?') then
  203. Add (Str => "Type any key to see its keypad value. Also:");
  204. Add (Ch => newl);
  205. Add (Str => "g -- triggers a getstr test");
  206. Add (Ch => newl);
  207. Add (Str => "s -- shell out");
  208. Add (Ch => newl);
  209. Add (Str => "q -- quit");
  210. Add (Ch => newl);
  211. Add (Str => "? -- repeats this help message");
  212. Add (Ch => newl);
  213. end if;
  214. loop
  215. c := Getchar;
  216. exit when c /= Key_None;
  217. if blockflag /= Blocking then
  218. Put (tmp6, incount); -- argh string length!
  219. Add (Str => tmp6);
  220. Add (Str => ": input timed out");
  221. Add (Ch => newl);
  222. else
  223. Put (tmp6, incount);
  224. Add (Str => tmp6);
  225. Add (Str => ": input error");
  226. Add (Ch => newl);
  227. exit;
  228. end if;
  229. incount := incount + 1;
  230. end loop;
  231. end loop;
  232. End_Mouse (tmp2);
  233. Set_Timeout_Mode (Mode => Blocking, Amount => 0); -- amount is ignored
  234. Set_Raw_Mode (SwitchOn => False);
  235. Set_NL_Mode (SwitchOn => True);
  236. Erase;
  237. End_Windows;
  238. end ncurses2.getch_test;