10
votes

I have a strange problem with functions timeout and getch from the ncurses library used in Haskell. When I use them from GHCi or runhaskell, they work as expected -- getch waits for the number of miliseconds given to timeout and then returns, even if no input was given. But when I compile the same file using GHC, getch returns immediately.

I tried two ncurses bindings for Haskell; hscurses:

import UI.HSCurses.Curses

main = do
  initCurses
  timeout 1000
  c <- getch
  endWin
  print c

and ncurses:

import UI.NCurses

main = do
  e <- runCurses $ do
    win <- defaultWindow
    getEvent win $ Just 1000
  print e

Both behave the same strange way described before.

I also tried equivalent program in C:

#include <ncurses.h>

int main()
{
  initscr();
  wtimeout(stdscr,1000);
  int c = getch();
  endwin();
  printf("%d\n", c);
  return 0;
}

This one works as expected.

So my question is: what can make the difference when using terminal from interpreted and from compiled Haskell? Do runhaskell and ghci modify some subtle terminal settings? Or does the compiled code load libraries a different way?

ADDED:

I tried to call a the C program from compiled Haskell using FFI and it returned immediately (which is incorrect). I think that means that the problem isn't in the libraries, but somewhere in GHC's runtime.

1
if the code shown above really behaves as described when compiled, you should submit a bug report to the library maintainers. - didierc
The Haskell libraries should do essentialy the same as the C program and they work correctly when interpreted, so I don't think that the problem is here. - Jan Špaček
well, it works for me either from the repl or with runhaskell. - didierc
hscurses ver. 1.3.0.2, ghc 6.12.1 - didierc
@didierc Yes, it works from ghci and runhaskell; the problem is that it doesn't work when compiled (for me). - Jan Špaček

1 Answers

1
votes

I tried your code - slightly modified with a larger timeout value - using runhaskell, and ghc with the following commands:

$ runhaskell so_15305317.hs

$ ghc -packages hscurses -lncurses so_15305317.hs
$ ./a.out

In both cases, I ended up with the expected behaviour. Your installation of ghc must be broken, or the command used for compilation including parameters breaking the library behaviour.

ghc version is 6.12.1, and hcurses is 1.13.0.2, on a debian 6.0.5 system.