From 0be25a41ca793c3c03814f529890492adfac8ad5 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Thu, 9 Nov 2017 22:50:32 +0300 Subject: Resolve weird crash caused by Tetris inside ADLMIDI2 and other changes - Move most of inline classes methods into own CC file - Move Input into own header with own CC file to share it with the puzzle game - Created virtual destructors and resolved weak vtable trouble between of Tetris's classes - Remove static declarisons of Tetris class. Instead, let it be member of UserInterface - Fixed forgot note-offs while sorting events row with zero length notes - Fixed crash caused by unsafe access by reference to element of array that was modified/reallocated one or multiple times - Stabilize dealing with zero-length notes --- utils/adlmidi-2/input.cc | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 utils/adlmidi-2/input.cc (limited to 'utils/adlmidi-2/input.cc') diff --git a/utils/adlmidi-2/input.cc b/utils/adlmidi-2/input.cc new file mode 100644 index 0000000..9d69469 --- /dev/null +++ b/utils/adlmidi-2/input.cc @@ -0,0 +1,56 @@ +#include "input.hpp" + +xInput::xInput() +{ +#ifdef _WIN32 + inhandle = GetStdHandle(STD_INPUT_HANDLE); +#endif +#if (!defined(_WIN32) || defined(__CYGWIN__)) && !defined(__DJGPP__) + ioctl(0, TCGETA, &back); + struct termio term = back; + term.c_lflag &= ~(ICANON | ECHO); + term.c_cc[VMIN] = 0; // 0=no block, 1=do block + if(ioctl(0, TCSETA, &term) < 0) + fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); +#endif +} + +xInput::~xInput() +{ +#if (!defined(_WIN32) || defined(__CYGWIN__)) && !defined(__DJGPP__) + if(ioctl(0, TCSETA, &back) < 0) + fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~ O_NONBLOCK); +#endif +} + +char xInput::PeekInput() +{ +#ifdef __DJGPP__ + if(kbhit()) + { + int c = getch(); + return c ? c : getch(); + } +#endif +#ifdef _WIN32 + DWORD nread = 0; + INPUT_RECORD inbuf[1]; + while(PeekConsoleInput(inhandle, inbuf, sizeof(inbuf) / sizeof(*inbuf), &nread) && nread) + { + ReadConsoleInput(inhandle, inbuf, sizeof(inbuf) / sizeof(*inbuf), &nread); + if(inbuf[0].EventType == KEY_EVENT + && inbuf[0].Event.KeyEvent.bKeyDown) + { + char c = inbuf[0].Event.KeyEvent.uChar.AsciiChar; + unsigned s = inbuf[0].Event.KeyEvent.wVirtualScanCode; + if(c == 0) c = s; + return c; + } + } +#endif +#if (!defined(_WIN32) || defined(__CYGWIN__)) && !defined(__DJGPP__) + char c = 0; + if(read(0, &c, 1) == 1) return c; +#endif + return '\0'; +} -- cgit v1.2.3