Linux Step By Steps

BORLAND <> LINUX (KBHIT, Sleep etc)

From
Jeffrey.F.Hawkins@motorola.com Sun Oct 29 07:40:29 2000
MikeRo@norfolk.nf
Anders Brander <anders@brander.dk>

C and C++ code examples below


"_sleep()" can be replaced by a number of Linux (UNIX) functions, depending on the time resolution. Below are some of these functions (reference the man pages).

sleep() - second resolution
usleep() - microsecond resolution
nanosleep() - nanosecond resolution


getch() is getchar() or use the readch() function I provide below.

kbhit() is not available on Linux (UNIX), it is a DOS function. Below is some code (functions) to replace the "kbhit" functionality (this code is from a book called "Beginning Linux Programming", from Wrox Press -- www.wrox.com -- you can download the Code Examples from the Book at this Site):

CCODE
/********************************************************************/

KBHIT.H

#ifndef KBHITh
#define KBHITh

void   init_keyboard(void);
void   close_keyboard(void);
int      kbhit(void);
int     readch(void);

#endif

KBHIT.C

#include "kbhit.h"
#include <termios.h>

#include <unistd.h>   // for read()

static struct termios initial_settings, new_settings;
static int peek_character = -1;

void init_keyboard()
{
    tcgetattr(0,&initial_settings);
    new_settings = initial_settings;
    new_settings.c_lflag &= ~ICANON;
    new_settings.c_lflag &= ~ECHO;
    new_settings.c_lflag &= ~ISIG;
    new_settings.c_cc[VMIN] = 1;
    new_settings.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &new_settings);
}

void close_keyboard()
{
    tcsetattr(0, TCSANOW, &initial_settings);
}

int kbhit()
{
unsigned char ch;
int nread;

    if (peek_character != -1) return 1;
    new_settings.c_cc[VMIN]=0;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0,&ch,1);
    new_settings.c_cc[VMIN]=1;
    tcsetattr(0, TCSANOW, &new_settings);
    if(nread == 1)
    {
        peek_character = ch;
        return 1;
    }
    return 0;
}

int readch()
{
char ch;

    if(peek_character != -1)
    {
        ch = peek_character;
        peek_character = -1;
        return ch;
    }
    read(0,&ch,1);
    return ch;
}

/***************************************************/
C++ CODE
///////////////////////////////////////////////////////////
// KBHIT.H

#ifndef KBHITh
#define KBHITh

#include <termios.h>

class keyboard
{
public:

      keyboard();

    ~keyboard();
    int kbhit();
    int getch();

private:

    struct termios initial_settings, new_settings;
    int peek_character;

};

#endif

// KBHIT.CPP

#include "kbhit.h"
#include <unistd.h> // read()

keyboard::keyboard()
{
    tcgetattr(0,&initial_settings);

    new_settings = initial_settings;
    new_settings.c_lflag &= ~ICANON;
    new_settings.c_lflag &= ~ECHO;
    new_settings.c_lflag &= ~ISIG;
    new_settings.c_cc[VMIN] = 1;
    new_settings.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &new_settings);
    peek_character=-1;
}

keyboard::~keyboard()
{
    tcsetattr(0, TCSANOW, &initial_settings);
}

int keyboard::kbhit()
{
unsigned char ch;
int nread;

    if (peek_character != -1) return 1;
    new_settings.c_cc[VMIN]=0;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0,&ch,1);
    new_settings.c_cc[VMIN]=1;
    tcsetattr(0, TCSANOW, &new_settings);

    if (nread == 1)
    {
        peek_character = ch;
        return 1;
    }
    return 0;
}

int keyboard::getch()
{
char ch;

    if (peek_character != -1)
    {
        ch = peek_character;
        peek_character = -1;
    } else read(0,&ch,1);

    return ch;
}

////////////////////////////////////////////////////////////////