diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e53ce5ddd..593e4b220 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -106,6 +106,16 @@ set(conky_sources luamm.hh data-source.cc data-source.hh + display-output.cc + display-output.hh + display-console.cc + display-console.hh + display-ncurses.cc + display-ncurses.hh + display-http.cc + display-http.hh + display-x11.cc + display-x11.hh lua-config.cc lua-config.hh setting.cc diff --git a/src/conky.cc b/src/conky.cc index d2eb170e1..09de830c2 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -121,6 +121,7 @@ #include "weather.h" #endif /* BUILD_WEATHER_METAR */ +#include "display-output.hh" #include "lua-config.hh" #include "setting.hh" @@ -2911,6 +2912,10 @@ void initialisation(int argc, char **argv) { tmpstring2 = new char[text_buffer_size.get(*state)]; memset(tmpstring2, 0, text_buffer_size.get(*state)); + if (!conky::initialize_display_outputs()) { + CRIT_ERR(nullptr, nullptr, "initialize_display_outputs() failed."); + } + #ifdef BUILD_X11 X11_create_window(); #endif /* BUILD_X11 */ diff --git a/src/display-console.cc b/src/display-console.cc new file mode 100644 index 000000000..5377bc8e0 --- /dev/null +++ b/src/display-console.cc @@ -0,0 +1,60 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include + +#include "conky.h" +#include "display-console.hh" + +#include +#include +#include + +namespace conky { +namespace { + +conky::display_output_console console_output("console"); + +} // namespace + +namespace priv {} // namespace priv + +display_output_console::display_output_console(const std::string &name_) + : display_output_base(name_) { + // lowest priority, it's a fallback + priority = 0; +} + +bool display_output_console::detect() { + if (out_to_stdout.get(*state) || out_to_stderr.get(*state)) { + std::cerr << "Display output '" << name << "' enabled in config." + << std::endl; + return true; + } + return false; +} + +bool display_output_console::initialize() { return true; } + +bool display_output_console::shutdown() { return true; } + +} // namespace conky diff --git a/src/display-console.hh b/src/display-console.hh new file mode 100644 index 000000000..8d8e37b07 --- /dev/null +++ b/src/display-console.hh @@ -0,0 +1,55 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef DISPLAY_CONSOLE_HH +#define DISPLAY_CONSOLE_HH + +#include +#include +#include + +#include "display-output.hh" +#include "luamm.hh" + +namespace conky { + +/* + * A base class for console display output. + */ +class display_output_console : public display_output_base { + public: + explicit display_output_console(const std::string &name_); + + virtual ~display_output_console() {} + + // check if available and enabled in settings + virtual bool detect(); + // connect to DISPLAY and other stuff + virtual bool initialize(); + virtual bool shutdown(); + + // console-specific +}; + +} // namespace conky + +#endif /* DISPLAY_CONSOLE_HH */ diff --git a/src/display-http.cc b/src/display-http.cc new file mode 100644 index 000000000..e25bee63b --- /dev/null +++ b/src/display-http.cc @@ -0,0 +1,67 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include + +#include "conky.h" +#include "display-http.hh" + +#include +#include +#include + +namespace conky { +namespace { + +#ifdef BUILD_HTTP +conky::display_output_http http_output; +#else +conky::disabled_display_output http_output_disabled("http", "BUILD_HTTP"); +#endif + +} // namespace + +namespace priv {} // namespace priv + +#ifdef BUILD_HTTP + +display_output_http::display_output_http() : display_output_base("http") { + priority = 1; +} + +bool display_output_http::detect() { + /* TODO: + if (out_to_http.get(*state)) { + std::cerr << "Display output '" << name << "' enabled in config." << + std::endl; return true; + } + */ + return false; +} + +bool display_output_http::initialize() { return false; } + +bool display_output_http::shutdown() { return false; } + +#endif + +} // namespace conky diff --git a/src/display-http.hh b/src/display-http.hh new file mode 100644 index 000000000..fd635d7fe --- /dev/null +++ b/src/display-http.hh @@ -0,0 +1,55 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef DISPLAY_HTTP_HH +#define DISPLAY_HTTP_HH + +#include +#include +#include + +#include "display-output.hh" +#include "luamm.hh" + +namespace conky { + +/* + * A base class for HTTP display output. + */ +class display_output_http : public display_output_base { + public: + explicit display_output_http(); + + virtual ~display_output_http() {} + + // check if available and enabled in settings + virtual bool detect(); + // connect to DISPLAY and other stuff + virtual bool initialize(); + virtual bool shutdown(); + + // HTTP-specific +}; + +} // namespace conky + +#endif /* DISPLAY_HTTP_HH */ diff --git a/src/display-ncurses.cc b/src/display-ncurses.cc new file mode 100644 index 000000000..744362ee7 --- /dev/null +++ b/src/display-ncurses.cc @@ -0,0 +1,69 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include + +#include "conky.h" +#include "display-ncurses.hh" +#include "nc.h" + +#include +#include +#include + +namespace conky { +namespace { + +#ifdef BUILD_NCURSES +conky::display_output_ncurses ncurses_output; +#else +conky::disabled_display_output ncurses_output_disabled("ncurses", + "BUILD_NCURSES"); +#endif + +} // namespace + +namespace priv {} // namespace priv + +#ifdef BUILD_NCURSES + +display_output_ncurses::display_output_ncurses() + : display_output_console("ncurses") { + priority = 1; +} + +bool display_output_ncurses::detect() { + if (out_to_ncurses.get(*state)) { + std::cerr << "Display output '" << name << "' enabled in config." + << std::endl; + return true; + } + return false; +} + +bool display_output_ncurses::initialize() { return false; } + +bool display_output_ncurses::shutdown() { return false; } + +#endif /* BUILD_NCURSES */ + +} // namespace conky diff --git a/src/display-ncurses.hh b/src/display-ncurses.hh new file mode 100644 index 000000000..c0d23517e --- /dev/null +++ b/src/display-ncurses.hh @@ -0,0 +1,55 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef DISPLAY_NCURSES_HH +#define DISPLAY_NCURSES_HH + +#include +#include +#include + +#include "display-console.hh" +#include "luamm.hh" + +namespace conky { + +/* + * A base class for ncurses display output. + */ +class display_output_ncurses : public display_output_console { + public: + explicit display_output_ncurses(); + + virtual ~display_output_ncurses() {} + + // check if available and enabled in settings + virtual bool detect(); + // connect to DISPLAY and other stuff + virtual bool initialize(); + virtual bool shutdown(); + + // ncurses-specific +}; + +} // namespace conky + +#endif /* DISPLAY_NCURSES_HH */ diff --git a/src/display-output.cc b/src/display-output.cc new file mode 100644 index 000000000..4e304bd30 --- /dev/null +++ b/src/display-output.cc @@ -0,0 +1,120 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include + +#include "display-output.hh" + +#include +#include +#include + +namespace conky { +namespace { + +typedef std::unordered_map + display_outputs_t; + +/* + * We cannot construct this object statically, because order of object + * construction in different modules is not defined, so register_source could be + * called before this object is constructed. Therefore, we create it on the + * first call to register_source. + */ +display_outputs_t *display_outputs; + +} // namespace + +/* + * The selected and active display output. + * XXX: do we want to support multiple outputs??? + */ +display_output_base *active_display_output = nullptr; + +namespace priv { +void do_register_display_output(const std::string &name, + display_output_base *output) { + struct display_output_constructor { + display_output_constructor() { display_outputs = new display_outputs_t(); } + ~display_output_constructor() { + delete display_outputs; + display_outputs = nullptr; + } + }; + static display_output_constructor constructor; + + bool inserted = display_outputs->insert({name, output}).second; + if (!inserted) { + throw std::logic_error("Display output with name '" + name + + "' already registered"); + } +} + +} // namespace priv + +display_output_base::display_output_base(const std::string &name_) + : name(name_), is_active(false), priority(-1) { + priv::do_register_display_output(name, this); +} + +disabled_display_output::disabled_display_output(const std::string &name, + const std::string &define) + : display_output_base(name) { + priority = -2; + // XXX some generic way of reporting errors? NORM_ERR? + std::cerr << "Support for display output '" << name + << "' has been disabled during compilation. Please recompile with '" + << define << "'" << std::endl; +} + +bool initialize_display_outputs() { + std::vector outputs; + outputs.reserve(display_outputs->size()); + + for (auto &output : *display_outputs) { outputs.push_back(output.second); } + sort(outputs.begin(), outputs.end(), &display_output_base::priority_compare); + + for (auto output : outputs) { + std::cerr << "Testing display output '" << output->name << "'... " + << std::endl; + if (output->detect()) { + std::cerr << "Detected display output '" << output->name << "'... " + << std::endl; + if (output->initialize()) { + std::cerr << "Initialized display output '" << output->name << "'... " + << std::endl; + output->is_active = true; + active_display_output = output; + return true; + } + } + } + std::cerr << "Unable to find a usable display output." << std::endl; + return true; // false; +} + +bool shutdown_display_outputs() { + if (active_display_output) { return active_display_output->shutdown(); } + return false; +} + +} // namespace conky diff --git a/src/display-output.hh b/src/display-output.hh new file mode 100644 index 000000000..6b36d249a --- /dev/null +++ b/src/display-output.hh @@ -0,0 +1,95 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef DISPLAY_OUTPUT_HH +#define DISPLAY_OUTPUT_HH + +#include +#include +#include + +#include "luamm.hh" + +namespace conky { + +bool initialize_display_outputs(); + +bool shutdown_display_outputs(); + +/* + * A base class for all display outputs. + * API consists of two functions: + * - get_number should return numeric representation of the data (if available). + * This can then be used when drawing graphs, bars, ... The default + * implementation returns NaN. + * - get_text should return textual representation of the data. This is used + * when simple displaying the value of the data source. The default + * implementation converts get_number() to a string, but you can override to + * return anything (e.g. add units) + */ +class display_output_base { + private: + // copying is a REALLY bad idea + display_output_base(const display_output_base &) = delete; + display_output_base &operator=(const display_output_base &) = delete; + + public: + const std::string name; + bool is_active; + int priority; + + explicit display_output_base(const std::string &name_); + + virtual ~display_output_base() {} + + static bool priority_compare(const display_output_base *a, + const display_output_base *b) { + return a->priority > b->priority; + } + + // check if available and enabled in settings + virtual bool detect() { return false; }; + // connect to DISPLAY and other stuff + virtual bool initialize() { return false; }; + virtual bool shutdown() { return false; }; + + friend bool conky::initialize_display_outputs(); + friend bool conky::shutdown_display_outputs(); + + protected: + virtual bool active() { return is_active; }; +}; + +/* + * Use this to declare a display output that has been disabled during + * compilation. We can then print a nice error message telling the used which + * setting to enable. + */ +class disabled_display_output : public display_output_base { + public: + const std::string define; + disabled_display_output(const std::string &name, const std::string &define); +}; + +} // namespace conky + +#endif /* DISPLAY_OUTPUT_HH */ diff --git a/src/display-x11.cc b/src/display-x11.cc new file mode 100644 index 000000000..0fe353ccf --- /dev/null +++ b/src/display-x11.cc @@ -0,0 +1,65 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include + +#include "display-x11.hh" + +#include +#include +#include + +namespace conky { +namespace { + +#ifdef BUILD_X11 +conky::display_output_x11 x11_output; +#else +conky::disabled_display_output x11_output_disabled("x11", "BUILD_X11"); +#endif + +} // namespace + +namespace priv {} // namespace priv + +#ifdef BUILD_X11 + +display_output_x11::display_output_x11() : display_output_base("x11") { + priority = 2; +} + +bool display_output_x11::detect() { + if (out_to_x.get(*state)) { + std::cerr << "Display output '" << name << "' enabled in config." + << std::endl; + return true; + } + return false; +} + +bool display_output_x11::initialize() { return false; } + +bool display_output_x11::shutdown() { return false; } + +#endif /* BUILD_X11 */ + +} // namespace conky diff --git a/src/display-x11.hh b/src/display-x11.hh new file mode 100644 index 000000000..3c96b9d84 --- /dev/null +++ b/src/display-x11.hh @@ -0,0 +1,55 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2010 Pavel Labath et al. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef DISPLAY_X11_HH +#define DISPLAY_X11_HH + +#include +#include +#include + +#include "display-output.hh" +#include "luamm.hh" + +namespace conky { + +/* + * A base class for X11 display output. + */ +class display_output_x11 : public display_output_base { + public: + explicit display_output_x11(); + + virtual ~display_output_x11() {} + + // check if available and enabled in settings + virtual bool detect(); + // connect to DISPLAY and other stuff + virtual bool initialize(); + virtual bool shutdown(); + + // X11-specific +}; + +} // namespace conky + +#endif /* DISPLAY_X11_HH */ diff --git a/src/main.cc b/src/main.cc index cba50987d..65d375aae 100644 --- a/src/main.cc +++ b/src/main.cc @@ -33,6 +33,7 @@ #include "build.h" #include "config.h" #include "conky.h" +#include "display-output.hh" #include "lua-config.hh" #ifdef BUILD_X11 @@ -368,6 +369,8 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } + conky::shutdown_display_outputs(); + #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) kvm_close(kd); #endif