0

I'm using select to do non-blocking reads from a socket and STDIN. In pseudo-code:

loop
  readable = select(socket, STDIN)
  foreach input in readable
    if input == STDIN
      handle_keyboard_input(read(STDIN))
    else
      handle_socket_input(read(socket))

The above works great. But now I'd like to start using libncurses to build a more sophisticated text-based UI. (I'll probably have two windows: A large log window and a smaller one for the prompt.)

Presumably, the socket handling will remain the same. But I'm unclear on how libncurses abstracts away STDIN. What is the correct idiom for doing non-blocking reads in a libncurses window?

In truth this is in Ruby, but I'm just using thin wrappers around the C APIs, so I felt this was more appropriately tagged as a C problem.

rlkw1024
  • 6,455
  • 1
  • 36
  • 65

1 Answers1

2

Basically, ncurses only reads from its input stream when you call an input function such as getch. So to that extent, ncurses doesn't abstract away anything; all it does is to associates two file descriptors, one for input and one for output, with each SCREEN. See man newterm for details.

If you call nodelay or use timeout/wtimeout to set a timeout of 0, then getch will immediately return an error if there is no pending input for the corresponding window. That's basically all you need to write asynchronous input handling (as far as ncurses is concerned.) If you want to use a select loop, you'll need to deal with the mechanics of scheduling background tasks, etc. ncurses has rudimentary support for multi-threaded applications if you want to go that route.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Would there be any problem with using `select` for the socket and `getch` for STDIN within the same loop? – rlkw1024 Jun 03 '14 at 14:34
  • @Jarrett: No, that would be fine, particularly if you have a single-threaded event loop. The problem might come if you try to do input in one thread and screen display in another thread, since `getch` normally does some screen display, including a `refresh`. – rici Jun 03 '14 at 16:05