DOS to Linux migration library
Rob Wehrli
plug-devel@lists.PLUG.phoenix.az.us
Sat Mar 22 09:16:02 2003
Alan,
Have you seen the following?
Take Care.
Rob!
------------------------------------------------------------------------
--------
"_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;
}
> -----Original Message-----
> From: plug-devel-admin@lists.PLUG.phoenix.az.us
> [mailto:plug-devel-admin@lists.PLUG.phoenix.az.us]On Behalf Of Alan
> Dayley
> Sent: Friday, March 21, 2003 11:16 PM
> To: plug-devel@lists.PLUG.phoenix.az.us
> Subject: DOS to Linux migration library
>
>
> I have begun an adventure to migrate some DOS applications,
> written in C, to
> be native Linux applications. In an ideal world, I could
> just drop the code
> in gcc and have it come out executable on the other end. Of
> course this is
> not going to happen.
>
> A few facts:
> - Several of the apps work great under dosemu. This is a
> good start but if
> any continued development happens, I don't want to continue
> using "DOS world"
> compilers.
> - The applications are pretty vanilla. ie. Nothing really
> esoteric for memory
> needs or funky drivers.
> - They do use screen/cursor/keyboard control stuff. For
> those that remember
> the DOS world that would be calls to kbhit(), cursor positioning, etc.
>
> I know that for terminal applications, ncurses is the way to
> go for cursor
> stuff. I am studying that now. However, I wondered if
> anyone has built or
> seen a "library" or collection of functions that interfaces
> the DOS screen
> "API" calls into ncurses calls. I'd rather not build one if
> it is already
> out there. Any of you heard of such a thing? I am still
> looking around.
>
> Alan
>