This is example 2. This example demonstrates the following:

  1. Setting up an event loop that reads user input from the console

  2. Processing (single character) user commands

Note, this is not using any eye-tracker functions, nor is it part of the EyeVec SDK. This is just to demonstrate what we’ll be using in the other example programs as a placeholder for your actual eye-tracking application. It enables us to test many eye-tracker functions interactively without having to write a graphic application (although that would work too).

Source code

Source file: example2.c

Additional source files used by this example and others can be found in the utils directory. You don’t need to give any attention to these files.

Files Description

utils_console.c, utils_console.h

Utility functions for retrieving input from console.

utils_signal.c, utils_signal.h

Utility functions for signal handling.

utils_time.c, utils_time.h

Utility function for retrieving the current time in us.

utils_wait.c, utils_wait.h

Utility functions for waiting on descriptor readiness.

inputLoop()

The structure of the program is simple. The main() function calls function inputLoop() which retrieves a character from the console and then calls function processInput() to handle the single character command. It loops until you hit q or you interrupt the program or some eror occurs.

static int inputLoop(void)
{
#if defined(EYEVEC_PLATFORM_WINDOWS)
    HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
#else
    int console = fileno(stdin);
#endif

    int ret = 0;
    while (true) {
        ret = waitForInput1(console, -1); (1)
        if (quit_by_signal) break;  // Interrupted by user, quit.
        if (ret == 0) continue;     // Timeout, keep going.
        if (ret < 0) break;         // Error.

        // Get user input (single character).
        int ch = 0;
        if (kbhit()) {
            ch = getch();
            if (ch < 0) break;
        }
        if (ch == '\n') printf("\n");
        if (ch == 0 || !isprint(ch)) continue;  // Ignore non-printables.

        printf("[%c]\n", ch);
        ret = processInput(ch); (2)
        if (ret != 0) break;
    }

    return (ret >= 0) ? 0 : ret;
}
1 Wait until user hits a key.
2 Call processInput() to handle the key.

processInput()

The processInput() function checks the specified single character command and executes the action defined for it. In this particular example it’s basically all no-op.

static int processInput(int ch)
{
    if (ch == 'q' || ch == 'Q') return 1;   // Quit on 'q'.
    if (ch == '?') {
        printf(
            "q            quit\n"
            "h            say hi\n"
        );
    }
    else if (ch == 'h') {
        printf("Hello there!\n");
    }
    else {
        printf("Unhandled input: %c\n", ch);
    }

    return 0;   // Keep going.
}

In the other example programs we will extend this pattern enabling us to interactively test various eye-tracker functions.

Building and running

To build this example first navigate to the utils directory and run make, then go back to the example2 directory and run make again.

After a succesful build run the program:

  1. Press ? to show help.

  2. Press h to make the program say hello.

  3. Press x to see message for unhandled input.

  4. Press q to quit.

Output might look like this (empty lines added for clarity):

Type q to quit, ? for help.

[?]
q            quit
h            say hi

[h]
Hello there!

[x]
Unhandled input: x

[q]