ncurses2-util.adb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- ncurses2.util --
  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:51:20 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Ada.Text_IO; use Ada.Text_IO;
  43. pragma Warnings (Off);
  44. with Terminal_Interface.Curses.Aux;
  45. pragma Warnings (On);
  46. with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
  47. with Interfaces.C;
  48. with Interfaces.C.Strings;
  49. with Ada.Characters.Handling;
  50. with ncurses2.genericPuts;
  51. package body ncurses2.util is
  52. -- #defines from C
  53. -- #define CTRL(x) ((x) & 0x1f)
  54. function CTRL (c : Character) return Key_Code is
  55. begin
  56. return Character'Pos (c) mod 16#20#;
  57. -- uses a property of ASCII
  58. -- A = 16#41#; a = 16#61#; ^A = 1 or 16#1#
  59. end CTRL;
  60. function CTRL (c : Character) return Character is
  61. begin
  62. return Character'Val (Character'Pos (c) mod 16#20#);
  63. -- uses a property of ASCII
  64. -- A = 16#41#; a = 16#61#; ^A = 1 or 16#1#
  65. end CTRL;
  66. save_trace : Trace_Attribute_Set;
  67. -- Common function to allow ^T to toggle trace-mode in the middle of a test
  68. -- so that trace-files can be made smaller.
  69. function Getchar (win : Window := Standard_Window) return Key_Code is
  70. c : Key_Code;
  71. begin
  72. -- #ifdef TRACE
  73. c := Get_Keystroke (win);
  74. while c = CTRL ('T') loop
  75. -- if _nc_tracing in C
  76. if Current_Trace_Setting /= Trace_Disable then
  77. save_trace := Current_Trace_Setting;
  78. Trace_Put ("TOGGLE-TRACING OFF");
  79. Current_Trace_Setting := Trace_Disable;
  80. else
  81. Current_Trace_Setting := save_trace;
  82. end if;
  83. Trace_On (Current_Trace_Setting);
  84. if Current_Trace_Setting /= Trace_Disable then
  85. Trace_Put ("TOGGLE-TRACING ON");
  86. end if;
  87. end loop;
  88. -- #else c := Get_Keystroke;
  89. return c;
  90. end Getchar;
  91. procedure Getchar (win : Window := Standard_Window) is
  92. begin
  93. if Getchar (win) < 0 then
  94. Beep;
  95. end if;
  96. end Getchar;
  97. procedure Pause is
  98. begin
  99. Move_Cursor (Line => Lines - 1, Column => 0);
  100. Add (Str => "Press any key to continue... ");
  101. Getchar;
  102. end Pause;
  103. procedure Cannot (s : String) is
  104. use Interfaces.C;
  105. use Interfaces.C.Strings;
  106. use Terminal_Interface.Curses.Aux;
  107. function getenv (x : char_array) return chars_ptr;
  108. pragma Import (C, getenv, "getenv");
  109. tmp1 : char_array (0 .. 10);
  110. package p is new ncurses2.genericPuts (1024);
  111. use p;
  112. use p.BS;
  113. tmpb : BS.Bounded_String;
  114. Length : size_t;
  115. begin
  116. To_C ("TERM", tmp1, Length);
  117. Fill_String (getenv (tmp1), tmpb);
  118. Add (Ch => newl);
  119. myAdd (Str => "This " & tmpb & " terminal " & s);
  120. Pause;
  121. end Cannot;
  122. procedure ShellOut (message : Boolean) is
  123. use Interfaces.C;
  124. Txt : char_array (0 .. 10);
  125. Length : size_t;
  126. procedure system (x : char_array);
  127. pragma Import (C, system, "system");
  128. begin
  129. To_C ("sh", Txt, Length);
  130. if message then
  131. Add (Str => "Shelling out...");
  132. end if;
  133. Save_Curses_Mode (Mode => Curses);
  134. End_Windows;
  135. system (Txt);
  136. if message then
  137. Add (Str => "returned from shellout.");
  138. Add (Ch => newl);
  139. end if;
  140. Refresh;
  141. end ShellOut;
  142. function Is_Digit (c : Key_Code) return Boolean is
  143. begin
  144. if c >= 16#100# then
  145. return False;
  146. else
  147. return Ada.Characters.Handling.Is_Digit (Character'Val (c));
  148. end if;
  149. end Is_Digit;
  150. procedure P (s : String) is
  151. begin
  152. Add (Str => s);
  153. Add (Ch => newl);
  154. end P;
  155. function Code_To_Char (c : Key_Code) return Character is
  156. begin
  157. if c > Character'Pos (Character'Last) then
  158. return Character'Val (0);
  159. -- maybe raise exception?
  160. else
  161. return Character'Val (c);
  162. end if;
  163. end Code_To_Char;
  164. -- This was untestable due to a bug in GNAT (3.12p)
  165. -- Hmm, what bug? I don't remember.
  166. function ctoi (c : Character) return Integer is
  167. begin
  168. return Character'Pos (c) - Character'Pos ('0');
  169. end ctoi;
  170. end ncurses2.util;