/*
   <h3>Keyreader library functions</h3>

   <p>This library allows a program to read a single key at a time from
   the terminal. Unlike the normal line input mode, which allows the
   line being input to be edited before it is released to the program when
   Enter is pressed, each character is sent to the program as the key is
   pressed. The advantage is that single keystrokes can be input: the
   disadvantage is that any input editing and correction capabilities must
   be provided by the calling program.</p>
   

   <kbd>
   Copyright (C) 2009  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
   Contact Martin Gregorie at martin@gregorie.org
   </kbd>
*/

#include <termios.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

#include "keyreader.h"

extern int errno;

sigset_t       sig,
               savesig;
               
struct termios modes,
               savemodes;
/*
   Initialise the single key reading mode.

   Keyboard signals are blocked and the current terminal attributes are saved.
   Terminal attributes are changed to enable character at a time input without
   echoing keypresses.
*/
void krinit()
{
   sigaddset(&sig, SIGINT);
   sigaddset(&sig, SIGQUIT);
   sigaddset(&sig, SIGSTOP);
   sigprocmask(SIG_BLOCK, &sig, &savesig);

   /*
      Get current terminal attributes
   */
   if (tcgetattr(0, &modes) < 0)
   {
      logmsg("krinit: tcgetattr() returned %d - %s",
             errno,
             strerror(errno));
      exit(1);
   }

   /*
      Save a copy of them to restore later.
      Then change the attributes to set
      character at a time input and turn off echo.
   */
   savemodes = modes;
   modes.c_cc[VMIN] = 1;
   modes.c_cc[VTIME] = 0;
   modes.c_lflag &= ~ICANON;
   modes.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE | ISIG);

   /*
      Activate the changes
   */
   if (tcsetattr(0, TCSAFLUSH, &modes) < 0)
   {
      logmsg("krinit: tcsetattr() returned %d - %s",
             errno,
             strerror(errno));
      exit(1);
   }
}

/*
   Read an input character.
   -1 is returned on EOF (Ctrl-D pressed)
   -2 is returned if an error ocurred. errno is set.
*/
char krgetchar()
{
   char c;
   int  reply;
   
   reply = read(0, &c, 1);

   /*
      Since non-cannonical mode disables EOF we need to handle it ourselves
      and return -1. If nothing was read an error has ocurred, so return -2.
      errno is set.

   */
   if (reply == 0)
      c = -2;
   else
   if (c == savemodes.c_cc[VEOF])
      c = -1;

   return c;
}

/*
   Restore the original terminal attributes and keyboard signals.
*/
void krend()
{
   if (tcsetattr(0, TCSAFLUSH, &savemodes) < 0)
   {
      logmsg("krend: tcsetattr() returned %d - %s",
	     errno,
             strerror(errno));
      exit(1);
   }

   /*
      Restore the original signal mask
   */
   sigprocmask(SIG_SETMASK, &savesig, (sigset_t *)0);
}

