1 | #ifndef _3DV_CONIO_H
|
---|
2 | #define _3DV_CONIO_H
|
---|
3 |
|
---|
4 | /* 3dv-client/3dv_conio.h
|
---|
5 | *
|
---|
6 | * Copyright (C) 2013 VisLab
|
---|
7 | *
|
---|
8 | * This file is part of 3dv-client; you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU Lesser General Public License as published by
|
---|
10 | * the Free Software Foundation; either version 3 of the License, or (at
|
---|
11 | * your option) any later version.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful, but
|
---|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Lesser General Public License
|
---|
19 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <string.h>
|
---|
25 | #include <unistd.h>
|
---|
26 | #include <sys/select.h>
|
---|
27 | #include <termios.h>
|
---|
28 |
|
---|
29 | struct termios orig_termios;
|
---|
30 |
|
---|
31 | void reset_terminal_mode()
|
---|
32 | {
|
---|
33 | tcsetattr(0, TCSANOW, &orig_termios);
|
---|
34 | }
|
---|
35 |
|
---|
36 | void set_conio_terminal_mode()
|
---|
37 | {
|
---|
38 | struct termios new_termios;
|
---|
39 |
|
---|
40 | /* take two copies - one for now, one for later */
|
---|
41 | tcgetattr(0, &orig_termios);
|
---|
42 | memcpy(&new_termios, &orig_termios, sizeof(new_termios));
|
---|
43 |
|
---|
44 | /* register cleanup handler, and set the new terminal mode */
|
---|
45 | atexit(reset_terminal_mode);
|
---|
46 | cfmakeraw(&new_termios);
|
---|
47 | tcsetattr(0, TCSANOW, &new_termios);
|
---|
48 | }
|
---|
49 |
|
---|
50 | int kbhit()
|
---|
51 | {
|
---|
52 | struct timeval tv = { 0L, 0L };
|
---|
53 | fd_set fds;
|
---|
54 | FD_ZERO(&fds);
|
---|
55 | FD_SET(0, &fds);
|
---|
56 | return select(1, &fds, NULL, NULL, &tv);
|
---|
57 | }
|
---|
58 |
|
---|
59 | int getch()
|
---|
60 | {
|
---|
61 | int r;
|
---|
62 | unsigned char c;
|
---|
63 | if ((r = read(0, &c, sizeof(c))) < 0) {
|
---|
64 | return r;
|
---|
65 | } else {
|
---|
66 | return c;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | ///*main example*/
|
---|
71 | // int main(int argc, char *argv[])
|
---|
72 | // {
|
---|
73 | // set_conio_terminal_mode();
|
---|
74 | //
|
---|
75 | // while (!kbhit()) {
|
---|
76 | // /* do some work */
|
---|
77 | // }
|
---|
78 | // (void)getch(); /* consume the character */
|
---|
79 | // }
|
---|
80 | #endif |
---|