Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
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 
34 #include <stdio.h>
35 
36 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
37 #include <sys/select.h>
38 #elif defined(_WIN32)
39 #include <conio.h>
40 #endif
41 #include <visp3/core/vpException.h>
42 #include <visp3/io/vpKeyboard.h>
43 
53 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
54  : initial_settings(), new_settings()
55 {
56  init();
57 }
58 #else
59 {}
60 #endif
61 
62 
63 
68 {
69 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
70  end();
71 #endif
72 }
73 
79 {
80 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
81  int c;
82  c = ::getchar();
83  return c;
84 #elif defined(_WIN32) && !defined(WINRT)
85  return _getch();
86 #elif defined(_WIN32) && defined(WINRT)
87  throw(vpException(vpException::fatalError, "vpKeyboard::getchar() is not supported on UWP platform!"));
88 #endif
89 }
90 
97 {
98 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
99  struct timeval tv = { 0, 0 };
100  fd_set readfds;
101 
102  FD_ZERO(&readfds);
103  FD_SET(STDIN_FILENO, &readfds);
104 
105  return select(STDIN_FILENO + 1, &readfds, nullptr, nullptr, &tv) == 1;
106 #elif defined(_WIN32) && !defined(WINRT)
107  return _kbhit();
108 #elif defined(_WIN32) && defined(WINRT)
109  throw(vpException(vpException::fatalError, "vpKeyboard::kbhit() is not supported on UWP platform!"));
110 #endif
111 }
112 
113 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
118 void vpKeyboard::init()
119 {
120  setRawMode(true);
121 }
122 
127 void vpKeyboard::end()
128 {
129  setRawMode(false);
130 }
131 
139 void vpKeyboard::setRawMode(bool active)
140 {
141  if (active) {
142 
143  tcgetattr(STDIN_FILENO, &initial_settings);
144 
145  // new_settings = initial_settings;
146  // cfmakeraw(&new_settings);
147  new_settings = initial_settings;
148  new_settings.c_lflag &= (unsigned int)~ICANON;
149  new_settings.c_lflag &= (unsigned int)~ECHO;
150  new_settings.c_lflag &= (unsigned int)~ISIG;
151  // new_settings.c_oflag &= (unsigned int)~NL0;
152  // new_settings.c_oflag &= (unsigned int)~CR0;
153  new_settings.c_oflag &= (unsigned int)~TAB0;
154  // new_settings.c_oflag &= (unsigned int)~BS0;
155  new_settings.c_cc[VMIN] = 1;
156  new_settings.c_cc[VTIME] = 0;
157  tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
158 
159  }
160  else {
161  tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
162  }
163 }
164 
165 #endif // defined UNIX
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
void init(unsigned int h, unsigned int w, Type value)
Definition: vpImage.h:619
virtual ~vpKeyboard()
Definition: vpKeyboard.cpp:67
int getchar()
Definition: vpKeyboard.cpp:78
int kbhit()
Definition: vpKeyboard.cpp:96