Hi all, A couple more questions. 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? 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)". Thanks Ian
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@reid-consulting-uk.ltd.uk http://www.reid-consulting-uk.ltd.uk/alastair/
Alastair Reid wrote:
[...] 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; } [...]
Uh, oh! Don't even try this if you ever want to be thread-safe! :-}
or what GreenCard used to do which is more or less [...]
Much better, and a two-element array would be even easier (see the FFI's marshaling utitlities) and more portable (structure layout is an ABI issue) here. Cheers, S.
Uh, oh! Don't even try this if you ever want to be thread-safe! :-}
Yeah, I know. For what it's worth, it's not what GreenCard does with the native Hugs backend (at least, it wasn't last time I looked). Hugs primitives are allowed to return tuples of values so they do. It's a shame C doesn't support multiple return values and kinda surprising given that C is like assembler in so many other ways. -- Alastair Reid alastair@reid-consulting-uk.ltd.uk http://www.reid-consulting-uk.ltd.uk/alastair/
On Tue, Jul 02, 2002 at 06:34:02PM +0100, Alastair Reid wrote:
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.)
I'd rather stick to H'98+FFI where possible.
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)".
[snip] Thanks to you both for your input - I think I'll go with Sven's two element array. Ian
participants (4)
-
Alastair Reid -
Ian Lynagh -
Malcolm Wallace -
Sven Panne