conio.cpp
Go to the documentation of this file.
1  /*****************************************************************************
2 
3 Copyright (c) 2005 SensAble Technologies, Inc. All rights reserved.
4 
5 OpenHaptics(TM) toolkit. The material embodied in this software and use of
6 this software is subject to the terms and conditions of the clickthrough
7 Development License Agreement.
8 
9 For questions, comments or bug reports, go to forums at:
10  http://dsc.sensable.com
11 
12  @file conio.c
13 
14  @brief Console functionality required by example code.
15 
16 ******************************************************************************/
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/poll.h>
22 #include <termios.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 
30 static struct termios term_attribs, term_attribs_old;
31 
32 static void restore_term(void)
33 {
34  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attribs_old) < 0)
35  {
36  perror("tcsetattr: ");
37  exit(-1);
38  }
39 }
40 
41 int _kbhit()
42 {
43  static int initialized;
44 
45  fd_set rfds;
46  struct timeval tv;
47  int retcode;
48 
49  if(!initialized)
50  {
51  if(tcgetattr(STDIN_FILENO, &term_attribs) < 0)
52  {
53  perror("tcgetattr: ");
54  exit(-1);
55  }
56 
57  term_attribs_old = term_attribs;
58 
59  if(atexit(restore_term))
60  {
61  perror("atexit: ");
62  exit(-1);
63  }
64 
65  term_attribs.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
66  term_attribs.c_iflag &= ~(IXON | BRKINT | INPCK | ICRNL | ISTRIP);
67  term_attribs.c_cflag &= ~(CSIZE | PARENB);
68  term_attribs.c_cflag |= CS8;
69  term_attribs.c_cc[VTIME] = 0;
70  term_attribs.c_cc[VMIN] = 0;
71 
72  if(tcsetattr(STDIN_FILENO, TCSANOW, &term_attribs) < 0)
73  {
74  perror("tcsetattr: ");
75  exit(-1);
76  }
77 
78  initialized = 1;
79  }
80 
81  FD_ZERO(&rfds);
82  FD_SET(STDIN_FILENO, &rfds);
83  memset(&tv, 0, sizeof(tv));
84 
85  retcode = select(1, &rfds, NULL, NULL, &tv);
86  if(retcode == -1 && errno == EINTR)
87  {
88  return 0;
89  }
90  else if(retcode < 0)
91  {
92  perror("select: ");
93  exit(-1);
94  }
95  else if(FD_ISSET(STDIN_FILENO, &rfds))
96  {
97  return retcode;
98  }
99 
100  return 0;
101 }
102 
103 int getch()
104 {
105  fd_set rfds;
106  int retcode;
107 
108  FD_ZERO(&rfds);
109  FD_SET(STDIN_FILENO, &rfds);
110 
111  retcode = select(1, &rfds, NULL, NULL, NULL);
112  if(retcode == -1 && errno == EINTR)
113  {
114  return 0;
115  }
116  else if(retcode < 0)
117  {
118  perror("select: ");
119  exit(-1);
120  }
121 
122  return getchar();
123 }
124 
int getch()
Definition: conio.cpp:103
static struct termios term_attribs term_attribs_old
Definition: conio.cpp:30
int _kbhit()
Definition: conio.cpp:41
static void restore_term(void)
Definition: conio.cpp:32


phantom_control
Author(s): Emilio Estrelinha
autogenerated on Mon Mar 2 2015 01:32:35