mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-10 07:32:42 -04:00
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:
parent
444730d19c
commit
7b5bfc99d3
3 changed files with 79 additions and 65 deletions
|
@ -5,72 +5,83 @@
|
||||||
|
|
||||||
#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
|
||||||
/**
|
#include <termios.h>
|
||||||
Linux (POSIX) implementation of _kbhit().
|
#include <stdio.h>
|
||||||
Morgan McGuire, morgan@cs.brown.edu
|
#include <sys/ioctl.h>
|
||||||
(modified to disable echo)
|
#endif
|
||||||
*/
|
#define TERMINALINPUT_DEBUG
|
||||||
#include <stdio.h>
|
class TerminalInput
|
||||||
#include <sys/select.h>
|
{
|
||||||
#include <sys/ioctl.h>
|
public:
|
||||||
#include <termios.h>
|
TerminalInput()
|
||||||
#include <stropts.h>
|
{
|
||||||
|
#ifndef _WIN32
|
||||||
|
/*
|
||||||
|
Q: Is there a getch() (from conio) equivalent on Linux/UNIX?
|
||||||
|
|
||||||
int _kbhit() {
|
A: No. But it's easy to emulate:
|
||||||
static const int STDIN = 0;
|
|
||||||
static bool initialized = false;
|
|
||||||
|
|
||||||
if (! initialized) {
|
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''.
|
||||||
// Use termios to turn off line buffering
|
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).
|
||||||
termios term;
|
|
||||||
tcgetattr(STDIN, &term);
|
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 &= ~ICANON;
|
||||||
term.c_lflag &= ~ECHO; // disable echo
|
term.c_lflag &= ~ECHO; // disable echo
|
||||||
tcsetattr(STDIN, TCSANOW, &term);
|
tcsetattr(STDIN_FILENO, TCSANOW, &term);
|
||||||
setbuf(stdin, NULL);
|
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;
|
// returns a non zero value if a key was pressed
|
||||||
ioctl(STDIN, FIONREAD, &bytesWaiting);
|
int kbhit()
|
||||||
return bytesWaiting;
|
{
|
||||||
}
|
#ifdef _WIN32
|
||||||
/*
|
return _kbhit();
|
||||||
Q: Is there a getch() (from conio) equivalent on Linux/UNIX?
|
#else // LINUX
|
||||||
|
int bytesWaiting;
|
||||||
A: No. But it's easy to emulate:
|
ioctl(STDIN_FILENO, FIONREAD, &bytesWaiting);
|
||||||
|
return bytesWaiting;
|
||||||
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''.
|
#endif
|
||||||
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).
|
}
|
||||||
|
// return the pressed key
|
||||||
http://cboard.cprogramming.com/faq-board/27714-faq-there-getch-conio-equivalent-linux-unix.html
|
int getch()
|
||||||
*/
|
{
|
||||||
#include <stdio.h>
|
#ifdef _WIN32
|
||||||
#include <termios.h>
|
return _getch();
|
||||||
#include <unistd.h>
|
#else // LINUX
|
||||||
|
return getchar();
|
||||||
int _getch( ) {
|
#endif
|
||||||
struct termios oldt,
|
}
|
||||||
newt;
|
private:
|
||||||
int ch;
|
#ifndef _WIN32
|
||||||
tcgetattr( STDIN_FILENO, &oldt );
|
struct termios mOldTermSettings;
|
||||||
newt = oldt;
|
#endif
|
||||||
newt.c_lflag &= ~( ICANON | ECHO );
|
};
|
||||||
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
|
|
||||||
ch = getchar();
|
|
||||||
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
|
|
||||||
return ch;
|
|
||||||
}
|
|
||||||
#endif // LINUX
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -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,20 +85,21 @@ 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);
|
||||||
|
|
||||||
resource_api::ApiServerMHD* httpd = 0;
|
resource_api::ApiServerMHD* httpd = 0;
|
||||||
if(httpPort != 0)
|
if(httpPort != 0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue