1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright 2020 Thomas E. Dickey --
11 -- Copyright 2000-2008,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 with Ada.Text_IO; use Ada.Text_IO;
45 with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
48 with Interfaces.C.Strings;
50 with Ada.Characters.Handling;
52 with ncurses2.genericPuts;
54 package body ncurses2.util is
57 -- #define CTRL(x) ((x) & 0x1f)
58 function CTRL (c : Character) return Key_Code is
60 return Character'Pos (c) mod 16#20#;
61 -- uses a property of ASCII
62 -- A = 16#41#; a = 16#61#; ^A = 1 or 16#1#
65 function CTRL (c : Character) return Character is
67 return Character'Val (Character'Pos (c) mod 16#20#);
68 -- uses a property of ASCII
69 -- A = 16#41#; a = 16#61#; ^A = 1 or 16#1#
72 save_trace : Trace_Attribute_Set;
73 -- Common function to allow ^T to toggle trace-mode in the middle of a test
74 -- so that trace-files can be made smaller.
75 function Getchar (win : Window := Standard_Window) return Key_Code is
79 c := Get_Keystroke (win);
80 while c = CTRL ('T') loop
81 -- if _nc_tracing in C
82 if Current_Trace_Setting /= Trace_Disable then
83 save_trace := Current_Trace_Setting;
84 Trace_Put ("TOGGLE-TRACING OFF");
85 Current_Trace_Setting := Trace_Disable;
87 Current_Trace_Setting := save_trace;
89 Trace_On (Current_Trace_Setting);
90 if Current_Trace_Setting /= Trace_Disable then
91 Trace_Put ("TOGGLE-TRACING ON");
94 -- #else c := Get_Keystroke;
98 procedure Getchar (win : Window := Standard_Window) is
100 if Getchar (win) < 0 then
107 Move_Cursor (Line => Lines - 1, Column => 0);
108 Add (Str => "Press any key to continue... ");
112 procedure Cannot (s : String) is
114 use Interfaces.C.Strings;
115 function getenv (x : char_array) return chars_ptr;
116 pragma Import (C, getenv, "getenv");
117 tmp1 : char_array (0 .. 10);
118 package p is new ncurses2.genericPuts (1024);
122 tmpb : BS.Bounded_String;
126 To_C ("TERM", tmp1, Length);
127 Fill_String (getenv (tmp1), tmpb);
129 myAdd (Str => "This " & tmpb & " terminal " & s);
133 procedure ShellOut (message : Boolean) is
135 Txt : char_array (0 .. 10);
137 procedure system (x : char_array);
138 pragma Import (C, system, "system");
140 To_C ("sh", Txt, Length);
142 Add (Str => "Shelling out...");
144 Save_Curses_Mode (Mode => Curses);
148 Add (Str => "returned from shellout.");
154 function Is_Digit (c : Key_Code) return Boolean is
159 return Ada.Characters.Handling.Is_Digit (Character'Val (c));
163 procedure P (s : String) is
169 function Code_To_Char (c : Key_Code) return Character is
171 if c > Character'Pos (Character'Last) then
172 return Character'Val (0);
173 -- maybe raise exception?
175 return Character'Val (c);
179 -- This was untestable due to a bug in GNAT (3.12p)
180 -- Hmm, what bug? I don't remember.
181 function ctoi (c : Character) return Integer is
183 return Character'Pos (c) - Character'Pos ('0');