conio.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #include <phua_haptic/conio.h>
33 
34 int _kbhit()
35 {
36  static int initialized;
37 
38  fd_set rfds;
39  struct timeval tv;
40  int retcode;
41 
42  if(!initialized)
43  {
44  if(tcgetattr(STDIN_FILENO, &term_attribs) < 0)
45  {
46  perror("tcgetattr: ");
47  exit(-1);
48  }
49 
50  term_attribs_old = term_attribs;
51 
52  if(atexit(restore_term))
53  {
54  perror("atexit: ");
55  exit(-1);
56  }
57 
58  term_attribs.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
59  term_attribs.c_iflag &= ~(IXON | BRKINT | INPCK | ICRNL | ISTRIP);
60  term_attribs.c_cflag &= ~(CSIZE | PARENB);
61  term_attribs.c_cflag |= CS8;
62  term_attribs.c_cc[VTIME] = 0;
63  term_attribs.c_cc[VMIN] = 0;
64 
65  if(tcsetattr(STDIN_FILENO, TCSANOW, &term_attribs) < 0)
66  {
67  perror("tcsetattr: ");
68  exit(-1);
69  }
70 
71  initialized = 1;
72  }
73 
74  FD_ZERO(&rfds);
75  FD_SET(STDIN_FILENO, &rfds);
76  memset(&tv, 0, sizeof(tv));
77 
78  retcode = select(1, &rfds, NULL, NULL, &tv);
79  if(retcode == -1 && errno == EINTR)
80  {
81  return 0;
82  }
83  else if(retcode < 0)
84  {
85  perror("select: ");
86  exit(-1);
87  }
88  else if(FD_ISSET(STDIN_FILENO, &rfds))
89  {
90  return retcode;
91  }
92 
93  return 0;
94 }
95 
96 int getch()
97 {
98  fd_set rfds;
99  int retcode;
100 
101  FD_ZERO(&rfds);
102  FD_SET(STDIN_FILENO, &rfds);
103 
104  retcode = select(1, &rfds, NULL, NULL, NULL);
105  if(retcode == -1 && errno == EINTR)
106  {
107  return 0;
108  }
109  else if(retcode < 0)
110  {
111  perror("select: ");
112  exit(-1);
113  }
114 
115  return getchar();
116 }
int getch()
gets character from keyboard input
Definition: conio.cpp:96
My version of conio.h include file that some portions of code provided by SensAble require...
int _kbhit()
waits for user input on keyboard
Definition: conio.cpp:34
static struct termios term_attribs term_attribs_old
Definition: conio.h:46
static void restore_term(void)
Definition: conio.h:48


phua_haptic
Author(s): Pedro Cruz
autogenerated on Mon Mar 2 2015 01:32:36