#!/usr/bin/python
#coding: utf8

# Copyright Seven Solutions S.L., 2012
# Author: Richard Carrillo <rcarrillo@sevensols.com>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
# Website: http://www.sevensols.com

import sys,select,fcntl,os

def kbhit():
   """ Return True if there is a key pending to be read """
   dr,dw,de = select.select([sys.stdin], [], [], 0.1)
#   print "dr:"+str(dr)+" dw:"+str(dr)+" de:"+str(de)
   return dr <> []

def getch():
   """ Read one char from stdin """
   return sys.stdin.read(1)

def purgestdin():
   """ Discard all key strokes """
   # make stdin a non-blocking file
   fd = sys.stdin.fileno()
   fl = fcntl.fcntl(fd, fcntl.F_GETFL)
   fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
   try:
      sys.stdin.read(4096) # purge stdin
   except:
      pass # ignore resource-unavailable exception
   # restore stdin attributes
   fcntl.fcntl(fd, fcntl.F_SETFL, fl)

