comp.lang.ada
 help / color / mirror / Atom feed
* Get_Immediate has  raised  CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
@ 2015-02-18 17:29 darek
  2015-02-18 18:34 ` Dmitry A. Kazakov
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: darek @ 2015-02-18 17:29 UTC (permalink / raw)


Dear All,
  reading from a keyboard  keys such as arrow keys, PgUp, PgDn, etc. seems to be a small challenge in Ada.  For this purpose one of the  Get_Immediate procedures from the Ada.Text_IO shall be used. For the keyboard we can use either 
  - procedure Get_Immediate  (Item : out Character) 
               or
  - procedure Get_Immediate  (Item      : out Character; Available : out Boolean)

When I have used the Get_Immediate procedure with the extra  boolean parameter, it  generates the constraint error for:
  - all arrow keys, PgUp, PgDn 
  - [ character from the keyboard !!!!

Is this a feature or a bug. How pressing a legal key can cause such a weird result? 


The Get_Immediate procedure without the boolean parameter works fine but it waits for a key to be pressed. 

System: Linux Testpc 3.11-2-amd64 #1 SMP Debian 3.11.8-1 (2013-11-13) x86_64 GNU/Linux (running as a guest inside the VirtualBox VM)
Compiler:gcc version 4.7.4 20140401 for GNAT GPL gpl-2014 (20140405) (GCC)

Any ideas how to avoid the problem with this exception? Am I missing something?

Cheers,
  Darek

The code:
with Ada.Text_IO;

procedure  TestKbd is

   package tIO renames Ada.Text_IO;

    procedure Test_Get_Immediate_No_Avail is 
      Char : Character;
   begin
      loop -- an infinite loop 
         tIO.Put_Line("Read key:"); 
         --| Here Get_Immediate is waiting for the input
         tIO.Get_Immediate(Char);
         tIO.Put_Line("Char ==>" & Character'Image(Char) & " code:" & Integer'Image(Character'Pos(Char)));      
         delay (0.1);
      end loop;
   end Test_Get_Immediate_No_Avail;
   
   procedure Test_Get_Immediate_With_Avail is
      Char : Character;
      Avail : Boolean;
   begin
      loop -- an infinite loop 
         tIO.Put_Line("Read key: "); 
         tIO.Get_Immediate(Char, Avail);
         if Avail then 
            tIO.Put_Line("Char ==>" & Character'Image(Char) & " code:" & Integer'Image(Character'Pos(Char)));          
         end if;         
         delay (1.0);
     end loop;
   end Test_Get_Immediate_With_Avail;
   
     
   
begin
   Test_Get_Immediate_With_Avail;
   --Test_Get_Immediate_No_Avail;
end TestKbd;



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has  raised  CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 17:29 Get_Immediate has raised CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise darek
@ 2015-02-18 18:34 ` Dmitry A. Kazakov
  2015-02-18 19:34   ` darek
  2015-02-18 20:16 ` Simon Wright
  2015-02-19 21:37 ` Björn Lundin
  2 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2015-02-18 18:34 UTC (permalink / raw)


On Wed, 18 Feb 2015 09:29:50 -0800 (PST), darek wrote:

> reading from a keyboard  keys such as arrow keys, PgUp, PgDn, etc. seems
> to be a small challenge in Ada.  For this purpose one of the 
> Get_Immediate procedures from the Ada.Text_IO shall be used.

shall?

> For the
> keyboard we can use either 
>   - procedure Get_Immediate  (Item : out Character) 
>                or
>   - procedure Get_Immediate  (Item : out Character; Available : out Boolean)
> 
> When I have used the Get_Immediate procedure with the extra  boolean
> parameter, it  generates the constraint error for:
>   - all arrow keys, PgUp, PgDn 
>   - [ character from the keyboard !!!!
> 
> Is this a feature or a bug. How pressing a legal key can cause such a
> weird result? 

Neither, I suppose.

AFAIK, RM does not mandate any specific mapping of non-character keyboard
keys or, for that matter, any events from any possible input devices
(mouse, joystick, pen, motion detector...) onto the type Character. Except
for formatting keys (e.g. ones generating CR, LF, HT etc)

> The Get_Immediate procedure without the boolean parameter works fine but
> it waits for a key to be pressed. 
>
> Any ideas how to avoid the problem with this exception? Am I missing something?

Yes. Page Up/Down are not a characters. No key necessarily is, however,
some keys may have corresponding characters, or sequences of characters.

You shall not use Text_IO for keyboard input, unless under some ASCII
terminal emulator, e.g. VT52 or VT100.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has  raised  CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 18:34 ` Dmitry A. Kazakov
@ 2015-02-18 19:34   ` darek
  2015-02-18 20:36     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 10+ messages in thread
From: darek @ 2015-02-18 19:34 UTC (permalink / raw)


So, are there any alternatives in Ada (GNAT) that should/may  be used for this trivial task? 

Regards,
  Darek 

On Wednesday, 18 February 2015 19:34:21 UTC+1, Dmitry A. Kazakov  wrote:
> On Wed, 18 Feb 2015 09:29:50 -0800 (PST), darek wrote:
> 
> > reading from a keyboard  keys such as arrow keys, PgUp, PgDn, etc. seems
> > to be a small challenge in Ada.  For this purpose one of the 
> > Get_Immediate procedures from the Ada.Text_IO shall be used.
> 
> shall?
> 
> > For the
> > keyboard we can use either 
> >   - procedure Get_Immediate  (Item : out Character) 
> >                or
> >   - procedure Get_Immediate  (Item : out Character; Available : out Boolean)
> > 
> > When I have used the Get_Immediate procedure with the extra  boolean
> > parameter, it  generates the constraint error for:
> >   - all arrow keys, PgUp, PgDn 
> >   - [ character from the keyboard !!!!
> > 
> > Is this a feature or a bug. How pressing a legal key can cause such a
> > weird result? 
> 
> Neither, I suppose.
> 
> AFAIK, RM does not mandate any specific mapping of non-character keyboard
> keys or, for that matter, any events from any possible input devices
> (mouse, joystick, pen, motion detector...) onto the type Character. Except
> for formatting keys (e.g. ones generating CR, LF, HT etc)
> 
> > The Get_Immediate procedure without the boolean parameter works fine but
> > it waits for a key to be pressed. 
> >
> > Any ideas how to avoid the problem with this exception? Am I missing something?
> 
> Yes. Page Up/Down are not a characters. No key necessarily is, however,
> some keys may have corresponding characters, or sequences of characters.
> 
> You shall not use Text_IO for keyboard input, unless under some ASCII
> terminal emulator, e.g. VT52 or VT100.
> 
> -- 
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has  raised  CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 17:29 Get_Immediate has raised CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise darek
  2015-02-18 18:34 ` Dmitry A. Kazakov
@ 2015-02-18 20:16 ` Simon Wright
  2015-02-18 20:34   ` darek
  2015-02-19 21:37 ` Björn Lundin
  2 siblings, 1 reply; 10+ messages in thread
From: Simon Wright @ 2015-02-18 20:16 UTC (permalink / raw)


Someone was asking a very closely related question (i.e. the very same
constraint error) on Stack Overflow recently [1].

The answer there was to open the input file with a non-default wide
character encoding method: Form => "WCEM=8" was suggested.

Not sure how to get this effect on standard input.You could try
compiling with -gnatW8.

[1] http://stackoverflow.com/q/28486505

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has  raised  CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 20:16 ` Simon Wright
@ 2015-02-18 20:34   ` darek
  0 siblings, 0 replies; 10+ messages in thread
From: darek @ 2015-02-18 20:34 UTC (permalink / raw)


Hi Simon,
  the  -gnatW8 compiler option does the trick - the problem is solved.  Thanks for your suggestions. It would take same time for me to figure this out on my own. 
On the other hand, in such a versatile  system (GNAT Ada) it should work "out of the box" via a specialised (and OS independent)  package. 

Thanks again. 

Cheers,
  Darek 

On Wednesday, 18 February 2015 21:16:17 UTC+1, Simon Wright  wrote:
> Someone was asking a very closely related question (i.e. the very same
> constraint error) on Stack Overflow recently [1].
> 
> The answer there was to open the input file with a non-default wide
> character encoding method: Form => "WCEM=8" was suggested.
> 
> Not sure how to get this effect on standard input.You could try
> compiling with -gnatW8.
> 
> [1] http://stackoverflow.com/q/28486505


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has raised CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 19:34   ` darek
@ 2015-02-18 20:36     ` Dmitry A. Kazakov
  2015-02-18 21:19       ` darek
  2015-02-18 22:09       ` Georg Bauhaus
  0 siblings, 2 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2015-02-18 20:36 UTC (permalink / raw)


On Wed, 18 Feb 2015 11:34:34 -0800 (PST), darek wrote:

> So, are there any alternatives in Ada (GNAT) that should/may  be used for
> this trivial task? 

It is not a trivial task, even in the days of teletypes, it was not. As for
alternatives, Ada or not, use ncurses if you have to stay console-only, or
a console terminal emulator, see below.

Better make a proper GUI using GTK, Qt, WinGDI or gnoga. All have Ada
interfaces, of course.

Any such library provides an abstraction level between your application and
the input devices.

AFAIK, there exist some implementations of famous MS-DOS ANSI.SYS for
Win32. E.g.

   https://github.com/adoxa/ansicon

Never tried it, though.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has  raised  CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 20:36     ` Dmitry A. Kazakov
@ 2015-02-18 21:19       ` darek
  2015-02-18 21:44         ` Dmitry A. Kazakov
  2015-02-18 22:09       ` Georg Bauhaus
  1 sibling, 1 reply; 10+ messages in thread
From: darek @ 2015-02-18 21:19 UTC (permalink / raw)


Hi Dmitry,
The software I am developing is for an internal use (remote control of a PTZ camera, video recording, and radar data acquisition subsystem). It runs on a very minimal Linux (and an industrial PC because of its operational temperature and available interfaces - http://www.comp-mall.de/DRPC-100.html). At this moment no fancy graphics and the simplest working solution is the way to go.

Thanks for your suggestions. 

Cheers, 
  Darek



On Wednesday, 18 February 2015 21:36:18 UTC+1, Dmitry A. Kazakov  wrote:
> On Wed, 18 Feb 2015 11:34:34 -0800 (PST), darek wrote:
> 
> > So, are there any alternatives in Ada (GNAT) that should/may  be used for
> > this trivial task? 
> 
> It is not a trivial task, even in the days of teletypes, it was not. As for
> alternatives, Ada or not, use ncurses if you have to stay console-only, or
> a console terminal emulator, see below.
> 
> Better make a proper GUI using GTK, Qt, WinGDI or gnoga. All have Ada
> interfaces, of course.
> 
> Any such library provides an abstraction level between your application and
> the input devices.
> 
> AFAIK, there exist some implementations of famous MS-DOS ANSI.SYS for
> Win32. E.g.
> 
>    https://github.com/adoxa/ansicon
> 
> Never tried it, though.
> 
> -- 
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has raised CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 21:19       ` darek
@ 2015-02-18 21:44         ` Dmitry A. Kazakov
  0 siblings, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2015-02-18 21:44 UTC (permalink / raw)


On Wed, 18 Feb 2015 13:19:47 -0800 (PST), darek wrote:

> The software I am developing is for an internal use (remote control of a
> PTZ camera, video recording, and radar data acquisition subsystem). It
> runs on a very minimal Linux (and an industrial PC because of its
> operational temperature and available interfaces - http://www.comp-mall.de/DRPC-100.html).

A quite fat expensive box compared to the boards we are using (with Ada of
course).

IMO, there should be no problem to run GTK or Qt on it, with full X11.

But you could also give gnoga a try. It is Web-based, so you would not need
to run X11 on the box. The box will run only a HTTP server (in 100% Ada). A
remote browser will do the rendering, no keyboard, no mouse. Which could be
an advantage, as an industrial PC is usually mounted in some inaccessible
rack.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has  raised  CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 20:36     ` Dmitry A. Kazakov
  2015-02-18 21:19       ` darek
@ 2015-02-18 22:09       ` Georg Bauhaus
  1 sibling, 0 replies; 10+ messages in thread
From: Georg Bauhaus @ 2015-02-18 22:09 UTC (permalink / raw)


On 18.02.15 21:36, Dmitry A. Kazakov wrote:
> On Wed, 18 Feb 2015 11:34:34 -0800 (PST), darek wrote:
>
>> So, are there any alternatives in Ada (GNAT) that should/may  be used for
>> this trivial task?
>
> It is not a trivial task, even in the days of teletypes, it was not. As for
> alternatives, Ada or not, use ncurses if you have to stay console-only, or
> a console terminal emulator, see below.
>
> Better make a proper GUI using GTK, Qt, WinGDI or gnoga. All have Ada
> interfaces, of course.

Ncurses, if at all necessary: in addition to the use case Darek has
mentioned, another use case  will not warrant a fancy GUI either;
there will be cursing when one needs to

(a) install a fully equipped graphical workstation(*) to manage
     a GNU/Linux based server system, say

(b) which drags in a load of software for just that fancy GUI,
     inflating the software repertoire to be managed; the "real"
      software does not need it at all.

Condescending comments will ensue, typically starting with "Just install …".
But they will only be heard as long as the speaker has not had
to manage a single task slightly different from what the GUI
designers have covered, or has had to do it at a computer that isn't
his own.

Many GUIs I have seen were incomplete, sluggish, and non-standard.
That's no surprise, I think, as it takes a fair amount of work to
make them handle everything well. In particular, when the OS offers
no direct support for such programs.  The task is difficult already when
using just text!

Maybe there is a correlation between administrative GUIs being
half-baked and graphical programming (not: GUI programming)
being not that popular?


Of course, all applies less to Windows™ XP style programming, as the
feel of text or scripting is a fairly recent addition to Windows™ NT
based OSs, and as users expect installers, say, to be as good as those
programs for controlling driver software. 8-)

__
(*) the screen may actually be virtual, without an option to
quickly alter its size, which adds another set of display problems,
e.g. dialogue windows being too large.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Get_Immediate has  raised  CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise
  2015-02-18 17:29 Get_Immediate has raised CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise darek
  2015-02-18 18:34 ` Dmitry A. Kazakov
  2015-02-18 20:16 ` Simon Wright
@ 2015-02-19 21:37 ` Björn Lundin
  2 siblings, 0 replies; 10+ messages in thread
From: Björn Lundin @ 2015-02-19 21:37 UTC (permalink / raw)


On 2015-02-18 18:29, darek wrote:
> Dear All,
>   reading from a keyboard  keys such as arrow keys, PgUp, PgDn, etc. seems to be a small challenge in Ada.  For this purpose one of the  Get_Immediate procedures from the Ada.Text_IO shall be used. For the keyboard we can use either 
>   - procedure Get_Immediate  (Item : out Character) 
>                or
>   - procedure Get_Immediate  (Item      : out Character; Available : out Boolean)
> 

if you use SDL, I think it will do the trick. I have some code that does

      SDL.Events.PollEventVP (PollEvent_Result, Event);
      exit when PollEvent_Result = 0;
      case Event.The_Type is
         when SDL.Events.KEYDOWN =>
           declare
             -- player 0 -> w,a,s,z
             -- player 1 -> up,left,rigt,down
             Name_Of_Key : String :=
C.Strings.Value(SDL.Keyboard.GetKeyName(Event.key.keysym.sym));
           begin
             Log("Game.Handle_Events"," Key down: '" & Name_Of_Key & "'");
             if    Name_Of_Key = "up" then
               Game.Player(Player_Idx).Game_Obj_Ptr.Heading := Up;
             elsif Name_Of_Key = "right" then
               Game.Player(Player_Idx).Game_Obj_Ptr.Heading := Right;
             elsif Name_Of_Key = "down" then
               Game.Player(Player_Idx).Game_Obj_Ptr.Heading := Down;
             elsif Name_Of_Key = "left" then
....




--
Björn

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-02-19 21:37 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-18 17:29 Get_Immediate has raised CONSTRAINT_ERROR : s-wchcnv.adb:207 explicit raise darek
2015-02-18 18:34 ` Dmitry A. Kazakov
2015-02-18 19:34   ` darek
2015-02-18 20:36     ` Dmitry A. Kazakov
2015-02-18 21:19       ` darek
2015-02-18 21:44         ` Dmitry A. Kazakov
2015-02-18 22:09       ` Georg Bauhaus
2015-02-18 20:16 ` Simon Wright
2015-02-18 20:34   ` darek
2015-02-19 21:37 ` Björn Lundin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox