From 9b1b081cfdb1c0fb6457278775e0823f8bc10f62 Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Wed, 25 Apr 2018 18:07:30 -0400 Subject: Import Upstream version 2.0.0+dfsg --- src/lib/base/NonBlockingStream.cpp | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/lib/base/NonBlockingStream.cpp (limited to 'src/lib/base/NonBlockingStream.cpp') diff --git a/src/lib/base/NonBlockingStream.cpp b/src/lib/base/NonBlockingStream.cpp new file mode 100644 index 0000000..d44add1 --- /dev/null +++ b/src/lib/base/NonBlockingStream.cpp @@ -0,0 +1,60 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2008 Debauchee Open Source Group + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#if !defined(_WIN32) + +#include "base/NonBlockingStream.h" + +#include // tcgetattr/tcsetattr, read +#include // tcgetattr/tcsetattr +#include +#include +#include + +NonBlockingStream::NonBlockingStream(int fd) : + _fd(fd) +{ + // disable ICANON & ECHO so we don't have to wait for a newline + // before we get data (and to keep it from being echoed back out) + termios ta; + tcgetattr(fd, &ta); + _p_ta_previous = new termios(ta); + ta.c_lflag &= ~(ICANON | ECHO); + tcsetattr(fd, TCSANOW, &ta); + + // prevent IO from blocking so we can poll (read()) + int _cntl_previous = fcntl(fd, F_GETFL); + fcntl(fd, F_SETFL, _cntl_previous | O_NONBLOCK); +} + +NonBlockingStream::~NonBlockingStream() +{ + tcsetattr(_fd, TCSANOW, _p_ta_previous); + fcntl(_fd, F_SETFL, _cntl_previous); + delete _p_ta_previous; +} + +bool NonBlockingStream::try_read_char(char &ch) const +{ + int result = read(_fd, &ch, 1); + if (result == 1) + return true; + assert(result == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)); + return false; +} + +#endif // !defined(_WIN32) -- cgit v1.2.3