From 7b5bfc99d3dad94bb75ddbb13dab3575b50d4742 Mon Sep 17 00:00:00 2001 From: electron128 Date: Sun, 3 May 2015 14:46:10 +0000 Subject: [PATCH] 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 --- retroshare-nogui/src/TerminalApiClient.cpp | 129 ++++++++++++--------- retroshare-nogui/src/retroshare-nogui.pro | 2 +- retroshare-nogui/src/retroshare.cc | 13 ++- 3 files changed, 79 insertions(+), 65 deletions(-) diff --git a/retroshare-nogui/src/TerminalApiClient.cpp b/retroshare-nogui/src/TerminalApiClient.cpp index cad7faf2e..76d299ed5 100644 --- a/retroshare-nogui/src/TerminalApiClient.cpp +++ b/retroshare-nogui/src/TerminalApiClient.cpp @@ -5,72 +5,83 @@ #include -// need two functions for non blocking read from stdin: -// int _kbhit() (returns a non zero value if a key was pressed) -// int _getch() (return the pressed key) -// these function are available on windows in conio.h -// they are not available on linux +// windows has _kbhit() and _getch() fr non-blocking keaboard read. +// linux does not have these functions. +// the TerminalInput class provides both for win and linux +// only use a single instance of this class in the whole program! +// 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 #include #else // LINUX -/** - Linux (POSIX) implementation of _kbhit(). - Morgan McGuire, morgan@cs.brown.edu - (modified to disable echo) - */ -#include -#include -#include -#include -#include + #include + #include + #include +#endif +#define TERMINALINPUT_DEBUG +class TerminalInput +{ +public: + TerminalInput() + { +#ifndef _WIN32 + /* + Q: Is there a getch() (from conio) equivalent on Linux/UNIX? -int _kbhit() { - static const int STDIN = 0; - static bool initialized = false; + A: No. But it's easy to emulate: - if (! initialized) { - // Use termios to turn off line buffering - termios term; - tcgetattr(STDIN, &term); + This code sets the terminal into non-canonical mode, thus disabling line buffering, reads a character from stdin and then restores the old terminal status. For more info on what else you can do with termios, see ``man termios''. + There's also a ``getch()'' function in the curses library, but it is /not/ equivalent to the DOS ``getch()'' and may only be used within real curses applications (ie: it only works in curses ``WINDOW''s). + + http://cboard.cprogramming.com/faq-board/27714-faq-there-getch-conio-equivalent-linux-unix.html + */ + tcgetattr(STDIN_FILENO, &mOldTermSettings); + termios term = mOldTermSettings; term.c_lflag &= ~ICANON; term.c_lflag &= ~ECHO; // disable echo - tcsetattr(STDIN, TCSANOW, &term); + tcsetattr(STDIN_FILENO, TCSANOW, &term); setbuf(stdin, NULL); - initialized = true; +#endif + } + ~TerminalInput() + { +#ifndef _WIN32 + // restore terminal settings + tcsetattr(STDIN_FILENO, TCSANOW, &mOldTermSettings); +#ifdef TERMINALINPUT_DEBUG + std::cerr << "Terminal restored" << std::endl; +#endif +#endif } - int bytesWaiting; - ioctl(STDIN, FIONREAD, &bytesWaiting); - return bytesWaiting; -} -/* -Q: Is there a getch() (from conio) equivalent on Linux/UNIX? - - A: No. But it's easy to emulate: - -This code sets the terminal into non-canonical mode, thus disabling line buffering, reads a character from stdin and then restores the old terminal status. For more info on what else you can do with termios, see ``man termios''. - There's also a ``getch()'' function in the curses library, but it is /not/ equivalent to the DOS ``getch()'' and may only be used within real curses applications (ie: it only works in curses ``WINDOW''s). - -http://cboard.cprogramming.com/faq-board/27714-faq-there-getch-conio-equivalent-linux-unix.html -*/ -#include -#include -#include - -int _getch( ) { - struct termios oldt, - newt; - int ch; - tcgetattr( STDIN_FILENO, &oldt ); - newt = oldt; - newt.c_lflag &= ~( ICANON | ECHO ); - tcsetattr( STDIN_FILENO, TCSANOW, &newt ); - ch = getchar(); - tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); - return ch; -} -#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 { @@ -108,6 +119,8 @@ void TerminalApiClient::run() bool ask_for_password = false; std::string key_name; + TerminalInput term; + while(isRunning()) { // assuming sleep_time >> work_time @@ -120,10 +133,10 @@ void TerminalApiClient::run() { last_io_poll = 0; last_char = 0; - if(_kbhit()) + if(term.kbhit()) { enter_was_pressed = false; - last_char = _getch(); + last_char = term.getch(); if(last_char > 127) std::cout << "Warning: non ASCII characters probably won't work." << std::endl; if(last_char >= ' ')// space is the first printable ascii character diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index 22a638869..5bdc2189d 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -4,7 +4,7 @@ CONFIG += bitdht #CONFIG += introserver #CONFIG += sshserver # webinterface, requires libmicrohttpd -#CONFIG += webui +CONFIG += webui CONFIG -= qt xml gui # if you are linking against the libretroshare with gxs. diff --git a/retroshare-nogui/src/retroshare.cc b/retroshare-nogui/src/retroshare.cc index f7a19306f..a2d33a274 100644 --- a/retroshare-nogui/src/retroshare.cc +++ b/retroshare-nogui/src/retroshare.cc @@ -75,7 +75,7 @@ int main(int argc, char **argv) { #ifdef ENABLE_WEBUI - std::string docroot = ""; + std::string docroot = resource_api::getDefaultDocroot(); uint16_t httpPort = 0; std::string listenAddress; bool allowAllIps = false; @@ -85,20 +85,21 @@ int main(int argc, char **argv) args >> parameter("docroot", docroot, "path", "Serve static files from this path.", false); // unfinished //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(); if (args.helpRequested()) { 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::RsControlModule ctrl_mod(argc, argv, api.getStateTokenServer(), &api, true); - api.addResourceHandler("control", dynamic_cast(&ctrl_mod), &resource_api::RsControlModule::handleRequest); + api.addResourceHandler("control", dynamic_cast(&ctrl_mod), &resource_api::RsControlModule::handleRequest); resource_api::ApiServerMHD* httpd = 0; if(httpPort != 0)