support getch() returning EOF in client_test

This commit is contained in:
Arvid Norberg
2011-03-31 03:19:05 +00:00
parent 38caeb43d2
commit 91327f71da

View File

@@ -73,7 +73,7 @@ using boost::bind;
#include <windows.h> #include <windows.h>
#include <conio.h> #include <conio.h>
bool sleep_and_input(char* c, int sleep) bool sleep_and_input(int* c, int sleep)
{ {
for (int i = 0; i < sleep * 2; ++i) for (int i = 0; i < sleep * 2; ++i)
{ {
@@ -126,7 +126,7 @@ struct set_keypress
termios stored_settings; termios stored_settings;
}; };
bool sleep_and_input(char* c, int sleep) bool sleep_and_input(int* c, int sleep)
{ {
// sets the terminal to single-character mode // sets the terminal to single-character mode
// and resets when destructed // and resets when destructed
@@ -141,9 +141,7 @@ retry:
ret = select(1, &set, 0, 0, &tv); ret = select(1, &set, 0, 0, &tv);
if (ret > 0) if (ret > 0)
{ {
int r = getc(stdin); *c = getc(stdin);
if (r == EOF) return false;
*c = r;
return true; return true;
} }
if (errno == EINTR) if (errno == EINTR)
@@ -1283,23 +1281,27 @@ int main(int argc, char* argv[])
std::sort(handles.begin(), handles.end(), &compare_torrent); std::sort(handles.begin(), handles.end(), &compare_torrent);
if (loop_limit > 1) --loop_limit; if (loop_limit > 1) --loop_limit;
char c = 0; int c = 0;
while (sleep_and_input(&c, refresh_delay)) while (sleep_and_input(&c, refresh_delay))
{ {
if (c == EOF) { c = 'q'; break; }
if (c == 27) if (c == 27)
{ {
// escape code, read another character // escape code, read another character
#ifdef _WIN32 #ifdef _WIN32
c = _getch(); c = _getch();
#else #else
c = getc(stdin); int c = getc(stdin);
#endif #endif
if (c != '[') break; if (c == EOF) { c = 'q'; break; }
if (c != '[') continue;
#ifdef _WIN32 #ifdef _WIN32
c = _getch(); c = _getch();
#else #else
c = getc(stdin); c = getc(stdin);
#endif #endif
if (c == EOF) break;
if (c == 68) if (c == 68)
{ {
// arrow left // arrow left