KeyBoard.C

Go to the documentation of this file.
00001 /*!@file Devices/KeyBoard.C a simple keyboard interface */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Naila Rizvi <nrizvi@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/KeyBoard.C $
00035 // $Id: KeyBoard.C 13695 2010-07-25 18:19:38Z dberg $
00036 //
00037 
00038 #include "Devices/KeyBoard.H"
00039 
00040 #include "Util/log.H"
00041 #include <fcntl.h>
00042 #include <stdio.h>
00043 #include <termios.h>
00044 #include <unistd.h>
00045 
00046 // ######################################################################
00047 KeyBoard::KeyBoard()
00048 {
00049   // switch keyboard to non-canonical mode:
00050   struct termios new_settings;
00051   if (tcgetattr(0, &stored_settings) == -1)
00052     PLERROR("Cannot tcgetattr");
00053   new_settings = stored_settings;
00054   new_settings.c_lflag &= (~ICANON);
00055   new_settings.c_cc[VTIME] = 0;
00056   new_settings.c_cc[VMIN] = 1;
00057   if (tcsetattr(0, TCSANOW, &new_settings) == -1)
00058     PLERROR("Cannot tcsetattr");
00059 
00060   // by default, assume keyboard is blocking:
00061   blocking = true;
00062 }
00063 
00064 // ######################################################################
00065 KeyBoard:: ~KeyBoard()//Destructor
00066 {
00067   // restore blocking mode:
00068   setBlocking(true);
00069 
00070   // restore previous keyboard attributes:
00071   if (tcsetattr(0, TCSANOW, &stored_settings) == -1)
00072     PLERROR("Cannot tcsetattr");
00073 }
00074 
00075 // ######################################################################
00076 void KeyBoard::setBlocking(const bool block)
00077 {
00078   int flags = fcntl(0, F_GETFL, 0);
00079   if (flags == -1) PLERROR("Cannot get flags");
00080   if (block) flags &= (~O_NONBLOCK); else flags |= O_NONBLOCK;
00081   if (fcntl(0, F_SETFL, flags) == -1) PLERROR("Cannot set flags");
00082   else blocking = block; // remember setting
00083 }
00084 
00085 // ######################################################################
00086 KeyBoardKey KeyBoard::getKey(const bool block)
00087 {
00088   // make sure we are in the correct blocking/non-blocking mode:
00089   if (blocking != block) setBlocking(block);
00090   
00091   // get the key & return:
00092   int ch = getc(stdin);
00093   if (ch == EOF) return KBD_NONE;
00094   return fromChar(getc(stdin));
00095 }
00096 
00097 // ######################################################################
00098 int KeyBoard::getKeyAsChar(const bool block)
00099 {
00100   // make sure we are in the correct blocking/non-blocking mode:
00101   if (blocking != block) setBlocking(block);
00102   
00103   // get the key & return:
00104   int ch = getc(stdin);
00105   return ch;
00106 }
00107 
00108 // ######################################################################
00109 KeyBoardKey KeyBoard::fromChar(const char c) const
00110 {
00111   switch(c)
00112     {
00113     case 'a': return KBD_KEY1;
00114     case 'b': return KBD_KEY2;
00115     case 'c': return KBD_KEY3;
00116     case 'd': return KBD_KEY4;
00117     case 'e': return KBD_KEY5;
00118     default: return KBD_OTHER;
00119     }
00120 }
00121 
00122 // ######################################################################
00123 /* So things look consistent in everyone's emacs... */
00124 /* Local Variables: */
00125 /* indent-tabs-mode: nil */
00126 /* End: */
Generated on Sun May 8 08:40:37 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3