diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4e490ac0c1..6b7fb53f0e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -68,6 +68,11 @@ set(conky_sources user.cc user.h luamm.cc 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 setting.hh llua.cc llua.h diff --git a/src/conky.cc b/src/conky.cc index 040aab96a0..68741aa419 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -118,6 +118,7 @@ #include "lua-config.hh" #include "setting.hh" +#include "display-output.hh" /* check for OS and include appropriate headers */ #if defined(__linux__) @@ -3039,6 +3040,10 @@ void initialisation(int argc, char **argv) { tmpstring2 = static_cast(malloc(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 */ @@ -3169,6 +3174,8 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } + conky::shutdown_display_outputs(); + #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) kvm_close(kd); #endif diff --git a/src/display-console.cc b/src/display-console.cc new file mode 100644 index 0000000000..de683d3a6d --- /dev/null +++ b/src/display-console.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-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 0000000000..4b570b6c9b --- /dev/null +++ b/src/display-console.hh @@ -0,0 +1,57 @@ +/* + * + * 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 "luamm.hh" +#include "display-output.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 0000000000..2e0d828ea9 --- /dev/null +++ b/src/display-http.cc @@ -0,0 +1,76 @@ +/* + * + * 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 0000000000..8b626a3c47 --- /dev/null +++ b/src/display-http.hh @@ -0,0 +1,57 @@ +/* + * + * 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 "luamm.hh" +#include "display-output.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 0000000000..d21dec5108 --- /dev/null +++ b/src/display-ncurses.cc @@ -0,0 +1,75 @@ +/* + * + * 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 "nc.h" +#include "display-ncurses.hh" + +#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 0000000000..6660c190dd --- /dev/null +++ b/src/display-ncurses.hh @@ -0,0 +1,57 @@ +/* + * + * 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 "luamm.hh" +#include "display-console.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 0000000000..f6c0417fe9 --- /dev/null +++ b/src/display-output.cc @@ -0,0 +1,127 @@ +/* + * + * 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 (not 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 0000000000..159983797a --- /dev/null +++ b/src/display-output.hh @@ -0,0 +1,96 @@ +/* + * + * 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 0000000000..6908f26e07 --- /dev/null +++ b/src/display-x11.cc @@ -0,0 +1,73 @@ +/* + * + * 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 0000000000..3c3c10a9c7 --- /dev/null +++ b/src/display-x11.hh @@ -0,0 +1,57 @@ +/* + * + * 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 "luamm.hh" +#include "display-output.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 */