00001 /*************************************************************************** 00002 * getkey.cpp - getkey returns a keypress in non-blocking manner 00003 * 00004 * Created: Thu Jun 04 19:08:13 2009 (from RCSoftX) 00005 * Copyright 2009 Masrur Doostdar <doostdar@kbsg.rwth-aachen.de> 00006 * 00007 ****************************************************************************/ 00008 00009 /* This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU Library General Public License for more details. 00018 * 00019 * Read the full text in the LICENSE.GPL file in the doc directory. 00020 */ 00021 00022 #include <utils/system/getkey.h> 00023 00024 namespace fawkes { 00025 00026 /** Set non-blocking flag on STDIN. 00027 * Sets the 0_NONBLOCK Flag to 1, so that the read command in the 00028 * getkey()-method wont block the programm till a input is made (see also 00029 * libc manual, pages 105 and 117). 00030 */ 00031 static void 00032 set_nonblock_flag() 00033 { 00034 int oldflags; 00035 00036 oldflags = fcntl( STDIN_FILENO, F_GETFL, 0 ); 00037 oldflags |= O_NONBLOCK; 00038 fcntl( STDIN_FILENO, F_SETFL, oldflags ); 00039 } 00040 00041 00042 /** Clear non-blocking flag on STDIN. */ 00043 static void 00044 clear_nonblock_flag() 00045 { 00046 int oldflags; 00047 00048 oldflags = fcntl( STDIN_FILENO, F_GETFL, 0 ); 00049 oldflags &= ~O_NONBLOCK; 00050 fcntl( STDIN_FILENO, F_SETFL, oldflags ); 00051 } 00052 00053 00054 /** Get value of a single key-press non-blocking. 00055 * This method checks if a new keypress has happened and returns the value in 00056 * this case. Otherwise it returns 0. The method does not block. 00057 * @return key pressed or 0 00058 */ 00059 char 00060 getkey() 00061 { 00062 char buf[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 00063 struct termios tattr, // new terminal attributes 00064 saved_attributes; // restore the original settings of the terminal 00065 00066 set_nonblock_flag(); 00067 tcgetattr( STDIN_FILENO, &saved_attributes ); // save the original attributes 00068 00069 tcgetattr( STDIN_FILENO, &tattr ); // set the new attributes for the terminal: 00070 tattr.c_lflag &= ~(ICANON); // Clear ICANON 00071 tattr.c_lflag &= ~(ECHO); // and ECHO 00072 tattr.c_cc[VMIN] = 0; // noncanonical, direction transmission 00073 tattr.c_cc[VTIME]= 0; // of input characters (MIN=0,TIME=0) 00074 tcsetattr( STDIN_FILENO, TCSANOW, &tattr ); 00075 00076 read( STDIN_FILENO, buf, 1 ); 00077 00078 tcsetattr( STDIN_FILENO, TCSANOW, &saved_attributes ); 00079 clear_nonblock_flag(); 00080 00081 return buf[0]; 00082 } 00083 00084 } // end namespace fawkes 00085