appkit  1.5.1
RoadNarrows Robotics Application Kit
StateKb.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Library: librnr_appkit
6 //
7 // File: StateKb.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2013-05-06 10:03:14 -0600 (Mon, 06 May 2013) $
12  * $Rev: 2907 $
13  *
14  * \brief Keyboard StateKb derived state class implementation.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  *
19  * \par Copyright
20  * \h_copy 2012-2017. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  *
27  * Permission is hereby granted, without written agreement and without
28  * license or royalty fees, to use, copy, modify, and distribute this
29  * software and its documentation for any purpose, provided that
30  * (1) The above copyright notice and the following two paragraphs
31  * appear in all copies of the source code and (2) redistributions
32  * including binaries reproduces these notices in the supporting
33  * documentation. Substantial modifications to this software may be
34  * copyrighted by their authors and need not follow the licensing terms
35  * described here, provided that the new terms are clearly indicated in
36  * all files where they apply.
37  *
38  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
39  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
40  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
41  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
42  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
43  * THE POSSIBILITY OF SUCH DAMAGE.
44  *
45  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
46  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
47  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
48  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
49  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
50  *
51  * @EulaEnd@
52  */
53 ////////////////////////////////////////////////////////////////////////////////
54 
55 #include <sys/select.h>
56 #include <stdio.h>
57 #include <unistd.h>
58 #include <stdarg.h>
59 #include <libgen.h>
60 #include <fcntl.h>
61 #include <termios.h>
62 
63 #include <sstream>
64 #include <string>
65 #include <map>
66 
67 #include "rnr/rnrconfig.h"
68 #include "rnr/log.h"
69 
70 #include "rnr/appkit/Random.h"
71 #include "rnr/appkit/State.h"
72 #include "rnr/appkit/StateKb.h"
73 
74 using namespace std;
75 using namespace rnr;
76 
77 
78 //------------------------------------------------------------------------------
79 // Private
80 //------------------------------------------------------------------------------
81 
82 
83 PRAGMA_IGNORED(sign-conversion)
84 /*!
85  * \brief FD_SET() wrapper with no annoying warnings.
86  * \param fd File descriptor to add to set.
87  * \param pset Pointer to fd set.
88  */
89 static inline void fdset_nowarn(int fd, fd_set *pset)
90 {
91  FD_SET(fd, pset);
92 }
93 PRAGMA_WARNING(sign-conversion)
94 
95 
96 //------------------------------------------------------------------------------
97 // StateKb Class
98 //------------------------------------------------------------------------------
99 
100 int StateKb::ClassObjRefCnt = 0;
101 int StateKb::OrigInputStatusFlags = 0;
102 struct termios StateKb::OrigInputTio = {0, };
103 
104 int StateKb::receiveEvent()
105 {
106  byte_t byte;
107  fd_set rset;
108  struct timeval timeout;
109  int fd = fileno(stdin);
110  int nFd;
111  ssize_t n;
112 
113  FD_ZERO(&rset);
114  fdset_nowarn(fd, &rset);
115 
116  // wait for character with timeout
117  if( m_usecTimeOut > 0 )
118  {
119  // load timeout
120  timeout.tv_sec = (time_t)(m_usecTimeOut / 1000000);
121  timeout.tv_usec = (time_t)(m_usecTimeOut % 1000000);
122 
123  nFd = select(fd+1, &rset, NULL, NULL, &timeout);
124  }
125 
126  // block indefinitely for character
127  else
128  {
129  nFd = select(fd+1, &rset, NULL, NULL, NULL);
130  }
131 
132  // system error occurred
133  if( nFd < 0 )
134  {
135  LOGSYSERROR("select(%d,...)", fd);
136  return KbEventError;
137  }
138 
139  // select() timeout occurred
140  else if( nFd == 0 )
141  {
142  LOGDIAG4("select() on read timed out");
143  errno = ETIMEDOUT;
144  return KbEventTimeOut;
145  }
146 
147  // data available from serial device, but error on read
148  else if( (n = read(fd, &byte, (size_t)1)) < 0 )
149  {
150  LOGSYSERROR("read(%d,...)", fd);
151  return KbEventError;
152  }
153 
154  // nothing read
155  else if( n == 0 )
156  {
157  LOGERROR("0=read(%d,...)", fd);
158  errno = EIO;
159  return KbEventError;
160  }
161 
162  // read character
163  else
164  {
165  LOGDIAG4("%s() byte=0x%02x read", LOGFUNCNAME, byte);
166  return (int)(((uint_t)(byte)) & 0x00ff);
167  }
168 }
169 
170 void StateKb::configInput()
171 {
172  struct termios tio;
173  int fd = fileno(stdin);
174 
175  // get the terminal settings for stdin
176  tcgetattr(fd, &OrigInputTio);
177 
178  // copy settings
179  tio = OrigInputTio;
180 
181  // disable canonical mode (buffered i/o) and local echo
182  tio.c_lflag &= (uint_t)(~ICANON & ~ECHO);
183 
184  // set the new settings immediately
185  tcsetattr(fd, TCSANOW, &tio);
186 
187  // get file status flags
188  OrigInputStatusFlags = fcntl(fd, F_GETFL, 0);
189 
190  // set non-blocking mode
191  fcntl(fd, F_SETFL, OrigInputStatusFlags | O_NONBLOCK);
192 }
193 
194 void StateKb::restoreInput()
195 {
196  int fd = fileno(stdin);
197 
198  tcsetattr(fd, TCSANOW, &OrigInputTio);
199 
200  fcntl(fd, F_SETFL, OrigInputStatusFlags);
201 }
static void fdset_nowarn(int fd, fd_set *pset)
FD_SET() wrapper with no annoying warnings.
Definition: StateKb.cxx:89
Keyboard StateKb derived state class interface.
Random variable generator class interface.
State base class interface.
RoadNarrows Robotics.
Definition: Camera.h:74