More C interfacing issues

Alastair Reid reid at reid-consulting-uk.ltd.uk
Tue Jul 2 13:34:02 EDT 2002


> 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.)

> 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:

  static int x;
  static int y;
  void wrap_getyx(WINDOW *win) { getyx(win,x,y); }
  int getx(void) { return x; }
  int gety(void) { return y; }

or what GreenCard used to do which is more or less

  struct result { int x, int y };
  void wrap_getyx(WINDOW *win, struct result *r) { 
    getyx(win,&(r->y),&(r->x));
  }

  getyx :: Ptr Window -> IO (CInt, CInt)"
  getyx w = do
    r <- alloc sizeof_result
    wrap_getyx w r
    x <- deref r 0
    y <- deref r 1
    free r
    return (y,x)
    
or guess/check that getyx is really, really cheap to call and write two
wrappers gety and getx.

--
Alastair Reid   
alastair at reid-consulting-uk.ltd.uk  
http://www.reid-consulting-uk.ltd.uk/alastair/



More information about the FFI mailing list