ViSP  2.9.0
vpKeyboard.cpp
1 /****************************************************************************
2  *
3  * $Id: vpKeyboard.cpp 4632 2014-02-03 17:06:40Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Keybord management.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 
43 
44 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
45 # include <stdio.h>
46 # include <visp/vpKeyboard.h>
47 
57 vpKeyboard::vpKeyboard() : initial_settings(), new_settings()
58 {
59  init();
60 }
61 
65 vpKeyboard::~vpKeyboard()
66 {
67  end();
68 }
69 
74 int
75 vpKeyboard::getchar()
76 {
77  int c;
78  c = ::getchar();
79  return c;
80 }
81 
87 int
88 vpKeyboard::kbhit()
89 {
90  struct timeval tv = { 0, 0 };
91  fd_set readfds;
92 
93  FD_ZERO(&readfds);
94  FD_SET(STDIN_FILENO, &readfds);
95 
96  return select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv) == 1;
97 }
98 
103 void
104 vpKeyboard::init()
105 {
106  setRawMode(true);
107 }
108 
113 void
114 vpKeyboard::end()
115 {
116  setRawMode(false);
117 }
118 
126 void
127 vpKeyboard::setRawMode(bool active)
128 {
129  if (active) {
130 
131  tcgetattr(STDIN_FILENO, &initial_settings);
132 
133  new_settings = initial_settings;
134  // cfmakeraw(&new_settings);
135  new_settings = initial_settings;
136  new_settings.c_lflag &= (unsigned int)~ICANON;
137  new_settings.c_lflag &= (unsigned int)~ECHO;
138  new_settings.c_lflag &= (unsigned int)~ISIG;
139  //new_settings.c_oflag &= (unsigned int)~NL0;
140  //new_settings.c_oflag &= (unsigned int)~CR0;
141  new_settings.c_oflag &= (unsigned int)~TAB0;
142  //new_settings.c_oflag &= (unsigned int)~BS0;
143  new_settings.c_cc[VMIN] = 1;
144  new_settings.c_cc[VTIME] = 0;
145  tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
146 
147  }
148  else {
149  tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
150  }
151 }
152 
153 #endif // defined UNIX