How to do non-blocking keyboard input reading on console app?

Hi,

I'd like to make a simple game as console app. I want the game keeps on going with the sprite and animation (using emojis) while there's no keyboard input; and it will react accordingly if there are ones such as arrow keys to direct the actor path, etc. However, I don't know how to do it using Swift (v5).

I knew there's termios() in Darwin module but I found it incompatible with many C examples out there. Any hints, please?

Thank you.

~Bee

You probably want to look into Dispatch. It's the goto for things dealing with asynchronous processing.

I don't think I would need Dispatch. There's some configurations in Terminal via termios() to make it non-blocking. But I don't know how to do it in Swift.

Would you mind linking one of these non-working C examples? Unless they utilize weird macros or variadic functions, you should be able to call all the C stuff you need.

Here a C example that I found. It works with C but I failed to convert it to Swift using Darwin module.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>

struct termios orig_termios;

void reset_terminal_mode()
{
    tcsetattr(0, TCSANOW, &orig_termios);
}

void set_conio_terminal_mode()
{
    struct termios new_termios;

    /* take two copies - one for now, one for later */
    tcgetattr(0, &orig_termios);
    memcpy(&new_termios, &orig_termios, sizeof(new_termios));

    /* register cleanup handler, and set the new terminal mode */
    atexit(reset_terminal_mode);
    cfmakeraw(&new_termios);
    tcsetattr(0, TCSANOW, &new_termios);
}

int kbhit()
{
    struct timeval tv = { 0L, 0L };
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(0, &fds);
    return select(1, &fds, NULL, NULL, &tv);
}

int getch()
{
    int r;
    unsigned char c;
    if ((r = read(0, &c, sizeof(c))) < 0) {
        return r;
    } else {
        return c;
    }
}

int main(int argc, char *argv[])
{
    int key;

    printf("press a key: ");
    fflush(stdout);

    set_conio_terminal_mode();

    while (1) {
        if (kbhit()) {
            key = getch();

            if (key == 13) {
                printf("\n\r");
                break;
            } else if (key >= 20) {
                printf("%c, ", key);
                fflush(stdout);
            }
        }
        else {
            /* do some work */
            printf(".");
            usleep(10);
            printf(".");
            usleep(10);
            printf(".");
            usleep(10);
            printf("\e[3D");
            usleep(10);
        }
    }

    reset_terminal_mode();
}

There are some variables and methods that don't exist in Darwin module, such as the FD_ZERO.

It appears FD_SET and FD_ZERO are implemented as macros, which is why they aren't importing properly.

To get around this, add a C header to your swift project with the following declarations and them import it into your swift project using a method like this:

#pragma once
#include <sys/select.h>

static inline void fdset_zero(fd_set *set) { FD_ZERO(set); }
static inline void fdset_set(int fd, fd_set *set) { FD_SET(fd, set); }
static inline void fdset_clr(int fd, fd_set *set) { FD_CLR(fd, set); }
static inline int fdset_isset(int fd, fd_set *set) { return FD_ISSET(fd, set); }
2 Likes

There are some variables and methods that don't exist in Darwin
module, such as the FD_ZERO.

The select you’re doing on STDIN_FILENO in kbhit [1] is equivalent to using a Dispatch source, and Dispatch sources have a much nicer projection into Swift.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

[1] Well, kbhit isn’t using STDIN_FILENO, but it should be (-:

2 Likes

Alright, I'll see what I can do with Dispatch. Thank you.

If you do end up going with select, I created an SPM package that includes the select C macros which can be imported to swift: GitHub - Ponyboy47/CSelect: Expose some of the <sys/select.h> file descriptor functions for swift

Thank you for the help, guys. I appreciate it. I've found the solution, using pollfd instead of select or Dispatch. The working example is here: non-blocking keyboard input in swift - Pastebin.com