Skip to content

Commit

Permalink
move file output to a separate display-file
Browse files Browse the repository at this point in the history
  • Loading branch information
mmuman committed Oct 19, 2018
1 parent 6c7d88f commit 59f1e65
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 4 additions & 35 deletions src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,6 @@ conky::range_config_setting<int> net_avg_samples("net_avg_samples", 1, 14, 2,
conky::range_config_setting<int> diskio_avg_samples("diskio_avg_samples", 1, 14,
2, true);

/* filenames for output */
static conky::simple_config_setting<std::string> overwrite_file(
"overwrite_file", std::string(), true);
static FILE *overwrite_fpointer = nullptr;
static conky::simple_config_setting<std::string> append_file("append_file",
std::string(),
true);
static FILE *append_fpointer = nullptr;

#ifdef BUILD_X11

static conky::simple_config_setting<bool> show_graph_scale("show_graph_scale",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<unsigned int>(!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<unsigned int>(!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)) {
Expand Down Expand Up @@ -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
Expand Down
113 changes: 113 additions & 0 deletions src/display-file.cc
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

#include <config.h>

#include "conky.h"
#include "nc.h"
#include "display-file.hh"

#include <iostream>
#include <sstream>
#include <unordered_map>

/* filenames for output */
static conky::simple_config_setting<std::string> overwrite_file(
"overwrite_file", std::string(), true);
static FILE *overwrite_fpointer = nullptr;
static conky::simple_config_setting<std::string> 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<unsigned int>(!overwrite_file.get(*state).empty()) != 0u ||
static_cast<unsigned int>(!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<unsigned int>(!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<unsigned int>(!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

62 changes: 62 additions & 0 deletions src/display-file.hh
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

#ifndef DISPLAY_FILE_HH
#define DISPLAY_FILE_HH

#include <limits>
#include <string>
#include <type_traits>

#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 */

0 comments on commit 59f1e65

Please sign in to comment.