Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference for ncurses between interpreted and compiled Haskell?

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.

like image 560
Jan Špaček Avatar asked Mar 08 '13 22:03

Jan Špaček


1 Answers

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.

like image 127
didierc Avatar answered Nov 07 '22 12:11

didierc