/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... use getch()/putch()/kbhit().
 */

#include <stdio.h>
#include "tty.h"

/*
 * The basic stream I/O functions are:
 *  fgetc() blocking (wait for character)
 *          echo stdin to stdout
 *          cooked (convert "\r\n" to '\n')
 *  fputc() cooked (convert '\n' to "\r\n")
 *
 * Special functions for stdin/stdout:
 *  getch() blocking (wait for character)
 *          no echo
 *          raw (no conversion)
 *  putch() raw (no conversion)
 *  kbhit() return "char available"
 */
int
main(void)
{
	char c;

	/*
	 * Using getch():
	 * Read raw char without echo from stdin.
	 */
	puts("getch()...");
	c = getch();

	/*
	 * Using putch():
	 * Raw write the char read by getch() to stdout.
	 */
	puts("putch():");
	putch(c);

	/*
	 * Using kbhit():
	 * Check if char available from stdin.
	 */
	puts("\nkbhit() loop...");
	while (1) {

		/*
		 * Show when a key is hit, then
		 * remove the char with getch().
		 */
		if (kbhit()) puts("KEY HIT!"), getch();
	}
}

