Update lua versions
[ryzomcore.git] / nel / samples / net / chat / kbhit.cpp
blobfb56acb765a1a61948258198c300a86af68a6b91
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "nel/misc/types_nl.h"
22 #ifndef NL_OS_WINDOWS
23 #include "kbhit.h"
24 #include <termios.h>
25 #include <unistd.h> // for read()
26 #include <stdio.h>
28 static struct termios initial_settings, new_settings;
29 static int peek_character = -1;
31 void init_keyboard()
33 tcgetattr(STDIN_FILENO,&initial_settings);
34 new_settings = initial_settings;
35 new_settings.c_lflag &= ~ICANON;
36 new_settings.c_lflag &= ~ECHO;
37 new_settings.c_lflag &= ~ISIG;
38 new_settings.c_cc[VMIN] = 1;
39 new_settings.c_cc[VTIME] = 0;
40 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
43 void close_keyboard()
45 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
48 int kbhit()
50 unsigned char ch;
51 int nread;
53 if (peek_character != -1) return 1;
55 new_settings.c_cc[VMIN]=0;
56 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
57 nread = read(STDIN_FILENO,&ch,1);
58 new_settings.c_cc[VMIN]=1;
59 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
61 if(nread == 1)
63 peek_character = ch;
64 return 1;
66 return 0;
69 int getch()
71 char ch;
73 if(peek_character != -1)
75 ch = peek_character;
76 peek_character = -1;
77 return ch;
79 if (read(STDIN_FILENO,&ch,1) != 1) return ' ';
81 return ch;
84 #endif // __GNUC__