/*
 * Title: kbhit.c
 * Author: Jed Marti KI7NNP
 * Description: Linux version of Microsoft's  kbhit. From stackoverflow. Get's used to dump more
 *   of current state.
 * Revision History: (Created Thu Feb  9 14:59:59 2023)
 *   Mon Dec 18 05:44:47 2023 - JBM return character if present.
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <stdint.h>


uint8_t kbhit(void)
/*     Returns 1 if there's been a keyboard hit and 0 if not. The caller must read the character
 *   or it will site in the buffer.
 * Parameters: none
 * External References: none
 * Returned values: character if a character, 0 if not.
 * Errors detected: none
 */
{
  struct termios term;
  int32_t byteswaiting;

  tcgetattr(0, &term);

  struct termios term2 = term;
  term2.c_lflag &= ~ICANON;
  tcsetattr(0, TCSANOW, &term2);

  ioctl(0, FIONREAD, &byteswaiting);

  tcsetattr(0, TCSANOW, &term);
  if (byteswaiting > 0) return getchar();
  else return 0;
  //  return byteswaiting > 0;
}


// EOF
