#ifndef CONIO_H
#define CONIO_H


#ifdef WIN32

///////////
//Windows//
///////////
#include <conio.h>


#elif defined __linux__

/////////
//Linux//
/////////
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/poll.h>


// kbhit() function

inline int kbhit()
{
  struct pollfd ufds = { STDIN_FILENO, POLLIN, 0 };
  return poll(&ufds, 1, 0);
}


// getch() function

inline int getch()
{
  // Initialisation
  struct termios oldsettings, newsettings;
  char character;
  int error;
  fflush(stdout);
  tcgetattr(STDIN_FILENO, &oldsettings);
  newsettings = oldsettings;
  newsettings.c_lflag &= (~ICANON);
  newsettings.c_lflag &= (~ECHO);
  newsettings.c_cc[VTIME] = 0;
  newsettings.c_cc[VMIN] = 1;

  error = tcsetattr(STDIN_FILENO, TCSANOW, &newsettings);
  if (error == 0)
    {
      error  = read(STDIN_FILENO, &character, 1);
      error += tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldsettings);
    }

  return ( (error == 1) ? character : -1 );
}


#else

////////////
//Other OS//
////////////
#error ERROR: OS not supported


#endif

#endif // CONIO_H
