SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Kbhit.h
Go to the documentation of this file.
1 /*****************************************************************************
2 _kbhit() and _getch() for Linux/UNIX
3 Chris Giese <geezer@execpc.com> http://my.execpc.com/~geezer
4 Release date: ?
5 This code is public domain (no copyright).
6 You can do whatever you want with it.
7 *****************************************************************************/
8 /*
9  * Modified work: Copyright (c) 2016, SLikeSoft UG (haftungsbeschränkt)
10  *
11  * This source code was modified by SLikeSoft. Modifications in this file are put under the public domain.
12  * Alternatively you are permitted to license the modifications under the MIT license, if you so desire. The
13  * license can be found in the license.txt file in the root directory of this source tree.
14  */
15 
16 #if defined(_WIN32)
17 #include <conio.h> /* _kbhit(), _getch() */
18 
19 #else
20 #include <sys/time.h> /* struct timeval, select() */
21 /* ICANON, ECHO, TCSANOW, struct termios */
22 #include <termios.h> /* tcgetattr(), tcsetattr() */
23 #include <stdlib.h> /* atexit(), exit() */
24 #include <unistd.h> /* read() */
25 #include <stdio.h> /* printf() */
26 #include <string.h> /* memcpy */
27 
28 static struct termios g_old_kbd_mode;
29 /*****************************************************************************
30 *****************************************************************************/
31 static void cooked(void)
32 {
33  tcsetattr(0, TCSANOW, &g_old_kbd_mode);
34 }
35 /*****************************************************************************
36 *****************************************************************************/
37 static void raw(void)
38 {
39  static char init;
40 
41  struct termios new_kbd_mode;
42 
43  if(init)
44  return;
45 /* put keyboard (stdin, actually) in raw, unbuffered mode */
46  tcgetattr(0, &g_old_kbd_mode);
47  memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
48  new_kbd_mode.c_lflag &= ~(ICANON /*| ECHO */ );
49  new_kbd_mode.c_cc[VTIME] = 0;
50  new_kbd_mode.c_cc[VMIN] = 1;
51  tcsetattr(0, TCSANOW, &new_kbd_mode);
52 /* when we exit, go back to normal, "cooked" mode */
53  atexit(cooked);
54 
55  init = 1;
56 }
57 /*****************************************************************************
58 *****************************************************************************/
59 static int _kbhit(void)
60 {
61  struct timeval timeout;
62  fd_set read_handles;
63  int status;
64 
65  raw();
66 /* check stdin (fd 0) for activity */
67  FD_ZERO(&read_handles);
68  FD_SET(0, &read_handles);
69  timeout.tv_sec = timeout.tv_usec = 0;
70  status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
71  if(status < 0)
72  {
73  printf("select() failed in _kbhit()\n");
74  exit(1);
75  }
76  return status;
77 }
78 /*****************************************************************************
79 *****************************************************************************/
80 static int _getch(void)
81 {
82  unsigned char temp;
83 
84  raw();
85 /* stdin = fd 0 */
86  if(read(0, &temp, 1) != 1)
87  return 0;
88  return temp;
89 }
90 #endif
91 
92