1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "cloud_print/gcp20/prototype/conio_posix.h" 6 7 #include <stdio.h> 8 #include <sys/time.h> 9 #include <sys/types.h> 10 #include <termios.h> 11 #include <unistd.h> 12 13 void SetTemporaryTermiosSettings(bool temporary) { 14 static termios oldt, newt; 15 16 if (temporary) { 17 tcgetattr(STDIN_FILENO, &oldt); 18 newt = oldt; 19 newt.c_lflag &= ~ICANON; // Disable buffered IO. 20 tcsetattr(STDIN_FILENO, TCSANOW, &newt); 21 } else { 22 tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // Restore default settings. 23 } 24 } 25 26 int _kbhit() { 27 SetTemporaryTermiosSettings(true); 28 29 timeval tv; 30 fd_set rdfs; 31 32 tv.tv_sec = 0; 33 tv.tv_usec = 0; 34 35 FD_ZERO(&rdfs); 36 FD_SET(STDIN_FILENO, &rdfs); 37 select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv); 38 SetTemporaryTermiosSettings(false); 39 40 return FD_ISSET(STDIN_FILENO, &rdfs); 41 } 42 43 int _getche() { 44 SetTemporaryTermiosSettings(true); 45 int c = getchar(); 46 SetTemporaryTermiosSettings(false); 47 return c; 48 } 49 50