From f30263c2fc92a5964210a615287141e00a55e05e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Fri, 5 Oct 2018 05:46:50 +0200 Subject: [PATCH] Initial ncurses display-output Some leftovers still, but it still works. --- src/conky.cc | 48 ++++++---------------------- src/display-ncurses.cc | 72 ++++++++++++++++++++++++++++++++++++++++-- src/display-ncurses.hh | 16 ++++++++++ src/display-output.hh | 11 +++++++ 4 files changed, 106 insertions(+), 41 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index c45134cc..5b52aec7 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -171,10 +171,6 @@ static conky::simple_config_setting disable_auto_reload( /* two strings for internal use */ static char *tmpstring1, *tmpstring2; -#ifdef BUILD_NCURSES -extern WINDOW *ncurses_window; -#endif - enum spacer_state { NO_SPACER = 0, LEFT_SPACER, RIGHT_SPACER }; template <> conky::lua_traits::Map conky::lua_traits::map = { @@ -1059,9 +1055,8 @@ static inline void set_foreground_color(long c) { XSetForeground(display, window.gc, current_color); } #endif /* BUILD_X11 */ -#ifdef BUILD_NCURSES - if (out_to_ncurses.get(*state)) { attron(COLOR_PAIR(c)); } -#endif /* BUILD_NCURSES */ + for (auto output : conky::active_display_outputs) + output->set_foreground_color(c); UNUSED(c); } @@ -1095,9 +1090,6 @@ static void draw_string(const char *s) { if (draw_mode == FG && (append_fpointer != nullptr)) { fprintf(append_fpointer, "%s\n", s); } -#ifdef BUILD_NCURSES - if (out_to_ncurses.get(*state) && draw_mode == FG) { printw("%s", s); } -#endif /* BUILD_NCURSES */ if (conky::active_display_outputs.size() && draw_mode == FG) for (auto output : conky::active_display_outputs) output->draw_string(s, width_of_s); @@ -1602,14 +1594,9 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) { // make sure shades are 1 pixel to the right of the text if (draw_mode == BG) { cur_x++; } #endif /* BUILD_X11 */ -#ifdef BUILD_NCURSES cur_x = static_cast(current->arg); - if (out_to_ncurses.get(*state)) { - int x, y; - getyx(ncurses_window, y, x); - move(y, cur_x); - } -#endif /* BUILD_NCURSES */ + for (auto output : conky::active_display_outputs) + output->gotox(cur_x); } break; } @@ -1632,9 +1619,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) { cur_y += cur_y_add; #endif /* BUILD_X11 */ draw_string(s); -#ifdef BUILD_NCURSES - if (out_to_ncurses.get(*state)) { printw("\n"); } -#endif /* BUILD_NCURSES */ + for (auto output : display_outputs()) output->line_inner_done(); #ifdef BUILD_X11 if (out_to_x.get(*state)) { cur_y += font_descent(); } #endif /* BUILD_X11 */ @@ -1647,11 +1632,10 @@ static int draw_line(char *s, int special_index) { return draw_each_line_inner(s, special_index, -1); } #endif /* BUILD_X11 */ -#ifdef BUILD_NCURSES - if (out_to_ncurses.get(*state)) { + + if (display_output() && display_output()->draw_line_inner_required()) { return draw_each_line_inner(s, special_index, -1); } -#endif /* BUILD_NCURSES */ draw_string(s); UNUSED(special_index); return 0; @@ -1688,10 +1672,6 @@ static void draw_text() { } setup_fonts(); #endif /* BUILD_X11 */ -#ifdef BUILD_NCURSES - init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); - attron(COLOR_PAIR(COLOR_WHITE)); -#endif /* BUILD_NCURSES */ for_each_line(text_buffer, draw_line); for (auto output : conky::active_display_outputs) output->end_draw_text(); } @@ -2207,12 +2187,7 @@ void main_loop() { nanosleep(&req, &rem); update_text(); draw_stuff(); -#ifdef BUILD_NCURSES - if (out_to_ncurses.get(*state)) { - refresh(); - clear(); - } -#endif + for (auto output : conky::active_display_outputs) output->flush(); #ifdef BUILD_X11 } #endif /* BUILD_X11 */ @@ -2237,12 +2212,7 @@ void main_loop() { NORM_ERR("received SIGUSR2. refreshing."); update_text(); draw_stuff(); -#ifdef BUILD_NCURSES - if (out_to_ncurses.get(*state)) { - refresh(); - clear(); - } -#endif + for (auto output : conky::active_display_outputs) output->flush(); } if (g_sigterm_pending != 0) { diff --git a/src/display-ncurses.cc b/src/display-ncurses.cc index 744362ee..10550430 100644 --- a/src/display-ncurses.cc +++ b/src/display-ncurses.cc @@ -29,6 +29,13 @@ #include #include #include +#ifdef BUILD_NCURSES +#include +#endif + +#ifdef BUILD_NCURSES +extern WINDOW *ncurses_window; +#endif namespace conky { namespace { @@ -42,7 +49,9 @@ conky::disabled_display_output ncurses_output_disabled("ncurses", } // namespace -namespace priv {} // namespace priv +// namespace priv { + +//} // namespace priv #ifdef BUILD_NCURSES @@ -60,10 +69,69 @@ bool display_output_ncurses::detect() { return false; } -bool display_output_ncurses::initialize() { return false; } +bool display_output_ncurses::initialize() { + return (ncurses_window != nullptr); +} bool display_output_ncurses::shutdown() { return false; } +bool display_output_ncurses::set_foreground_color(long c) { + attron(COLOR_PAIR(c)); + return true; +} + +bool display_output_ncurses::begin_draw_text() { + init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); + attron(COLOR_PAIR(COLOR_WHITE)); + return true; +} + +bool display_output_ncurses::end_draw_text() { return true; } + +bool display_output_ncurses::draw_string(const char *s, int w) { + printw("%s", s); + return true; +} + +void display_output_ncurses::line_inner_done() { printw("\n"); } + +int display_output_ncurses::getx() { + int x, y; + getyx(ncurses_window, y, x); + return x; +} + +int display_output_ncurses::gety() { + int x, y; + getyx(ncurses_window, y, x); + return y; +} + +bool display_output_ncurses::gotox(int x) { + int y, old_x; + getyx(ncurses_window, y, old_x); + move(y, x); + return true; +} + +bool display_output_ncurses::gotoy(int y) { + int x, old_y; + getyx(ncurses_window, old_y, x); + move(y, x); + return true; +} + +bool display_output_ncurses::gotoxy(int x, int y) { + move(y, x); + return true; +} + +bool display_output_ncurses::flush() { + refresh(); + clear(); + return true; +} + #endif /* BUILD_NCURSES */ } // namespace conky diff --git a/src/display-ncurses.hh b/src/display-ncurses.hh index c0d23517..75b54bc0 100644 --- a/src/display-ncurses.hh +++ b/src/display-ncurses.hh @@ -47,6 +47,22 @@ class display_output_ncurses : public display_output_console { virtual bool initialize(); virtual bool shutdown(); + // drawing primitives + virtual bool set_foreground_color(long c); + + virtual bool begin_draw_text(); + virtual bool end_draw_text(); + virtual bool draw_string(const char *s, int w); + virtual void line_inner_done(); + + virtual int getx(); + virtual int gety(); + virtual bool gotox(int x); + virtual bool gotoy(int y); + virtual bool gotoxy(int x, int y); + + virtual bool flush(); + // ncurses-specific }; diff --git a/src/display-output.hh b/src/display-output.hh index 6c983aed..ab71c52c 100644 --- a/src/display-output.hh +++ b/src/display-output.hh @@ -74,9 +74,20 @@ class display_output_base { virtual bool shutdown() { return false; }; // drawing primitives + virtual bool set_foreground_color(long c) { return false; } + virtual bool begin_draw_text() { return false; }; virtual bool end_draw_text() { return false; }; virtual bool draw_string(const char *s, int w) { return false; }; + virtual void line_inner_done() {} + + virtual int getx() { return 0; }; + virtual int gety() { return 0; }; + virtual bool gotox(int x) { return false; }; + virtual bool gotoy(int y) { return false; }; + virtual bool gotoxy(int x, int y) { return false; }; + + virtual bool flush() { return false; }; friend bool conky::initialize_display_outputs(); friend bool conky::shutdown_display_outputs();