More C interfacing issues

Simon Marlow simonmar at microsoft.com
Wed Jul 3 04:57:26 EDT 2002


> -----Original Message-----
> From: Alastair Reid [mailto:reid at reid-consulting-uk.ltd.uk] 
> Sent: 02 July 2002 18:34
> To: Ian Lynagh
> Cc: ffi at haskell.org
> Subject: Re: More C interfacing issues
> 
> 
> 
> > Firstly, some ncurses calls return a *WINDOW and others take one as
> > an argument but you never actually look at the contents of one
> > yourself. I have defined "data Window = Window" and am using Ptr
> > Window for the types of these. However, this means I get a warning
> > about an unused constructor. Is there a better way to do this?
> 
> Hugs supports:
> 
>   data Window
> 
> and I believe GHC and NHC do too.  (Malcolm, SimonM: please 
> shout if I overstate.)

Yes, GHC does.

> 
> > Secondly, does anyone have any suggestions on how to provide an
> > interface to this?
> 
> >     void getyx(WINDOW *win, int y, int x);
>     
> >     The getyx macro places the current cursor position of the given
> > window in the two integer variables y and x.
> 
> > The type would probably ideally be "Ptr Window -> IO (CInt, CInt)".
> 
> The easy way is to use GreenCard (the other ffi frontends may be of
> use too).
> 
> Or, you can do what GreenCard does which is to add 3 wrapper functions
> more or less like this:

What's wrong with just doing this in the plain FFI?

data Window
foreign import ccall "getyx" 
  getyx :: Ptr Window -> Ptr CInt -> Ptr CInt -> IO ()

niceGetYX :: Ptr Window -> IO (CInt, CInt)
niceGetYX win =
 alloca $ \py ->
  alloca $ \px -> do
   getyx win py px
   y <- peek py
   x <- peek px
   return (x,y)
   
Have I missed something?

Cheers,
	Simon



More information about the FFI mailing list