diff --git a/src/core/manager.cc b/src/core/manager.cc index c9716943..12863f7d 100644 --- a/src/core/manager.cc +++ b/src/core/manager.cc @@ -9,7 +9,6 @@ #include "manager.h" - namespace core { void diff --git a/src/display/Makefile.am b/src/display/Makefile.am index 319f6f71..5ae5ce03 100644 --- a/src/display/Makefile.am +++ b/src/display/Makefile.am @@ -11,6 +11,8 @@ libsub_display_a_SOURCES = \ window_download_statusbar.h \ window_download_list.cc \ window_download_list.h \ + window_input.cc \ + window_input.h \ window_peer_info.cc \ window_peer_info.h \ window_peer_list.cc \ diff --git a/src/display/canvas.h b/src/display/canvas.h index 1d2103af..b93392d5 100644 --- a/src/display/canvas.h +++ b/src/display/canvas.h @@ -23,6 +23,7 @@ public: int get_y() { int x, y; getyx(m_window, y, x); return y; } int get_width() { int x, y; getmaxyx(m_window, y, x); return x; } int get_height() { int x, y; getmaxyx(m_window, y, x); return y; } + chtype get_background() { return getbkgd(m_window); } void set_background(chtype c) { return wbkgdset(m_window, c); } @@ -33,15 +34,6 @@ public: chtype tl, chtype tr, chtype bl, chtype br) { wborder(m_window, ls, rs, ts, bs, tl, tr, bl, br); } - // Initialize stdscr. - static void init(); - static void cleanup(); - - static int get_screen_width() { int x, y; getmaxyx(stdscr, y, x); return x; } - static int get_screen_height() { int x, y; getmaxyx(stdscr, y, x); return y; } - - static void do_update() { doupdate(); } - void print(int x, int y, const char* str) { mvwprintw(m_window, y, x, str); } template @@ -66,6 +58,17 @@ public: void print(int x, int y, const char* str, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) { mvwprintw(m_window, y, x, str, a1, a2, a3, a4, a5, a6); } + void set_attr(int x, int y, int n, int attr, int color) { mvwchgat(m_window, y, x, n, attr, color, NULL); } + + // Initialize stdscr. + static void init(); + static void cleanup(); + + static int get_screen_width() { int x, y; getmaxyx(stdscr, y, x); return x; } + static int get_screen_height() { int x, y; getmaxyx(stdscr, y, x); return y; } + + static void do_update() { doupdate(); } + private: Canvas(const Canvas&); void operator = (const Canvas&); diff --git a/src/display/window_download_statusbar.cc b/src/display/window_download_statusbar.cc index 66a99e7d..66b85ce4 100644 --- a/src/display/window_download_statusbar.cc +++ b/src/display/window_download_statusbar.cc @@ -8,7 +8,7 @@ namespace display { WindowDownloadStatusbar::WindowDownloadStatusbar(core::Download* d) : - Window(new Canvas, false, 2), + Window(new Canvas, false, 3), m_download(d) { } @@ -36,7 +36,14 @@ WindowDownloadStatusbar::redraw() { (double)m_download->get_download().get_rate_down() / 1024.0, (double)m_download->get_download().get_bytes_up() / (double)(1 << 20)); - m_canvas->print(0, 1, "Tracker: [%c:%i] %s", + m_canvas->print(0, 1, "Peers: %i(%i) Min/Max: %i/%i Uploads: %i", + (int)m_download->get_download().get_peers_connected(), + (int)m_download->get_download().get_peers_not_connected(), + (int)m_download->get_download().get_peers_min(), + (int)m_download->get_download().get_peers_max(), + (int)m_download->get_download().get_uploads_max()); + + m_canvas->print(0, 2, "Tracker: [%c:%i] %s", m_download->get_download().is_tracker_busy() ? 'C' : ' ', (int)(m_download->get_download().get_tracker_timeout() / 1000000), m_download->get_tracker_msg().c_str()); diff --git a/src/display/window_input.cc b/src/display/window_input.cc new file mode 100644 index 00000000..dbc71924 --- /dev/null +++ b/src/display/window_input.cc @@ -0,0 +1,30 @@ +#include "config.h" + +#include "canvas.h" +#include "window_input.h" + +#include "input/text_input.h" + +namespace display { + +WindowInput::WindowInput(input::TextInput* input) : + Window(new Canvas, false, 1), + m_input(input), + m_focus(false) { +} + +void +WindowInput::redraw() { + if (!is_dirty()) + return; + + m_lastDraw = Timer::cache(); + m_canvas->erase(); + + m_canvas->print(0, 0, "> %s", m_input->c_str()); + + if (m_focus) + m_canvas->set_attr(m_input->get_pos() + 2, 0, 1, A_REVERSE, COLOR_PAIR(0)); +} + +} diff --git a/src/display/window_input.h b/src/display/window_input.h new file mode 100644 index 00000000..af6e7af8 --- /dev/null +++ b/src/display/window_input.h @@ -0,0 +1,30 @@ +#ifndef RTORRENT_DISPLAY_WINDOW_INPUT_H +#define RTORRENT_DISPLAY_WINDOW_INPUT_H + +#include "window.h" + +namespace input { + class TextInput; +} + +namespace display { + +class WindowInput : public Window { +public: + WindowInput(input::TextInput* input); + + input::TextInput* get_input() { return m_input; } + + bool get_focus() { return m_focus; } + void set_focus(bool f) { mark_dirty(); m_focus = f; } + + virtual void redraw(); + +private: + input::TextInput* m_input; + bool m_focus; +}; + +} + +#endif diff --git a/src/display/window_peer_info.cc b/src/display/window_peer_info.cc index 6c3dd13b..06c8ab27 100644 --- a/src/display/window_peer_info.cc +++ b/src/display/window_peer_info.cc @@ -33,14 +33,14 @@ WindowPeerInfo::redraw() { m_download->get_download().get_chunks_total(), m_download->get_download().get_chunks_size()); + y++; + if (*m_focus == m_list->end()) { m_canvas->print(0, y++, "No peer in focus"); return; } - y++; - m_canvas->print(0, y++, "DNS: %s:%hu", (*m_focus)->get_dns().c_str(), (*m_focus)->get_port()); m_canvas->print(0, y++, "Id: %s" , escape_string((*m_focus)->get_id()).c_str()); m_canvas->print(0, y++, "Snubbed: %s", (*m_focus)->get_snubbed() ? "Yes" : "No"); diff --git a/src/display/window_title.cc b/src/display/window_title.cc index d5b4b0e1..394d66d4 100644 --- a/src/display/window_title.cc +++ b/src/display/window_title.cc @@ -12,7 +12,12 @@ WindowTitle::WindowTitle(const std::string& s) : void WindowTitle::redraw() { + if (Timer::cache() - m_lastDraw < 10000000) + return; + + m_lastDraw = Timer::cache(); m_canvas->erase(); + m_canvas->print(std::max(0, (m_canvas->get_width() - (int)m_title.size()) / 2 - 4), 0, "*** %s ***", m_title.c_str()); } diff --git a/src/functional.h b/src/functional.h index 64d08fdd..9b8163e3 100644 --- a/src/functional.h +++ b/src/functional.h @@ -5,6 +5,17 @@ namespace func { +template +inline void for_each(Iterator first, Iterator last, Ftor ftor) { + Iterator tmp; + + while (first != last) { + tmp = first++; + + ftor(*tmp); + } +} + template struct _accumulate { _accumulate(Type& t, Ftor f) : m_t(t), m_f(f) {} diff --git a/src/input/Makefile.am b/src/input/Makefile.am index 7244f865..76fc66b7 100644 --- a/src/input/Makefile.am +++ b/src/input/Makefile.am @@ -4,6 +4,8 @@ libsub_input_a_SOURCES = \ bindings.cc \ bindings.h \ manager.cc \ - manager.h + manager.h \ + text_input.cc \ + text_input.h INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir) diff --git a/src/input/manager.cc b/src/input/manager.cc index ec153f7d..d4dd47ea 100644 --- a/src/input/manager.cc +++ b/src/input/manager.cc @@ -6,6 +6,7 @@ #include "manager.h" #include "bindings.h" +#include "text_input.h" namespace input { @@ -21,6 +22,9 @@ Manager::erase(Bindings* b) { void Manager::pressed(int key) { + if (m_textInput != NULL && m_textInput->pressed(key)) + return; + std::find_if(begin(), end(), std::bind2nd(std::mem_fun(&Bindings::pressed), key)); } diff --git a/src/input/manager.h b/src/input/manager.h index df6e6c56..7faca79a 100644 --- a/src/input/manager.h +++ b/src/input/manager.h @@ -6,6 +6,7 @@ namespace input { class Bindings; +class TextInput; class Manager : private std::list { public: @@ -24,11 +25,18 @@ public: using Base::push_back; using Base::push_front; + Manager() : m_textInput(NULL) {} + void erase(Bindings* b); void pressed(int key); + void set_text_input(TextInput* input = NULL) { m_textInput = input; } + // Slot for unreacted keys. + +private: + TextInput* m_textInput; }; } diff --git a/src/input/text_input.cc b/src/input/text_input.cc new file mode 100644 index 00000000..9b9206ca --- /dev/null +++ b/src/input/text_input.cc @@ -0,0 +1,79 @@ +#include "config.h" + +#include + +#include "text_input.h" +#include + +namespace input { + +bool +TextInput::pressed(int key) { + if (m_alt) { + m_alt = false; + + switch (key) { + case 'p': + Base::insert(m_pos, "M^p"); + break; + + default: + return false; + } + + } else if (key >= 0x20 && key < 0x7F) { + Base::insert(m_pos++, 1, key); + + } else { + switch (key) { + case KEY_BACKSPACE: + if (m_pos != 0) + Base::erase(--m_pos, 1); + + break; + + case KEY_DC: + if (m_pos != size()) + Base::erase(m_pos, 1); + + break; + + case KEY_LEFT: + case 0x10: + if (m_pos != 0) + --m_pos; + + break; + + case KEY_RIGHT: + case 0x0E: + if (m_pos != size()) + ++m_pos; + + break; + + default: + return false; + } + } + + m_slotDirty(); + + return true; + + // Testcode. +// if (key == KEY_ENTER || key == '\n') +// return false; + +// std::stringstream str; + +// str << "\\x" << std::hex << key; + +// Base::insert(m_pos, str.str()); + +// m_pos += str.str().length(); + +// return true; +} + +} diff --git a/src/input/text_input.h b/src/input/text_input.h new file mode 100644 index 00000000..099c6416 --- /dev/null +++ b/src/input/text_input.h @@ -0,0 +1,37 @@ +#ifndef RTORRENT_INPUT_TEXT_INPUT_H +#define RTORRENT_INPUT_TEXT_INPUT_H + +#include +#include + +namespace input { + +class TextInput : private std::string { +public: + typedef std::string Base; + typedef sigc::slot0 SlotDirty; + + using Base::c_str; + using Base::empty; + using Base::size; + + TextInput() : m_pos(0), m_alt(false) {} + + size_type get_pos() { return m_pos; } + + bool pressed(int key); + + void clear() { m_pos = 0; m_alt = false; Base::clear(); } + + void slot_dirty(SlotDirty s) { m_slotDirty = s; } + +private: + size_type m_pos; + + bool m_alt; + SlotDirty m_slotDirty; +}; + +} + +#endif diff --git a/src/main.cc b/src/main.cc index d2d7708c..13519cd6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -124,7 +124,7 @@ main(int argc, char** argv) { coreManager.get_http_queue().slot_factory(poll.get_http_factory()); torrent::initialize(); - torrent::listen_open(6880, 6999); + torrent::listen_open(10000, 20000); for (int i = 1; i < argc; ++i) coreManager.insert(argv[i]); diff --git a/src/ui/download.cc b/src/ui/download.cc index d1d0f088..7cf46536 100644 --- a/src/ui/download.cc +++ b/src/ui/download.cc @@ -24,7 +24,7 @@ Download::Download(DPtr d, Control* c) : m_title(c->get_display().end()), m_window(c->get_display().end()), m_downloadStatus(c->get_display().end()), - m_status(c->get_display().end()), + m_mainStatus(c->get_display().end()), m_control(c), m_bindings(new input::Bindings) { @@ -55,7 +55,7 @@ Download::activate() { m_window = m_control->get_display().insert(m_control->get_display().begin(), NULL); m_title = m_control->get_display().insert(m_control->get_display().begin(), new WTitle(m_download->get_download().get_name())); m_downloadStatus = m_control->get_display().insert(m_control->get_display().end(), new WDownloadStatus(m_download)); - m_status = m_control->get_display().insert(m_control->get_display().end(), new WStatus); + m_mainStatus = m_control->get_display().insert(m_control->get_display().end(), new WMainStatus); m_control->get_input().push_front(m_bindings); @@ -71,14 +71,14 @@ Download::disable() { delete *m_title; delete *m_downloadStatus; - delete *m_status; + delete *m_mainStatus; m_control->get_display().erase(m_window); m_control->get_display().erase(m_title); m_control->get_display().erase(m_downloadStatus); - m_control->get_display().erase(m_status); + m_control->get_display().erase(m_mainStatus); - m_window = m_title = m_downloadStatus = m_status = m_control->get_display().end(); + m_window = m_title = m_downloadStatus = m_mainStatus = m_control->get_display().end(); m_control->get_input().erase(m_bindings); } @@ -180,11 +180,32 @@ Download::receive_peer_disconnected(torrent::Peer p) { void Download::receive_throttle(int t) { - (*m_status)->mark_dirty(); + (*m_mainStatus)->mark_dirty(); torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) + t * 1024); } +void +Download::receive_max_uploads(int t) { + (*m_downloadStatus)->mark_dirty(); + + m_download->get_download().set_uploads_max(std::max(m_download->get_download().get_uploads_max() + t, (uint32_t)2)); +} + +void +Download::receive_min_peers(int t) { + (*m_downloadStatus)->mark_dirty(); + + m_download->get_download().set_peers_min(std::max(m_download->get_download().get_peers_min() + t, (uint32_t)5)); +} + +void +Download::receive_max_peers(int t) { + (*m_downloadStatus)->mark_dirty(); + + m_download->get_download().set_peers_max(std::max(m_download->get_download().get_peers_max() + t, (uint32_t)5)); +} + void Download::receive_change(Display d) { if (d == m_state) @@ -204,6 +225,13 @@ Download::bind_keys(input::Bindings* b) { (*b)['d'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_throttle), 50); (*b)['c'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_throttle), -50); + (*b)['1'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_uploads), -1); + (*b)['2'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_uploads), 1); + (*b)['3'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_min_peers), -5); + (*b)['4'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_min_peers), 5); + (*b)['5'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_peers), -5); + (*b)['6'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_peers), 5); + (*b)['t'] = sigc::bind(sigc::mem_fun(m_download->get_download(), &torrent::Download::set_tracker_timeout), 2 * 1000000); (*b)[KEY_UP] = sigc::mem_fun(*this, &Download::receive_prev); diff --git a/src/ui/download.h b/src/ui/download.h index 24c6f01c..6df324f0 100644 --- a/src/ui/download.h +++ b/src/ui/download.h @@ -27,7 +27,7 @@ public: typedef display::WindowPeerInfo WPeerInfo; typedef display::WindowPeerList WPeerList; typedef display::WindowTitle WTitle; - typedef display::WindowStatusbar WStatus; + typedef display::WindowStatusbar WMainStatus; typedef display::WindowDownloadStatusbar WDownloadStatus; typedef core::Download* DPtr; @@ -64,6 +64,9 @@ private: void receive_peer_disconnected(torrent::Peer p); void receive_throttle(int t); + void receive_max_uploads(int t); + void receive_min_peers(int t); + void receive_max_peers(int t); void receive_change(Display d); void bind_keys(input::Bindings* b); @@ -79,7 +82,7 @@ private: MItr m_title; MItr m_window; MItr m_downloadStatus; - MItr m_status; + MItr m_mainStatus; Control* m_control; input::Bindings* m_bindings; diff --git a/src/ui/download_list.cc b/src/ui/download_list.cc index dc054030..e148e194 100644 --- a/src/ui/download_list.cc +++ b/src/ui/download_list.cc @@ -5,9 +5,11 @@ #include #include "input/bindings.h" +#include "input/text_input.h" #include "display/window_title.h" #include "display/window_download_list.h" #include "display/window_statusbar.h" +#include "display/window_input.h" #include "control.h" #include "download.h" @@ -22,11 +24,14 @@ DownloadList::DownloadList(core::DownloadList* l, Control* c) : m_list(l), m_focus(l->end()), m_control(c), - m_bindings(new input::Bindings) { + m_bindings(new input::Bindings), + m_windowInput(new WInput(new input::TextInput)) { m_window = new WList(m_list, &m_focus); bind_keys(m_bindings); + + m_windowInput->get_input()->slot_dirty(sigc::mem_fun(*m_windowInput, &WInput::mark_dirty)); } DownloadList::~DownloadList() { @@ -34,10 +39,14 @@ DownloadList::~DownloadList() { delete m_title; delete m_status; delete m_bindings; + + delete m_windowInput->get_input(); + delete m_windowInput; } void DownloadList::activate() { + m_control->get_display().push_back(m_windowInput); m_control->get_display().push_back(m_status); m_control->get_display().push_front(m_window); m_control->get_display().push_front(m_title); @@ -46,6 +55,7 @@ DownloadList::activate() { void DownloadList::disable() { + m_control->get_display().erase(m_windowInput); m_control->get_display().erase(m_title); m_control->get_display().erase(m_window); m_control->get_display().erase(m_status); @@ -109,6 +119,27 @@ DownloadList::receive_throttle(int t) { torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) + t * 1024); } +void +DownloadList::receive_view_input() { + m_control->get_input().set_text_input(m_windowInput->get_input()); + + m_windowInput->set_focus(true); + + (*m_bindings)['\n'] = sigc::mem_fun(*this, &DownloadList::receive_exit_input); + (*m_bindings)[KEY_ENTER] = sigc::mem_fun(*this, &DownloadList::receive_exit_input); +} + +void +DownloadList::receive_exit_input() { + m_control->get_input().set_text_input(); + + m_windowInput->get_input()->clear(); + m_windowInput->set_focus(false); + + m_bindings->erase('\n'); + m_bindings->erase(KEY_ENTER); +} + void DownloadList::bind_keys(input::Bindings* b) { (*b)['a'] = sigc::bind(sigc::mem_fun(*this, &DownloadList::receive_throttle), 1); @@ -122,6 +153,7 @@ DownloadList::bind_keys(input::Bindings* b) { (*b)[KEY_DOWN] = sigc::mem_fun(*this, &DownloadList::receive_next); (*b)[KEY_RIGHT] = sigc::mem_fun(*this, &DownloadList::receive_view_download); + (*b)[KEY_BACKSPACE] = sigc::mem_fun(*this, &DownloadList::receive_view_input); } void diff --git a/src/ui/download_list.h b/src/ui/download_list.h index c2003405..4fa488c3 100644 --- a/src/ui/download_list.h +++ b/src/ui/download_list.h @@ -5,6 +5,7 @@ namespace display { class WindowTitle; class WindowDownloadList; class WindowStatusbar; + class WindowInput; } namespace ui { @@ -17,6 +18,7 @@ public: typedef display::WindowDownloadList WList; typedef display::WindowTitle WTitle; typedef display::WindowStatusbar WStatus; + typedef display::WindowInput WInput; typedef core::DownloadList DList; // We own 'window'. @@ -41,6 +43,9 @@ private: void receive_throttle(int t); + void receive_view_input(); + void receive_exit_input(); + void bind_keys(input::Bindings* b); void mark_dirty(); @@ -56,6 +61,9 @@ private: Control* m_control; input::Bindings* m_bindings; + + // Test + WInput* m_windowInput; }; }