From e97be17f7f0269423b1022e792f1c9a9a341c3c5 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 | 2 + src/conky.cc | 37 +--------------- src/display-file.cc | 106 ++++++++++++++++++++++++++++++++++++++++++++ src/display-file.hh | 60 +++++++++++++++++++++++++ 4 files changed, 170 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 593e4b22..11e55a69 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -110,6 +110,8 @@ set(conky_sources display-output.hh display-console.cc display-console.hh + display-file.cc + display-file.hh display-ncurses.cc display-ncurses.hh display-http.cc diff --git a/src/conky.cc b/src/conky.cc index 64bd23f4..c952c9bb 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -310,15 +310,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", @@ -1073,12 +1064,6 @@ static void draw_string(const char *s) { #ifdef BUILD_X11 width_of_s = get_string_width(s); #endif /* BUILD_X11 */ - 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); @@ -1672,18 +1657,7 @@ static void draw_stuff() { cimlib_render(text_start_x, text_start_y, window.width, window.height); #endif /* BUILD_IMLIB2 */ #endif /* BUILD_X11 */ - 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)) { @@ -1723,14 +1697,7 @@ 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 00000000..33c1ff55 --- /dev/null +++ b/src/display-file.cc @@ -0,0 +1,106 @@ +/* + * + * Conky, a system monitor, based on torsmo + * + * Please see COPYING for details + * + * Copyright (C) 2018 François Revol et al. + * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen + * Copyright (c) 2005-2019 Brenden Matthews, Philip Kovacs, et. al. + * (see AUTHORS) + * All rights reserved. + * + * 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-file.hh" +#include "nc.h" + +#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_file::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_file::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 00000000..bc643729 --- /dev/null +++ b/src/display-file.hh @@ -0,0 +1,60 @@ +/* + * + * 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 "display-output.hh" +#include "luamm.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 */