Skip to content

Commit

Permalink
Graphics device text/character mode. Linux port.
Browse files Browse the repository at this point in the history
Graphics device text/character mode. Linux port. Documentation updates.
  • Loading branch information
makarcz committed Sep 15, 2016
1 parent 9d6706d commit f4526b7
Show file tree
Hide file tree
Showing 17 changed files with 19,484 additions and 8,926 deletions.
202 changes: 196 additions & 6 deletions ConsoleIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*
* Purpose: Implementation of ConsoleIO class.
* The ConsoleIO class has methods helpful when
* UI is utilizing STDIO (DOS) console.
* UI is utilizing STDIO (DOS) console OR curses on
* Linux.
*
* Date: 8/26/2016
*
Expand Down Expand Up @@ -44,6 +45,15 @@
#include <conio.h>
#endif

#if defined(LINUX)
#include <termios.h>
#include <sys/ioctl.h>
#include <asm-generic/ioctls.h>
#include <ncurses.h>

static WINDOW *g_pWin = NULL;
#endif

using namespace std;

namespace MKBasic {
Expand Down Expand Up @@ -141,7 +151,7 @@ void ConsoleIO::ScrHome()
SetConsoleCursorPosition( hStdOut, homeCoords );
}

#endif
#endif // WINDOWS

#if defined(LINUX)

Expand All @@ -155,7 +165,12 @@ void ConsoleIO::ScrHome()
*/
void ConsoleIO::ClearScreen()
{
system("clear");
if (isendwin() || NULL == g_pWin) {
system("clear");
} else {
clear();
refresh();
}
}

/*
Expand All @@ -168,9 +183,184 @@ void ConsoleIO::ClearScreen()
*/
void ConsoleIO::ScrHome()
{
cout << "\033[1;1H";
if (!isendwin() && NULL != g_pWin) {
move(0, 0);
refresh();
} else {
cout << "\033[1;1H";
}
}

#endif // #define LINUX

/*
*--------------------------------------------------------------------
* Method: GetChar()
* Purpose: Get character from console.
* Arguments: n/a
* Returns: int - character code.
*--------------------------------------------------------------------
*/
int ConsoleIO::GetChar()
{
#if defined(LINUX)
if (!isendwin() && NULL != g_pWin)
return getch();
else
return getchar();
#else
return getch();
#endif
}
/*
*--------------------------------------------------------------------
* Method: KbHit()
* Purpose: Check if key has been pressed.
* Arguments: n/a
* Returns: bool - true if key pressed
*--------------------------------------------------------------------
*/
bool ConsoleIO::KbHit()
{
#if defined(LINUX)
if (!isendwin() && NULL != g_pWin) {
int ch = getch();

if (ch != ERR) {
ungetch(ch);
return true;
} else {
return false;
}
} else {
static const int STDIN = 0;
static bool initialized = false;

if (! initialized) {
// Use termios to turn off line buffering
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}

int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return (bytesWaiting > 0);

}
#else
return (kbhit() != 0);
#endif
}


/*
*--------------------------------------------------------------------
* Method: InitCursesScr()
* Purpose: Initialize nsurses screen.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void ConsoleIO::InitCursesScr()
{
fflush(stdin);
#if defined(LINUX)
if (NULL == g_pWin) {
g_pWin = initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
} else if (isendwin()) {
refresh();
}
#endif
}

/*
*--------------------------------------------------------------------
* Method: closeCursesScr()
* Purpose: Close nsurses screen.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void ConsoleIO::CloseCursesScr()
{
#if defined(LINUX)
if (!isendwin() && NULL != g_pWin) {
endwin();
}
#endif
}

/*
*--------------------------------------------------------------------
* Method: PrintChar()
* Purpose: Print character on the screen.
* Arguments: char - character.
* Returns: n/a
*--------------------------------------------------------------------
*/
void ConsoleIO::PrintChar(char c)
{
#if defined(LINUX)
if (!isendwin() && NULL != g_pWin) {
echochar(c);
} else {
cout << c << flush;
}
#else
cout << c;
#endif
}

/*
*--------------------------------------------------------------------
* Method: PrintString()
* Purpose: Print string on the screen.
* Arguments: char * - pointer to char array.
* Returns: n/a
*--------------------------------------------------------------------
*/
void ConsoleIO::PrintString(string s)
{
#if defined(LINUX)
if (!isendwin() && NULL != g_pWin) {
addstr(s.c_str());
refresh();
} else {
cout << s;
}
#else
cout << s;
#endif
}

#endif // #devine LINUX
/*
*--------------------------------------------------------------------
* Method: Beep()
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void ConsoleIO::Beep()
{
#if defined(LINUX)
if (!isendwin() && NULL != g_pWin) {
beep();
} else {
cout << "\a";
}
#else
cout << "\a";
#endif
}

} // END namespace MKBasic
} // END namespace MKBasic
20 changes: 15 additions & 5 deletions ConsoleIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,37 @@
#include <string>
#include <queue>
#include <chrono>
#include <string.h>
#include "system.h"

//#define WINDOWS 1
#if defined (WINDOWS)
#include <windows.h>
#endif

using namespace std;

namespace MKBasic {

class ConsoleIO
{
public:

ConsoleIO();
~ConsoleIO();
ConsoleIO();
~ConsoleIO();

void ClearScreen();
void ScrHome();
void ClearScreen();
void ScrHome();
void InitCursesScr();
void CloseCursesScr();
void PrintChar(char c);
void PrintString(string s);
bool KbHit();
int GetChar();
void Beep();

};

} // namespace MKBasic

#endif // #ifndef CONSOLEIO_H
#endif // #ifndef CONSOLEIO_H
18 changes: 15 additions & 3 deletions Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@
*--------------------------------------------------------------------
*/
#include "Display.h"
#include <ctype.h>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include "Display.h"
#include "MKGenException.h"

using namespace std;


#if defined(LINUX)
#include <ncurses.h>

extern bool g_initialized;

#endif

/*
*--------------------------------------------------------------------
* Method:
Expand Down Expand Up @@ -88,6 +97,9 @@ Display::~Display()
*/
void Display::InitScr()
{
mpConIO = new ConsoleIO();
if (NULL == mpConIO)
throw MKGenException("Display::InitScr() : Out of memory - ConsoleIO");
mLastChar = 0;
mScrLines = SCREENDIM_ROW;
mScrColumns = SCREENDIM_COL;
Expand Down Expand Up @@ -248,7 +260,7 @@ void Display::PutChar(char c)
mCursorCoord.col = 0;
} else if (c == SCREENSPECCHARS_TB) {
mLastChar = SCREENSPECCHARS_TB;
mCursorCoord.col += TABSIZE;
mCursorCoord.col += DISP_TABSIZE;
if (mCursorCoord.col >= mScrColumns) {
mCursorCoord.col = mScrColumns-1; // must work on it some more
}
Expand Down Expand Up @@ -339,7 +351,7 @@ void Display::ShowScr()
if (mShellConsoleWidth > mScrColumns) line = line + "\n";
scr = scr + line;
}
cout << scr;
mpConIO->PrintString(scr);
}

/*
Expand Down
4 changes: 3 additions & 1 deletion Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
#define DISPLAY_H

#include "system.h"
#include "ConsoleIO.h"

#define TABSIZE 4
#define DISP_TABSIZE 4

namespace MKBasic {

Expand Down Expand Up @@ -81,6 +82,7 @@ class Display
unsigned int mShellConsoleWidth;
unsigned int mScrLines;
unsigned int mScrColumns;
ConsoleIO *mpConIO;

void InitScr();
void ScrollUp();
Expand Down
Loading

0 comments on commit f4526b7

Please sign in to comment.