From 59f1e6540222a9ecb4321abc159df6bf85361cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Fri, 19 Oct 2018 02:58:38 +0200 Subject: [PATCH] move file output to a separate display-file --- src/CMakeLists.txt | 1 + src/conky.cc | 39 ++------------- src/display-file.cc | 113 ++++++++++++++++++++++++++++++++++++++++++++ src/display-file.hh | 62 ++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 35 deletions(-) create mode 100644 src/display-file.cc create mode 100644 src/display-file.hh diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b7fb53f0e..22b3ac0625 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -70,6 +70,7 @@ set(conky_sources data-source.cc data-source.hh display-output.cc display-output.hh display-console.cc display-console.hh + display-file.cc display-file.hh display-ncurses.cc display-ncurses.hh display-http.cc display-http.hh display-x11.cc display-x11.hh diff --git a/src/conky.cc b/src/conky.cc index 274b45be24..691c645329 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -494,15 +494,6 @@ conky::range_config_setting net_avg_samples("net_avg_samples", 1, 14, 2, conky::range_config_setting diskio_avg_samples("diskio_avg_samples", 1, 14, 2, true); -/* filenames for output */ -static conky::simple_config_setting overwrite_file( - "overwrite_file", std::string(), true); -static FILE *overwrite_fpointer = nullptr; -static conky::simple_config_setting append_file("append_file", - std::string(), - true); -static FILE *append_fpointer = nullptr; - #ifdef BUILD_X11 static conky::simple_config_setting show_graph_scale("show_graph_scale", @@ -1211,12 +1202,6 @@ static void draw_string(const char *s) { if (s[0] == '\0') { return; } width_of_s = get_string_width(s); - if (draw_mode == FG && (overwrite_fpointer != nullptr)) { - fprintf(overwrite_fpointer, "%s\n", s); - } - if (draw_mode == FG && (append_fpointer != nullptr)) { - fprintf(append_fpointer, "%s\n", s); - } if (conky::active_display_outputs.size() && draw_mode == FG) for (auto output : conky::active_display_outputs) output->draw_string(s, width_of_s); @@ -1776,18 +1761,8 @@ static void draw_stuff() { #ifdef BUILD_IMLIB2 cimlib_render(text_start_x, text_start_y, window.width, window.height); #endif /* BUILD_IMLIB2 */ - if (static_cast(!overwrite_file.get(*state).empty()) != 0u) { - overwrite_fpointer = fopen(overwrite_file.get(*state).c_str(), "we"); - if (overwrite_fpointer == nullptr) { - NORM_ERR("Cannot overwrite '%s'", overwrite_file.get(*state).c_str()); - } - } - if (static_cast(!append_file.get(*state).empty()) != 0u) { - append_fpointer = fopen(append_file.get(*state).c_str(), "ae"); - if (append_fpointer == nullptr) { - NORM_ERR("Cannot append to '%s'", append_file.get(*state).c_str()); - } - } + for (auto output : display_outputs()) + output->begin_draw_stuff(); #ifdef BUILD_X11 llua_draw_pre_hook(); if (out_to_x.get(*state)) { @@ -1828,14 +1803,8 @@ static void draw_stuff() { if (out_to_x.get(*state)) { xpmdb_swap_buffers(); } #endif #endif /* BUILD_X11 && BUILD_XDBE */ - if (overwrite_fpointer != nullptr) { - fclose(overwrite_fpointer); - overwrite_fpointer = nullptr; - } - if (append_fpointer != nullptr) { - fclose(append_fpointer); - append_fpointer = nullptr; - } + for (auto output : display_outputs()) + output->end_draw_stuff(); } #ifdef BUILD_X11 diff --git a/src/display-file.cc b/src/display-file.cc new file mode 100644 index 0000000000..38cb9ab634 --- /dev/null +++ b/src/display-file.cc @@ -0,0 +1,113 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2018 François Revol 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-file.hh" + +#include +#include +#include + +/* filenames for output */ +static conky::simple_config_setting overwrite_file( + "overwrite_file", std::string(), true); +static FILE *overwrite_fpointer = nullptr; +static conky::simple_config_setting append_file("append_file", + std::string(), + true); +static FILE *append_fpointer = nullptr; + +namespace conky { +namespace { + +conky::display_output_file file_output("file"); + +} // namespace + +namespace priv { + + +} // namespace priv + +display_output_file::display_output_file(const std::string &name_) + : display_output_base(name_) { + // lowest priority, it's a fallback + priority = 0; +} + +bool display_output_file::detect() { + if (static_cast(!overwrite_file.get(*state).empty()) != 0u || + static_cast(!append_file.get(*state).empty()) != 0u) { + std::cerr << "Display output '" << name << "' enabled in config." << std::endl; + return true; + } + return false; +} + +bool display_output_file::initialize() { + return true; +} + +bool display_output_file::shutdown() { + return true; +} + +void display_output_file::draw_string(const char *s, int w) { + if (overwrite_fpointer != nullptr) { + fprintf(overwrite_fpointer, "%s\n", s); + } + if (append_fpointer != nullptr) { + fprintf(append_fpointer, "%s\n", s); + } +} + +void display_output_x11::begin_draw_stuff() { + if (static_cast(!overwrite_file.get(*state).empty()) != 0u) { + overwrite_fpointer = fopen(overwrite_file.get(*state).c_str(), "we"); + if (overwrite_fpointer == nullptr) { + NORM_ERR("Cannot overwrite '%s'", overwrite_file.get(*state).c_str()); + } + } + if (static_cast(!append_file.get(*state).empty()) != 0u) { + append_fpointer = fopen(append_file.get(*state).c_str(), "ae"); + if (append_fpointer == nullptr) { + NORM_ERR("Cannot append to '%s'", append_file.get(*state).c_str()); + } + } +} + +void display_output_x11::end_draw_stuff() { + if (overwrite_fpointer != nullptr) { + fclose(overwrite_fpointer); + overwrite_fpointer = nullptr; + } + if (append_fpointer != nullptr) { + fclose(append_fpointer); + append_fpointer = nullptr; + } +} + +} // namespace conky + diff --git a/src/display-file.hh b/src/display-file.hh new file mode 100644 index 0000000000..948e5f47ae --- /dev/null +++ b/src/display-file.hh @@ -0,0 +1,62 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2018 François Revol 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_FILE_HH +#define DISPLAY_FILE_HH + +#include +#include +#include + +#include "luamm.hh" +#include "display-output.hh" + +namespace conky { + +/* + * A base class for file display output. + */ +class display_output_file : public display_output_base { + + public: + + explicit display_output_file(const std::string &name_); + + virtual ~display_output_file() {} + + // check if available and enabled in settings + virtual bool detect(); + // connect to DISPLAY and other stuff + virtual bool initialize(); + virtual bool shutdown(); + + virtual void draw_string(const char *s, int w); + + virtual void begin_draw_stuff(); + virtual void end_draw_stuff(); + + // file-specific +}; + +} // namespace conky + +#endif /* DISPLAY_FILE_HH */