added terminal restore to rs-nogui on linux. Enabled compile of webui in rs-nogui.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8214 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
electron128 2015-05-03 14:46:10 +00:00
parent 444730d19c
commit 7b5bfc99d3
3 changed files with 79 additions and 65 deletions

View file

@ -5,44 +5,29 @@
#include <api/JsonStream.h> #include <api/JsonStream.h>
// need two functions for non blocking read from stdin: // windows has _kbhit() and _getch() fr non-blocking keaboard read.
// int _kbhit() (returns a non zero value if a key was pressed) // linux does not have these functions.
// int _getch() (return the pressed key) // the TerminalInput class provides both for win and linux
// these function are available on windows in conio.h // only use a single instance of this class in the whole program!
// they are not available on linux // or destroy the classes in the inverse order how the where created
// else terminal echo will not be restored
// the point of this class is:
// - it configures the terminal in the constructor
// - it restores the terminal in the destructor
#ifdef _WIN32 #ifdef _WIN32
#include <conio.h> #include <conio.h>
#else // LINUX #else // LINUX
/**
Linux (POSIX) implementation of _kbhit().
Morgan McGuire, morgan@cs.brown.edu
(modified to disable echo)
*/
#include <stdio.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <termios.h> #include <termios.h>
#include <stropts.h> #include <stdio.h>
#include <sys/ioctl.h>
int _kbhit() { #endif
static const int STDIN = 0; #define TERMINALINPUT_DEBUG
static bool initialized = false; class TerminalInput
{
if (! initialized) { public:
// Use termios to turn off line buffering TerminalInput()
termios term; {
tcgetattr(STDIN, &term); #ifndef _WIN32
term.c_lflag &= ~ICANON;
term.c_lflag &= ~ECHO; // disable echo
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
/* /*
Q: Is there a getch() (from conio) equivalent on Linux/UNIX? Q: Is there a getch() (from conio) equivalent on Linux/UNIX?
@ -53,24 +38,50 @@ This code sets the terminal into non-canonical mode, thus disabling line bufferi
http://cboard.cprogramming.com/faq-board/27714-faq-there-getch-conio-equivalent-linux-unix.html http://cboard.cprogramming.com/faq-board/27714-faq-there-getch-conio-equivalent-linux-unix.html
*/ */
#include <stdio.h> tcgetattr(STDIN_FILENO, &mOldTermSettings);
#include <termios.h> termios term = mOldTermSettings;
#include <unistd.h> term.c_lflag &= ~ICANON;
term.c_lflag &= ~ECHO; // disable echo
int _getch( ) { tcsetattr(STDIN_FILENO, TCSANOW, &term);
struct termios oldt, setbuf(stdin, NULL);
newt; #endif
int ch; }
tcgetattr( STDIN_FILENO, &oldt ); ~TerminalInput()
newt = oldt; {
newt.c_lflag &= ~( ICANON | ECHO ); #ifndef _WIN32
tcsetattr( STDIN_FILENO, TCSANOW, &newt ); // restore terminal settings
ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &mOldTermSettings);
tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); #ifdef TERMINALINPUT_DEBUG
return ch; std::cerr << "Terminal restored" << std::endl;
#endif
#endif
} }
#endif // LINUX
// returns a non zero value if a key was pressed
int kbhit()
{
#ifdef _WIN32
return _kbhit();
#else // LINUX
int bytesWaiting;
ioctl(STDIN_FILENO, FIONREAD, &bytesWaiting);
return bytesWaiting;
#endif
}
// return the pressed key
int getch()
{
#ifdef _WIN32
return _getch();
#else // LINUX
return getchar();
#endif
}
private:
#ifndef _WIN32
struct termios mOldTermSettings;
#endif
};
namespace resource_api { namespace resource_api {
@ -108,6 +119,8 @@ void TerminalApiClient::run()
bool ask_for_password = false; bool ask_for_password = false;
std::string key_name; std::string key_name;
TerminalInput term;
while(isRunning()) while(isRunning())
{ {
// assuming sleep_time >> work_time // assuming sleep_time >> work_time
@ -120,10 +133,10 @@ void TerminalApiClient::run()
{ {
last_io_poll = 0; last_io_poll = 0;
last_char = 0; last_char = 0;
if(_kbhit()) if(term.kbhit())
{ {
enter_was_pressed = false; enter_was_pressed = false;
last_char = _getch(); last_char = term.getch();
if(last_char > 127) if(last_char > 127)
std::cout << "Warning: non ASCII characters probably won't work." << std::endl; std::cout << "Warning: non ASCII characters probably won't work." << std::endl;
if(last_char >= ' ')// space is the first printable ascii character if(last_char >= ' ')// space is the first printable ascii character

View file

@ -4,7 +4,7 @@ CONFIG += bitdht
#CONFIG += introserver #CONFIG += introserver
#CONFIG += sshserver #CONFIG += sshserver
# webinterface, requires libmicrohttpd # webinterface, requires libmicrohttpd
#CONFIG += webui CONFIG += webui
CONFIG -= qt xml gui CONFIG -= qt xml gui
# if you are linking against the libretroshare with gxs. # if you are linking against the libretroshare with gxs.

View file

@ -75,7 +75,7 @@ int main(int argc, char **argv)
{ {
#ifdef ENABLE_WEBUI #ifdef ENABLE_WEBUI
std::string docroot = ""; std::string docroot = resource_api::getDefaultDocroot();
uint16_t httpPort = 0; uint16_t httpPort = 0;
std::string listenAddress; std::string listenAddress;
bool allowAllIps = false; bool allowAllIps = false;
@ -85,17 +85,18 @@ int main(int argc, char **argv)
args >> parameter("docroot", docroot, "path", "Serve static files from this path.", false); args >> parameter("docroot", docroot, "path", "Serve static files from this path.", false);
// unfinished // unfinished
//args >> parameter("http-listen", listenAddress, "ipv6 address", "Listen only on the specified address.", false); //args >> parameter("http-listen", listenAddress, "ipv6 address", "Listen only on the specified address.", false);
args >> option("http-allow-all", allowAllIps, "allow connections from all IP adresses (default: localhost only)"); args >> option("http-allow-all", allowAllIps, "allow connections from all IP adresses (default= localhost only)");
args >> help(); args >> help();
if (args.helpRequested()) if (args.helpRequested())
{ {
std::cerr << args.usage() << std::endl; std::cerr << args.usage() << std::endl;
// print libretroshare command line args and exit
RsInit::InitRsConfig();
RsInit::InitRetroShare(argc, argv, true);
return 0;
} }
if(docroot.empty())
docroot = resource_api::getDefaultDocroot();
resource_api::ApiServer api; resource_api::ApiServer api;
resource_api::RsControlModule ctrl_mod(argc, argv, api.getStateTokenServer(), &api, true); resource_api::RsControlModule ctrl_mod(argc, argv, api.getStateTokenServer(), &api, true);
api.addResourceHandler("control", dynamic_cast<resource_api::ResourceRouter*>(&ctrl_mod), &resource_api::RsControlModule::handleRequest); api.addResourceHandler("control", dynamic_cast<resource_api::ResourceRouter*>(&ctrl_mod), &resource_api::RsControlModule::handleRequest);