Vita
compatibility_patch.h
Go to the documentation of this file.
1
13#if !defined(VITA_COMPATIBILITY_PATCH_H)
14#define VITA_COMPATIBILITY_PATCH_H
15
16#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
17// The `windows.h` header file (or more correctly, `windef.h` that it includes
18// in turn) has macros for `min` and `max` that are interfering with
19// `std::min`/`max` and `std::numeric_limits<T>::min`/`max`.
20// The `NOMINMAX` macro suppresses the `min` and `max` definitions in
21// `Windef.h`. The `undef` limits potential side effects.
22# define NOMINMAX
23# include <conio.h>
24# include <windows.h>
25# undef NOMINMAX
26#else
27# include <iostream>
28# include <termios.h>
29# include <unistd.h>
30#endif
31
32namespace vita
33{
34
35#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
47inline void term_raw_mode(bool enter)
48{
49 static termios oldt, newt;
50
51 if (enter)
52 {
53 tcgetattr(STDIN_FILENO, &oldt);
54 newt = oldt;
55 newt.c_lflag &= ~static_cast<unsigned>(ICANON | ECHO);
56 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
57 }
58 else
59 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
60}
61
65inline bool kbhit()
66{
67 // Do not wait at all, not even a microsecond.
68 timeval tv;
69 tv.tv_sec = 0;
70 tv.tv_usec = 0;
71
72 fd_set readfd;
73 FD_ZERO(&readfd); // initialize `readfd`
74 FD_SET(STDIN_FILENO, &readfd);
75
76 // The first parameter is the number of the largest file descriptor to
77 // check + 1.
78 if (select(STDIN_FILENO + 1, &readfd, nullptr, nullptr, &tv) == -1)
79 return false; // an error occurred
80 // `read_fd` now holds a bit map of files that are readable. We test the
81 // entry for the standard input (file 0).
82 return FD_ISSET(STDIN_FILENO, &readfd);
83}
84
85inline bool keypressed(int k) { return kbhit() && std::cin.get() == k; }
86
87#else
88
89inline void term_raw_mode(bool) {}
90inline bool keypressed(int k) { return _kbhit() && _getch() == k; }
91#endif
92
93} // namespace vita
94
95#endif // include guard
The main namespace for the project.
bool kbhit()
void term_raw_mode(bool enter)