Pārlūkot izejas kodu

cmKeyboard.h/c : Added cmIsKeyWaiting().

master
Kevin Larke 9 gadus atpakaļ
vecāks
revīzija
44cdad61e0
2 mainītis faili ar 63 papildinājumiem un 0 dzēšanām
  1. 34
    0
      cmKeyboard.c
  2. 29
    0
      cmKeyboard.h

+ 34
- 0
cmKeyboard.c Parādīt failu

@@ -233,3 +233,37 @@ void testKb2()
233 233
 
234 234
   reset_keypress();
235 235
 }
236
+
237
+
238
+// Based on: // From: http://www.flipcode.com/archives/_kbhit_for_Linux.shtml
239
+
240
+int cmIsKeyWaiting()
241
+{
242
+  static const int STDIN       = 0;
243
+  static bool      initialized = false;
244
+  struct timeval   timeout;
245
+  fd_set           rdset;
246
+
247
+  if (! initialized)
248
+  {
249
+    // Use termios to turn off line buffering
250
+    struct termios term;
251
+    tcgetattr(STDIN, &term);
252
+    term.c_lflag &= ~ICANON;
253
+    tcsetattr(STDIN, TCSANOW, &term);
254
+    setbuf(stdin, NULL);
255
+    initialized = true;
256
+  }
257
+
258
+
259
+  FD_ZERO(&rdset);
260
+  FD_SET(STDIN, &rdset);
261
+  timeout.tv_sec  = 0;
262
+  timeout.tv_usec = 0;
263
+
264
+  // time out immediately if STDIN is not ready.
265
+  return select(STDIN + 1, &rdset, NULL, NULL, &timeout);
266
+
267
+}
268
+
269
+

+ 29
- 0
cmKeyboard.h Parādīt failu

@@ -34,4 +34,33 @@ typedef struct
34 34
 // Set 'p' to NULL if the value of the key is not required.
35 35
 void cmKeyPress( cmKbRecd* p );
36 36
 
37
+
38
+// Return non-zero if a key is waiting to be read otherwise return 0.
39
+// Use getchar() to pick up the key.
40
+// 
41
+// Example:
42
+// while( 1 )
43
+// {
44
+//    if( cmIsKeyWaiting() == 0 )
45
+//       usleep(20000);
46
+//    else
47
+//    {
48
+//      char c = getchar();
49
+//      switch(c)
50
+//      {
51
+//        ....
52
+//      } 
53
+//    }
54
+//
55
+// }
56
+//
57
+// TODO: Note that this function turns off line-buffering on stdin.
58
+// It should be changed to a three function sequence.
59
+// bool org_state =  cmSetStdinLineBuffering(false);
60
+// ....
61
+// cmIsKeyWaiting()
62
+// ....
63
+// cmSetStdinLineBuffering(org_state)
64
+int cmIsKeyWaiting();
65
+
37 66
 #endif

Notiek ielāde…
Atcelt
Saglabāt