diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f78e9e8c..364614c2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -174,6 +174,10 @@ if(BUILD_BUILTIN_CONFIG) set(optional_sources ${optional_sources} conf_cookie.cc) endif(BUILD_BUILTIN_CONFIG) +if(BUILD_NCURSES) + set(optional_sources ${optional_sources} nc.cc) +endif(BUILD_NCURSES) + add_executable(conky ${conky_sources} ${optional_sources}) target_link_libraries(conky ${conky_libs}) diff --git a/src/conky.cc b/src/conky.cc index d8ddbdb8..9704904d 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -67,12 +67,6 @@ #include #include #include -#ifdef BUILD_NCURSES -#include -#ifdef LEAKFREE_NCURSES -#include "nc.h" -#endif -#endif #ifdef BUILD_WEATHER_XOAP #include #endif /* BUILD_WEATHER_XOAP */ @@ -94,6 +88,7 @@ #endif /* BUILD_LUA */ #include "logging.h" #include "mail.h" +#include "nc.h" #include "net_stat.h" #include "temphelper.h" #include "template.h" @@ -1141,7 +1136,7 @@ static inline void set_foreground_color(long c) } #endif /* BUILD_X11 */ #ifdef BUILD_NCURSES - if (output_methods & TO_NCURSES) { + if (out_to_ncurses.get(*state)) { attron(COLOR_PAIR(c)); } #endif /* BUILD_NCURSES */ @@ -1183,7 +1178,7 @@ static void draw_string(const char *s) fprintf(append_fpointer, "%s\n", s_with_newlines); } #ifdef BUILD_NCURSES - if ((output_methods & TO_NCURSES) && draw_mode == FG) { + if (out_to_ncurses.get(*state) && draw_mode == FG) { printw("%s", s_with_newlines); } #endif @@ -1710,7 +1705,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) #endif /* BUILD_X11 */ draw_string(s); #ifdef BUILD_NCURSES - if (output_methods & TO_NCURSES) { + if (out_to_ncurses.get(*state)) { printw("\n"); } #endif /* BUILD_NCURSES */ @@ -1733,7 +1728,7 @@ static int draw_line(char *s, int special_index) } #endif /* BUILD_X11 */ #ifdef BUILD_NCURSES - if (output_methods & TO_NCURSES) { + if (out_to_ncurses.get(*state)) { return draw_each_line_inner(s, special_index, -1); } #endif /* BUILD_NCURSES */ @@ -2252,7 +2247,7 @@ static void main_loop(void) update_text(); draw_stuff(); #ifdef BUILD_NCURSES - if(output_methods & TO_NCURSES) { + if(out_to_ncurses.get(*state)) { refresh(); clear(); } @@ -2418,11 +2413,6 @@ void clean_up(void *memtofree1, void* memtofree2) { free_update_callbacks(); -#ifdef BUILD_NCURSES - if(output_methods & TO_NCURSES) { - endwin(); - } -#endif conftree_empty(currentconffile); currentconffile = NULL; free_and_zero(memtofree1); @@ -2487,21 +2477,6 @@ void clean_up(void *memtofree1, void* memtofree2) state.reset(); } -static bool string_to_bool(const char *s) -{ - if (!s) { - // Assumes an option without a true/false means true - return true; - } else if (strcasecmp(s, "yes") == EQUAL) { - return true; - } else if (strcasecmp(s, "true") == EQUAL) { - return true; - } else if (strcasecmp(s, "1") == EQUAL) { - return true; - } - return false; -} - static void set_default_configurations(void) { #ifdef BUILD_MPD @@ -2947,15 +2922,6 @@ char load_config_file(const char *f) CONF("max_text_width") { max_text_width = atoi(value); } -#ifdef BUILD_NCURSES - CONF("out_to_ncurses") { - if(string_to_bool(value)) { - initscr(); - start_color(); - output_methods |= TO_NCURSES; - } - } -#endif CONF("overwrite_file") { free_and_zero(overwrite_file); if (overwrite_works(value)) { @@ -3265,14 +3231,14 @@ char load_config_file(const char *f) if(out_to_x.get(*state)) { current_text_color = default_color.get(*state); } - if (out_to_x.get(*state) && (output_methods & TO_NCURSES)) { + if (out_to_x.get(*state) && out_to_ncurses.get(*state)) { NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off"); - output_methods &= ~TO_NCURSES; - endwin(); + state->pushboolean(false); + out_to_ncurses.lua_set(*state); } #endif /* BUILD_X11 */ if ((out_to_stdout.get(*state) || out_to_stderr.get(*state)) - && (output_methods & TO_NCURSES)) { + && out_to_ncurses.get(*state)) { NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones"); // XXX: this will need some rethinking state->pushboolean(false); diff --git a/src/conky.h b/src/conky.h index 41eeacea..08cd2adb 100644 --- a/src/conky.h +++ b/src/conky.h @@ -324,9 +324,6 @@ extern std::string current_config; #define TO_STDERR 4 #define OVERWRITE_FILE 8 #define APPEND_FILE 16 -#ifdef BUILD_NCURSES -#define TO_NCURSES 32 -#endif /* BUILD_NCURSES */ enum x_initialiser_state { NO = 0, YES = 1, diff --git a/src/core.cc b/src/core.cc index 3ab6b22e..e2281ab4 100644 --- a/src/core.cc +++ b/src/core.cc @@ -63,6 +63,7 @@ #include "mixer.h" #include "mail.h" #include "mboxscan.h" +#include "nc.h" #include "net_stat.h" #ifdef BUILD_NVIDIA #include "nvidia.h" @@ -78,10 +79,6 @@ #include "user.h" #include "users.h" -#ifdef BUILD_NCURSES -#include -#endif - /* check for OS and include appropriate headers */ #if defined(__linux__) #include "linux.h" @@ -505,7 +502,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long } #endif /* BUILD_X11 */ #ifdef BUILD_NCURSES - if (output_methods & TO_NCURSES) { + if (out_to_ncurses.get(*state)) { obj->data.l = COLOR_WHITE; if(arg) { if(strcasecmp(arg, "red") == 0) { diff --git a/src/nc.cc b/src/nc.cc new file mode 100644 index 00000000..27cd3cd4 --- /dev/null +++ b/src/nc.cc @@ -0,0 +1,35 @@ +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=cpp + */ + +#include + +#include "nc.h" + +namespace priv { + void out_to_ncurses_setting::lua_setter(lua::state &l, bool init) + { + lua::stack_sentry s(l, -2); + + Base::lua_setter(l, init); + + if(init && do_convert(l, -1).first) { + initscr(); + start_color(); + } + + ++s; + } + + void out_to_ncurses_setting::cleanup(lua::state &l) + { + lua::stack_sentry s(l, -1); + + if(do_convert(l, -1).first) + endwin(); + + l.pop(); + } +} + +priv::out_to_ncurses_setting out_to_ncurses; diff --git a/src/nc.h b/src/nc.h index 5e04af69..ba9e87f7 100644 --- a/src/nc.h +++ b/src/nc.h @@ -2,13 +2,34 @@ * vim: ts=4 sw=4 noet ai cindent syntax=cpp */ -#ifndef CONKY_NC_H +#if defined(BUILD_NCURSES) && !defined(CONKY_NC_H) #define CONKY_NC_H +#include + +#include "setting.hh" + +#ifdef LEAKFREE_NCURSES extern "C" { - void _nc_free_and_exit(int); +} +#endif +namespace priv { + class out_to_ncurses_setting: public conky::simple_config_setting { + typedef conky::simple_config_setting Base; + + protected: + virtual void lua_setter(lua::state &l, bool init); + virtual void cleanup(lua::state &l); + + public: + out_to_ncurses_setting() + : Base("out_to_ncurses", false, false) + {} + }; } +extern priv::out_to_ncurses_setting out_to_ncurses; + #endif /* CONKY_NC_H */ diff --git a/src/specials.cc b/src/specials.cc index 67324451..1a4cfaeb 100644 --- a/src/specials.cc +++ b/src/specials.cc @@ -33,6 +33,7 @@ #include "fonts.h" #endif /* BUILD_X11 */ #include "logging.h" +#include "nc.h" #include "specials.h" #include #ifdef HAVE_SYS_PARAM_H @@ -484,7 +485,7 @@ void new_fg(struct text_object *obj, char *p, int p_max_size) new_special(p, FG)->arg = obj->data.l; #endif /* BUILD_X11 */ #ifdef BUILD_NCURSES - if (output_methods & TO_NCURSES) + if (out_to_ncurses.get(*state)) new_special(p, FG)->arg = obj->data.l; #endif /* BUILD_NCURSES */ UNUSED(obj);