aboutsummaryrefslogtreecommitdiff
path: root/Ada95/samples/ncurses2-getch_test.adb
blob: 2802cfb55017e8e93a579cb273315fa97b9b6ba5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
------------------------------------------------------------------------------
--                                                                          --
--                       GNAT ncurses Binding Samples                       --
--                                                                          --
--                                 ncurses                                  --
--                                                                          --
--                                 B O D Y                                  --
--                                                                          --
------------------------------------------------------------------------------
-- Copyright (c) 2000-2008,2009 Free Software Foundation, Inc.              --
--                                                                          --
-- Permission is hereby granted, free of charge, to any person obtaining a  --
-- copy of this software and associated documentation files (the            --
-- "Software"), to deal in the Software without restriction, including      --
-- without limitation the rights to use, copy, modify, merge, publish,      --
-- distribute, distribute with modifications, sublicense, and/or sell       --
-- copies of the Software, and to permit persons to whom the Software is    --
-- furnished to do so, subject to the following conditions:                 --
--                                                                          --
-- The above copyright notice and this permission notice shall be included  --
-- in all copies or substantial portions of the Software.                   --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
-- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
-- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
--                                                                          --
-- Except as contained in this notice, the name(s) of the above copyright   --
-- holders shall not be used in advertising or otherwise to promote the     --
-- sale, use or other dealings in this Software without prior written       --
-- authorization.                                                           --
------------------------------------------------------------------------------
--  Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
--  Version Control
--  $Revision: 1.8 $
--  $Date: 2009/12/26 17:38:58 $
--  Binding Version 01.00
------------------------------------------------------------------------------
--  Character input test
--  test the keypad feature

with ncurses2.util; use ncurses2.util;

with Terminal_Interface.Curses; use Terminal_Interface.Curses;
with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
with Ada.Characters.Handling;
with Ada.Strings.Bounded;

with ncurses2.genericPuts;

procedure ncurses2.getch_test is
   use Int_IO;

   function mouse_decode (ep : Mouse_Event) return String;

   function mouse_decode (ep : Mouse_Event) return String is
      Y      : Line_Position;
      X      : Column_Position;
      Button : Mouse_Button;
      State  : Button_State;
      package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (200);
      use BS;
      buf : Bounded_String := To_Bounded_String ("");
   begin
      --  Note that these bindings do not allow
      --  two button states,
      --  The C version can print {click-1, click-3} for example.
      --  They also don't have the 'id' or z coordinate.
      Get_Event (ep, Y, X, Button, State);

      --  TODO Append (buf, "id "); from C version
      Append (buf, "at (");
      Append (buf, Column_Position'Image (X));
      Append (buf, ", ");
      Append (buf, Line_Position'Image (Y));
      Append (buf, ") state");
      Append (buf, Mouse_Button'Image (Button));

      Append (buf, " = ");
      Append (buf, Button_State'Image (State));
      return To_String (buf);
   end mouse_decode;

   buf : String (1 .. 1024); --  TODO was BUFSIZE
   n : Integer;
   c : Key_Code;
   blockflag : Timeout_Mode := Blocking;
   firsttime : Boolean := True;
   tmp2  : Event_Mask;
   tmp6 : String (1 .. 6);
   tmp20 : String (1 .. 20);
   x : Column_Position;
   y : Line_Position;
   tmpx : Integer;
   incount : Integer := 0;

begin
   Refresh;
   tmp2 := Start_Mouse (All_Events);
   Add (Str => "Delay in 10ths of a second (<CR> for blocking input)? ");
   Set_Echo_Mode (SwitchOn => True);
   Get (Str => buf);

   Set_Echo_Mode (SwitchOn => False);
   Set_NL_Mode (SwitchOn => False);

   if Ada.Characters.Handling.Is_Digit (buf (1)) then
      Get (Item => n, From => buf, Last => tmpx);
      Set_Timeout_Mode (Mode => Delayed, Amount => n * 100);
      blockflag := Delayed;
   end if;

   c := Character'Pos ('?');
   Set_Raw_Mode (SwitchOn => True);
   loop
      if not firsttime then
         Add (Str => "Key pressed: ");
         Put (tmp6, Integer (c), 8);
         Add (Str => tmp6);
         Add (Ch => ' ');
         if c = Key_Mouse then
            declare
               event : Mouse_Event;
            begin
               event := Get_Mouse;
               Add (Str => "KEY_MOUSE, ");
               Add (Str => mouse_decode (event));
               Add (Ch => newl);
            end;
         elsif c >= Key_Min then
            Key_Name (c, tmp20);
            Add (Str => tmp20);
            --  I used tmp and got bitten by the length problem:->
            Add (Ch => newl);
         elsif c > 16#80# then --  TODO fix, use constant if possible
            declare
               c2 : constant Character := Character'Val (c mod 16#80#);
            begin
               if Ada.Characters.Handling.Is_Graphic (c2) then
                  Add (Str => "M-");
                  Add (Ch => c2);
               else
                  Add (Str => "M-");
                  Add (Str => Un_Control ((Ch => c2,
                                           Color => Color_Pair'First,
                                           Attr => Normal_Video)));
               end if;
               Add (Str => " (high-half character)");
               Add (Ch => newl);
            end;
         else
            declare
               c2 : constant Character := Character'Val (c mod 16#80#);
            begin
               if Ada.Characters.Handling.Is_Graphic (c2) then
                  Add (Ch => c2);
                  Add (Str => " (ASCII printable character)");
                  Add (Ch => newl);
               else
                  Add (Str => Un_Control ((Ch => c2,
                                          Color => Color_Pair'First,
                                          Attr => Normal_Video)));
                  Add (Str => " (ASCII control character)");
                  Add (Ch => newl);
               end if;
            end;
         end if;
         --  TODO I am not sure why this was in the C version
         --  the delay statement scroll anyway.
         Get_Cursor_Position (Line => y, Column => x);
         if y >= Lines - 1 then
            Move_Cursor (Line => 0, Column => 0);
         end if;
         Clear_To_End_Of_Line;
      end if;

      firsttime := False;
      if c = Character'Pos ('g') then
         declare
            package p is new ncurses2.genericPuts (1024);
            use p;
            use p.BS;
            timedout : Boolean := False;
            boundedbuf : Bounded_String;
         begin
            Add (Str => "getstr test: ");
            Set_Echo_Mode (SwitchOn => True);
            --  Note that if delay mode is set
            --  Get can raise an exception.
            --  The C version would print the string it had so far
            --  also TODO get longer length string, like the C version
            declare begin
               myGet (Str => boundedbuf);
            exception when Curses_Exception =>
               Add (Str => "Timed out.");
               Add (Ch => newl);
               timedout := True;
            end;
            --  note that the Ada Get will stop reading at 1024.
            if not timedout then
               Set_Echo_Mode (SwitchOn => False);
               Add (Str => " I saw '");
               myAdd (Str => boundedbuf);
               Add (Str => "'.");
               Add (Ch => newl);
            end if;
         end;
      elsif c = Character'Pos ('s') then
         ShellOut (True);
      elsif c = Character'Pos ('x') or c = Character'Pos ('q') or
        (c = Key_None and blockflag = Blocking) then
         exit;
      elsif c = Character'Pos ('?') then
         Add (Str => "Type any key to see its keypad value.  Also:");
         Add (Ch => newl);
         Add (Str => "g -- triggers a getstr test");
         Add (Ch => newl);
         Add (Str => "s -- shell out");
         Add (Ch => newl);
         Add (Str => "q -- quit");
         Add (Ch => newl);
         Add (Str => "? -- repeats this help message");
         Add (Ch => newl);
      end if;

      loop
         c := Getchar;
         exit when c /= Key_None;
         if blockflag /= Blocking then
            Put (tmp6, incount); --  argh string length!
            Add (Str => tmp6);
            Add (Str => ": input timed out");
            Add (Ch => newl);
         else
            Put (tmp6, incount);
            Add (Str => tmp6);
            Add (Str => ": input error");
            Add (Ch => newl);
            exit;
         end if;
         incount := incount + 1;
      end loop;
   end loop;

   End_Mouse (tmp2);
   Set_Timeout_Mode (Mode => Blocking, Amount => 0); --  amount is ignored
   Set_Raw_Mode (SwitchOn => False);
   Set_NL_Mode (SwitchOn => True);
   Erase;
   End_Windows;
end ncurses2.getch_test;