ncurses2-m.adb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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.8 $
  39. -- $Date: 2008/07/26 18:47:50 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. -- TODO use Default_Character where appropriate
  43. -- This is an Ada version of ncurses
  44. -- I translated this because it tests the most features.
  45. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  46. with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
  47. with Ada.Text_IO; use Ada.Text_IO;
  48. with Ada.Characters.Latin_1;
  49. -- with Ada.Characters.Handling;
  50. with Ada.Command_Line; use Ada.Command_Line;
  51. with Ada.Strings.Unbounded;
  52. with ncurses2.util; use ncurses2.util;
  53. with ncurses2.getch_test;
  54. with ncurses2.attr_test;
  55. with ncurses2.color_test;
  56. with ncurses2.demo_panels;
  57. with ncurses2.color_edit;
  58. with ncurses2.slk_test;
  59. with ncurses2.acs_display;
  60. with ncurses2.acs_and_scroll;
  61. with ncurses2.flushinp_test;
  62. with ncurses2.test_sgr_attributes;
  63. with ncurses2.menu_test;
  64. with ncurses2.demo_pad;
  65. with ncurses2.demo_forms;
  66. with ncurses2.overlap_test;
  67. with ncurses2.trace_set;
  68. with ncurses2.getopt; use ncurses2.getopt;
  69. package body ncurses2.m is
  70. use Int_IO;
  71. function To_trace (n : Integer) return Trace_Attribute_Set;
  72. procedure usage;
  73. procedure Set_Terminal_Modes;
  74. function Do_Single_Test (c : Character) return Boolean;
  75. function To_trace (n : Integer) return Trace_Attribute_Set is
  76. a : Trace_Attribute_Set := (others => False);
  77. m : Integer;
  78. rest : Integer;
  79. begin
  80. m := n mod 2;
  81. if 1 = m then
  82. a.Times := True;
  83. end if;
  84. rest := n / 2;
  85. m := rest mod 2;
  86. if 1 = m then
  87. a.Tputs := True;
  88. end if;
  89. rest := rest / 2;
  90. m := rest mod 2;
  91. if 1 = m then
  92. a.Update := True;
  93. end if;
  94. rest := rest / 2;
  95. m := rest mod 2;
  96. if 1 = m then
  97. a.Cursor_Move := True;
  98. end if;
  99. rest := rest / 2;
  100. m := rest mod 2;
  101. if 1 = m then
  102. a.Character_Output := True;
  103. end if;
  104. rest := rest / 2;
  105. m := rest mod 2;
  106. if 1 = m then
  107. a.Calls := True;
  108. end if;
  109. rest := rest / 2;
  110. m := rest mod 2;
  111. if 1 = m then
  112. a.Virtual_Puts := True;
  113. end if;
  114. rest := rest / 2;
  115. m := rest mod 2;
  116. if 1 = m then
  117. a.Input_Events := True;
  118. end if;
  119. rest := rest / 2;
  120. m := rest mod 2;
  121. if 1 = m then
  122. a.TTY_State := True;
  123. end if;
  124. rest := rest / 2;
  125. m := rest mod 2;
  126. if 1 = m then
  127. a.Internal_Calls := True;
  128. end if;
  129. rest := rest / 2;
  130. m := rest mod 2;
  131. if 1 = m then
  132. a.Character_Calls := True;
  133. end if;
  134. rest := rest / 2;
  135. m := rest mod 2;
  136. if 1 = m then
  137. a.Termcap_TermInfo := True;
  138. end if;
  139. return a;
  140. end To_trace;
  141. -- these are type Stdscr_Init_Proc;
  142. function rip_footer (
  143. Win : Window;
  144. Columns : Column_Count) return Integer;
  145. pragma Convention (C, rip_footer);
  146. function rip_footer (
  147. Win : Window;
  148. Columns : Column_Count) return Integer is
  149. begin
  150. Set_Background (Win, (Ch => ' ',
  151. Attr => (Reverse_Video => True, others => False),
  152. Color => 0));
  153. Erase (Win);
  154. Move_Cursor (Win, 0, 0);
  155. Add (Win, "footer:" & Columns'Img & " columns");
  156. Refresh_Without_Update (Win);
  157. return 0; -- Curses_OK;
  158. end rip_footer;
  159. function rip_header (
  160. Win : Window;
  161. Columns : Column_Count) return Integer;
  162. pragma Convention (C, rip_header);
  163. function rip_header (
  164. Win : Window;
  165. Columns : Column_Count) return Integer is
  166. begin
  167. Set_Background (Win, (Ch => ' ',
  168. Attr => (Reverse_Video => True, others => False),
  169. Color => 0));
  170. Erase (Win);
  171. Move_Cursor (Win, 0, 0);
  172. Add (Win, "header:" & Columns'Img & " columns");
  173. -- 'Img is a GNAT extention
  174. Refresh_Without_Update (Win);
  175. return 0; -- Curses_OK;
  176. end rip_header;
  177. procedure usage is
  178. -- type Stringa is access String;
  179. use Ada.Strings.Unbounded;
  180. -- tbl : constant array (Positive range <>) of Stringa := (
  181. tbl : constant array (Positive range <>) of Unbounded_String
  182. := (
  183. To_Unbounded_String ("Usage: ncurses [options]"),
  184. To_Unbounded_String (""),
  185. To_Unbounded_String ("Options:"),
  186. To_Unbounded_String (" -a f,b set default-colors " &
  187. "(assumed white-on-black)"),
  188. To_Unbounded_String (" -d use default-colors if terminal " &
  189. "supports them"),
  190. To_Unbounded_String (" -e fmt specify format for soft-keys " &
  191. "test (e)"),
  192. To_Unbounded_String (" -f rip-off footer line " &
  193. "(can repeat)"),
  194. To_Unbounded_String (" -h rip-off header line " &
  195. "(can repeat)"),
  196. To_Unbounded_String (" -s msec specify nominal time for " &
  197. "panel-demo (default: 1, to hold)"),
  198. To_Unbounded_String (" -t mask specify default trace-level " &
  199. "(may toggle with ^T)")
  200. );
  201. begin
  202. for n in tbl'Range loop
  203. Put_Line (Standard_Error, To_String (tbl (n)));
  204. end loop;
  205. -- exit(EXIT_FAILURE);
  206. -- TODO should we use Set_Exit_Status and throw and exception?
  207. end usage;
  208. procedure Set_Terminal_Modes is begin
  209. Set_Raw_Mode (SwitchOn => False);
  210. Set_Cbreak_Mode (SwitchOn => True);
  211. Set_Echo_Mode (SwitchOn => False);
  212. Allow_Scrolling (Mode => True);
  213. Use_Insert_Delete_Line (Do_Idl => True);
  214. Set_KeyPad_Mode (SwitchOn => True);
  215. end Set_Terminal_Modes;
  216. nap_msec : Integer := 1;
  217. function Do_Single_Test (c : Character) return Boolean is
  218. begin
  219. case c is
  220. when 'a' =>
  221. getch_test;
  222. when 'b' =>
  223. attr_test;
  224. when 'c' =>
  225. if not Has_Colors then
  226. Cannot ("does not support color.");
  227. else
  228. color_test;
  229. end if;
  230. when 'd' =>
  231. if not Has_Colors then
  232. Cannot ("does not support color.");
  233. elsif not Can_Change_Color then
  234. Cannot ("has hardwired color values.");
  235. else
  236. color_edit;
  237. end if;
  238. when 'e' =>
  239. slk_test;
  240. when 'f' =>
  241. acs_display;
  242. when 'o' =>
  243. demo_panels (nap_msec);
  244. when 'g' =>
  245. acs_and_scroll;
  246. when 'i' =>
  247. flushinp_test (Standard_Window);
  248. when 'k' =>
  249. test_sgr_attributes;
  250. when 'm' =>
  251. menu_test;
  252. when 'p' =>
  253. demo_pad;
  254. when 'r' =>
  255. demo_forms;
  256. when 's' =>
  257. overlap_test;
  258. when 't' =>
  259. trace_set;
  260. when '?' =>
  261. null;
  262. when others => return False;
  263. end case;
  264. return True;
  265. end Do_Single_Test;
  266. command : Character;
  267. my_e_param : Soft_Label_Key_Format := Four_Four;
  268. assumed_colors : Boolean := False;
  269. default_colors : Boolean := False;
  270. default_fg : Color_Number := White;
  271. default_bg : Color_Number := Black;
  272. -- nap_msec was an unsigned long integer in the C version,
  273. -- yet napms only takes an int!
  274. c : Integer;
  275. c2 : Character;
  276. optind : Integer := 1; -- must be initialized to one.
  277. optarg : getopt.stringa;
  278. length : Integer;
  279. tmpi : Integer;
  280. package myio is new Ada.Text_IO.Integer_IO (Integer);
  281. use myio;
  282. save_trace : Integer := 0;
  283. save_trace_set : Trace_Attribute_Set;
  284. function main return Integer is
  285. begin
  286. loop
  287. Qgetopt (c, Argument_Count, Argument'Access,
  288. "a:de:fhs:t:", optind, optarg);
  289. exit when c = -1;
  290. c2 := Character'Val (c);
  291. case c2 is
  292. when 'a' =>
  293. -- Ada doesn't have scanf, it doesn't even have a
  294. -- regular expression library.
  295. assumed_colors := True;
  296. myio.Get (optarg.all, Integer (default_fg), length);
  297. myio.Get (optarg.all (length + 2 .. optarg.all'Length),
  298. Integer (default_bg), length);
  299. when 'd' =>
  300. default_colors := True;
  301. when 'e' =>
  302. myio.Get (optarg.all, tmpi, length);
  303. if tmpi > 3 then
  304. usage;
  305. return 1;
  306. end if;
  307. my_e_param := Soft_Label_Key_Format'Val (tmpi);
  308. when 'f' =>
  309. Rip_Off_Lines (-1, rip_footer'Access);
  310. when 'h' =>
  311. Rip_Off_Lines (1, rip_header'Access);
  312. when 's' =>
  313. myio.Get (optarg.all, nap_msec, length);
  314. when 't' =>
  315. myio.Get (optarg.all, save_trace, length);
  316. when others =>
  317. usage;
  318. return 1;
  319. end case;
  320. end loop;
  321. -- the C version had a bunch of macros here.
  322. -- if (!isatty(fileno(stdin)))
  323. -- isatty is not available in the standard Ada so skip it.
  324. save_trace_set := To_trace (save_trace);
  325. Trace_On (save_trace_set);
  326. Init_Soft_Label_Keys (my_e_param);
  327. Init_Screen;
  328. Set_Background (Ch => (Ch => Blank,
  329. Attr => Normal_Video,
  330. Color => Color_Pair'First));
  331. if Has_Colors then
  332. Start_Color;
  333. if default_colors then
  334. Use_Default_Colors;
  335. elsif assumed_colors then
  336. Assume_Default_Colors (default_fg, default_bg);
  337. end if;
  338. end if;
  339. Set_Terminal_Modes;
  340. Save_Curses_Mode (Curses);
  341. End_Windows;
  342. -- TODO add macro #if blocks.
  343. Put_Line ("Welcome to " & Curses_Version & ". Press ? for help.");
  344. loop
  345. Put_Line ("This is the ncurses main menu");
  346. Put_Line ("a = keyboard and mouse input test");
  347. Put_Line ("b = character attribute test");
  348. Put_Line ("c = color test pattern");
  349. Put_Line ("d = edit RGB color values");
  350. Put_Line ("e = exercise soft keys");
  351. Put_Line ("f = display ACS characters");
  352. Put_Line ("g = display windows and scrolling");
  353. Put_Line ("i = test of flushinp()");
  354. Put_Line ("k = display character attributes");
  355. Put_Line ("m = menu code test");
  356. Put_Line ("o = exercise panels library");
  357. Put_Line ("p = exercise pad features");
  358. Put_Line ("q = quit");
  359. Put_Line ("r = exercise forms code");
  360. Put_Line ("s = overlapping-refresh test");
  361. Put_Line ("t = set trace level");
  362. Put_Line ("? = repeat this command summary");
  363. Put ("> ");
  364. Flush;
  365. command := Ada.Characters.Latin_1.NUL;
  366. -- get_input:
  367. -- loop
  368. declare
  369. Ch : Character;
  370. begin
  371. Get (Ch);
  372. -- TODO if read(ch) <= 0
  373. -- TODO ada doesn't have an Is_Space function
  374. command := Ch;
  375. -- TODO if ch = '\n' or '\r' are these in Ada?
  376. end;
  377. -- end loop get_input;
  378. declare
  379. begin
  380. if Do_Single_Test (command) then
  381. Flush_Input;
  382. Set_Terminal_Modes;
  383. Reset_Curses_Mode (Curses);
  384. Clear;
  385. Refresh;
  386. End_Windows;
  387. if command = '?' then
  388. Put_Line ("This is the ncurses capability tester.");
  389. Put_Line ("You may select a test from the main menu by " &
  390. "typing the");
  391. Put_Line ("key letter of the choice (the letter to left " &
  392. "of the =)");
  393. Put_Line ("at the > prompt. The commands `x' or `q' will " &
  394. "exit.");
  395. end if;
  396. -- continue; --why continue in the C version?
  397. end if;
  398. exception
  399. when Curses_Exception => End_Windows;
  400. end;
  401. exit when command = 'q';
  402. end loop;
  403. Curses_Free_All;
  404. return 0; -- TODO ExitProgram(EXIT_SUCCESS);
  405. end main;
  406. end ncurses2.m;