Allow rtorrent to run as a daemon, slightly modified version of the PR provided by sallyswiss

This commit is contained in:
zp
2016-06-19 02:06:30 -05:00
parent 9abcea84c7
commit c01709fa27
4 changed files with 99 additions and 70 deletions
+52 -36
View File
@@ -41,54 +41,63 @@
#include <termios.h>
#include <torrent/exceptions.h>
#include "rpc/parse_commands.h"
#include "canvas.h"
namespace display {
bool Canvas::m_isInitialized = false;
bool Canvas::m_isDaemon = false;
Canvas::Canvas(int x, int y, int width, int height) :
m_window(newwin(height, width, y, x)) {
Canvas::Canvas(int x, int y, int width, int height) {
if (!m_isDaemon) {
m_window = newwin(height, width, y, x);
if (m_window == NULL)
throw torrent::internal_error("Could not allocate ncurses canvas.");
if (m_window == NULL)
throw torrent::internal_error("Could not allocate ncurses canvas.");
}
}
void
Canvas::resize(int x, int y, int w, int h) {
wresize(m_window, h, w);
mvwin(m_window, y, x);
if (!m_isDaemon) {
wresize(m_window, h, w);
mvwin(m_window, y, x);
}
}
void
Canvas::print_attributes(unsigned int x, unsigned int y, const char* first, const char* last, const attributes_list* attributes) {
move(x, y);
if (!m_isDaemon) {
move(x, y);
attr_t org_attr;
short org_pair;
wattr_get(m_window, &org_attr, &org_pair, NULL);
attr_t org_attr;
short org_pair;
wattr_get(m_window, &org_attr, &org_pair, NULL);
attributes_list::const_iterator attrItr = attributes->begin();
wattr_set(m_window, Attributes::a_normal, Attributes::color_default, NULL);
attributes_list::const_iterator attrItr = attributes->begin();
wattr_set(m_window, Attributes::a_normal, Attributes::color_default, NULL);
while (first != last) {
const char* next = last;
while (first != last) {
const char* next = last;
if (attrItr != attributes->end()) {
next = attrItr->position();
if (attrItr != attributes->end()) {
next = attrItr->position();
if (first >= next) {
wattr_set(m_window, attrItr->attributes(), attrItr->colors(), NULL);
++attrItr;
if (first >= next) {
wattr_set(m_window, attrItr->attributes(), attrItr->colors(), NULL);
++attrItr;
}
}
print("%.*s", next - first, first);
first = next;
}
print("%.*s", next - first, first);
first = next;
// Reset the color.
wattr_set(m_window, org_attr, org_pair, NULL);
}
// Reset the color.
wattr_set(m_window, org_attr, org_pair, NULL);
}
void
@@ -96,14 +105,18 @@ Canvas::initialize() {
if (m_isInitialized)
return;
m_isDaemon = rpc::call_command_value("system.use_daemon");
m_isInitialized = true;
initscr();
raw();
noecho();
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
curs_set(0);
if (!m_isDaemon) {
initscr();
raw();
noecho();
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
curs_set(0);
}
}
void
@@ -113,18 +126,21 @@ Canvas::cleanup() {
m_isInitialized = false;
noraw();
endwin();
if (!m_isDaemon) {
noraw();
endwin();
}
}
std::pair<int, int>
Canvas::term_size() {
struct winsize ws;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
return std::pair<int, int>(ws.ws_col, ws.ws_row);
else
return std::pair<int, int>(80, 24);
if (!m_isDaemon) {
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
return std::pair<int, int>(ws.ws_col, ws.ws_row);
}
return std::pair<int, int>(80, 24);
}
}