Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpKeyboard.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Keyboard management.
32  */
33 
39 #include <stdio.h>
40 
41 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
42 #include <sys/select.h>
43 #elif defined(_WIN32)
44 #include <conio.h>
45 #endif
46 #include <visp3/core/vpException.h>
47 #include <visp3/io/vpKeyboard.h>
48 
49 BEGIN_VISP_NAMESPACE
50 
55 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
56  : initial_settings(), new_settings()
57 {
58  init();
59 }
60 #else
61 { }
62 #endif
63 
68 {
69 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
70  end();
71 #endif
72 }
73 
78 {
79 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
80  int c;
81  c = ::getchar();
82  return c;
83 #elif defined(_WIN32) && !defined(WINRT)
84  return _getch();
85 #elif defined(_WIN32) && defined(WINRT)
86  throw(vpException(vpException::fatalError, "vpKeyboard::getchar() is not supported on UWP platform!"));
87 #endif
88 }
89 
94 {
95 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
96  struct timeval tv = { 0, 0 };
97  fd_set readfds;
98 
99  FD_ZERO(&readfds);
100  FD_SET(STDIN_FILENO, &readfds);
101 
102  return select(STDIN_FILENO + 1, &readfds, nullptr, nullptr, &tv) == 1;
103 #elif defined(_WIN32) && !defined(WINRT)
104  return _kbhit();
105 #elif defined(_WIN32) && defined(WINRT)
106  throw(vpException(vpException::fatalError, "vpKeyboard::kbhit() is not supported on UWP platform!"));
107 #endif
108 }
109 
110 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
115 void vpKeyboard::init()
116 {
117  setRawMode(true);
118 }
119 
124 void vpKeyboard::end()
125 {
126  setRawMode(false);
127 }
128 
136 void vpKeyboard::setRawMode(bool active)
137 {
138  if (active) {
139 
140  tcgetattr(STDIN_FILENO, &initial_settings);
141 
142  // new_settings = initial_settings;
143  // cfmakeraw(&new_settings);
144  new_settings = initial_settings;
145  new_settings.c_lflag &= (unsigned int)~ICANON;
146  new_settings.c_lflag &= (unsigned int)~ECHO;
147  new_settings.c_lflag &= (unsigned int)~ISIG;
148  // new_settings.c_oflag &= (unsigned int)~NL0;
149  // new_settings.c_oflag &= (unsigned int)~CR0;
150  new_settings.c_oflag &= (unsigned int)~TAB0;
151  // new_settings.c_oflag &= (unsigned int)~BS0;
152  new_settings.c_cc[VMIN] = 1;
153  new_settings.c_cc[VTIME] = 0;
154  tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
155 
156  }
157  else {
158  tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
159  }
160 }
161 #endif // defined UNIX
162 
163 END_VISP_NAMESPACE
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
void init(unsigned int h, unsigned int w, Type value)
Definition: vpImage.h:376
virtual ~vpKeyboard()
Definition: vpKeyboard.cpp:67
int getchar()
Definition: vpKeyboard.cpp:77
int kbhit()
Definition: vpKeyboard.cpp:93