From 7c8d486b9ff2f5b8fbbafdc0c3342fdac38f14c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Fri, 5 Oct 2018 04:47:27 +0200 Subject: [PATCH] Move HTTP output code from conky.cc to display-http --- src/conky.cc | 113 ++------------------------------ src/display-http.cc | 146 +++++++++++++++++++++++++++++++++++++++--- src/display-http.hh | 8 +++ src/display-output.hh | 11 ++++ 4 files changed, 162 insertions(+), 116 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 09de830c..66d95cce 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -135,9 +135,6 @@ #elif defined(__OpenBSD__) #include "openbsd.h" #endif /* __OpenBSD__ */ -#ifdef BUILD_HTTP -#include -#endif /* BUILD_HTTP */ #ifdef BUILD_HSV_GRADIENT #include "hsv_gradient.h" @@ -328,63 +325,6 @@ static conky::simple_config_setting append_file("append_file", true); static FILE *append_fpointer = nullptr; -#ifdef BUILD_HTTP -#ifdef MHD_YES -/* older API */ -#define MHD_Result int -#endif /* MHD_YES */ -std::string webpage; -struct MHD_Daemon *httpd; -static conky::simple_config_setting http_refresh("http_refresh", false, - true); - -MHD_Result sendanswer(void *cls, struct MHD_Connection *connection, - const char *url, const char *method, const char *version, - const char *upload_data, size_t *upload_data_size, - void **con_cls) { - struct MHD_Response *response = MHD_create_response_from_buffer( - webpage.length(), (void *)webpage.c_str(), MHD_RESPMEM_PERSISTENT); - MHD_Result ret = MHD_queue_response(connection, MHD_HTTP_OK, response); - MHD_destroy_response(response); - if (cls || url || method || version || upload_data || upload_data_size || - con_cls) {} // make compiler happy - return ret; -} - -class out_to_http_setting : public conky::simple_config_setting { - typedef conky::simple_config_setting Base; - - protected: - virtual void lua_setter(lua::state &l, bool init) { - lua::stack_sentry s(l, -2); - - Base::lua_setter(l, init); - - if (init && do_convert(l, -1).first) { - httpd = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, HTTPPORT, nullptr, - NULL, &sendanswer, nullptr, MHD_OPTION_END); - } - - ++s; - } - - virtual void cleanup(lua::state &l) { - lua::stack_sentry s(l, -1); - - if (do_convert(l, -1).first) { - MHD_stop_daemon(httpd); - httpd = nullptr; - } - - l.pop(); - } - - public: - out_to_http_setting() : Base("out_to_http", false, false) {} -}; -static out_to_http_setting out_to_http; -#endif /* BUILD_HTTP */ - #ifdef BUILD_X11 static conky::simple_config_setting show_graph_scale("show_graph_scale", @@ -1125,19 +1065,6 @@ static inline void set_foreground_color(long c) { UNUSED(c); } -std::string string_replace_all(std::string original, const std::string &oldpart, - const std::string &newpart, - std::string::size_type start) { - std::string::size_type i = start; - int oldpartlen = oldpart.length(); - while (1) { - i = original.find(oldpart, i); - if (i == std::string::npos) { break; } - original.replace(i, oldpartlen, newpart); - } - return original; -} - static void draw_string(const char *s) { int i; int i2; @@ -1171,16 +1098,8 @@ static void draw_string(const char *s) { #ifdef BUILD_NCURSES if (out_to_ncurses.get(*state) && draw_mode == FG) { printw("%s", s); } #endif /* BUILD_NCURSES */ -#ifdef BUILD_HTTP - if (out_to_http.get(*state) && draw_mode == FG) { - std::string::size_type origlen = webpage.length(); - webpage.append(s); - webpage = string_replace_all(webpage, "\n", "
", origlen); - webpage = string_replace_all(webpage, " ", "  ", origlen); - webpage = string_replace_all(webpage, "  ", "  ", origlen); - webpage.append("
"); - } -#endif /* BUILD_HTTP */ + if (conky::active_display_output != nullptr && draw_mode == FG) + conky::active_display_output->draw_string(s, width_of_s); int tbs = text_buffer_size.get(*state); memset(tmpstring1, 0, tbs); memset(tmpstring2, 0, tbs); @@ -1738,27 +1657,8 @@ static int draw_line(char *s, int special_index) { } static void draw_text() { -#ifdef BUILD_HTTP -#define WEBPAGE_START1 \ - "\n" -#define WEBPAGE_START2 \ - "Conky

" -#define WEBPAGE_END "

" - if (out_to_http.get(*state)) { - webpage = WEBPAGE_START1; - if (http_refresh.get(*state)) { - webpage.append(""); - } - webpage.append(WEBPAGE_START2); - } -#endif /* BUILD_HTTP */ + if (conky::active_display_output) + conky::active_display_output->begin_draw_text(); #ifdef BUILD_X11 if (out_to_x.get(*state)) { cur_y = text_start_y; @@ -1793,9 +1693,8 @@ static void draw_text() { attron(COLOR_PAIR(COLOR_WHITE)); #endif /* BUILD_NCURSES */ for_each_line(text_buffer, draw_line); -#ifdef BUILD_HTTP - if (out_to_http.get(*state)) { webpage.append(WEBPAGE_END); } -#endif /* BUILD_HTTP */ + if (conky::active_display_output) + conky::active_display_output->end_draw_text(); } static void draw_stuff() { diff --git a/src/display-http.cc b/src/display-http.cc index e25bee63..6e875cb6 100644 --- a/src/display-http.cc +++ b/src/display-http.cc @@ -4,7 +4,11 @@ * * Please see COPYING for details * - * Copyright (C) 2010 Pavel Labath et al. + * 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 @@ -29,6 +33,10 @@ #include #include +#ifdef BUILD_HTTP +#include +#endif /* BUILD_HTTP */ + namespace conky { namespace { @@ -40,27 +48,147 @@ conky::disabled_display_output http_output_disabled("http", "BUILD_HTTP"); } // namespace -namespace priv {} // namespace priv +// TODO: cleanup namespace +// namespace priv { + +#ifdef BUILD_HTTP +#ifdef MHD_YES +/* older API */ +#define MHD_Result int +#endif /* MHD_YES */ +std::string webpage; +struct MHD_Daemon *httpd; +static conky::simple_config_setting http_refresh("http_refresh", false, + true); + +MHD_Result sendanswer(void *cls, struct MHD_Connection *connection, + const char *url, const char *method, const char *version, + const char *upload_data, size_t *upload_data_size, + void **con_cls) { + struct MHD_Response *response = MHD_create_response_from_buffer( + webpage.length(), (void *)webpage.c_str(), MHD_RESPMEM_PERSISTENT); + MHD_Result ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + if (cls || url || method || version || upload_data || upload_data_size || + con_cls) {} // make compiler happy + return ret; +} + +class out_to_http_setting : public conky::simple_config_setting { + typedef conky::simple_config_setting Base; + + protected: + virtual void lua_setter(lua::state &l, bool init) { + lua::stack_sentry s(l, -2); + + Base::lua_setter(l, init); + + if (init && do_convert(l, -1).first) { + httpd = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, HTTPPORT, nullptr, + NULL, &sendanswer, nullptr, MHD_OPTION_END); + } + + ++s; + } + + virtual void cleanup(lua::state &l) { + lua::stack_sentry s(l, -1); + + if (do_convert(l, -1).first) { + MHD_stop_daemon(httpd); + httpd = nullptr; + } + + l.pop(); + } + + public: + out_to_http_setting() : Base("out_to_http", false, false) {} +}; +static out_to_http_setting out_to_http; + +std::string string_replace_all(std::string original, const std::string &oldpart, + const std::string &newpart, + std::string::size_type start) { + std::string::size_type i = start; + int oldpartlen = oldpart.length(); + while (1) { + i = original.find(oldpart, i); + if (i == std::string::npos) { break; } + original.replace(i, oldpartlen, newpart); + } + return original; +} + +#endif /* BUILD_HTTP */ + +//} // namespace priv #ifdef BUILD_HTTP display_output_http::display_output_http() : display_output_base("http") { priority = 1; + httpd = NULL; } bool display_output_http::detect() { - /* TODO: - if (out_to_http.get(*state)) { - std::cerr << "Display output '" << name << "' enabled in config." << - std::endl; return true; + if (/*priv::*/ 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::initialize() { + if (/*priv::*/ out_to_http.get(*state)) { + is_active = true; + return true; + } + return false; +} -bool display_output_http::shutdown() { return false; } +bool display_output_http::shutdown() { return true; } + +bool display_output_http::begin_draw_text() { +#ifdef BUILD_HTTP +#define WEBPAGE_START1 \ + "\n" +#define WEBPAGE_START2 \ + "Conky

" +#define WEBPAGE_END "

" + if (out_to_http.get(*state)) { + webpage = WEBPAGE_START1; + if (http_refresh.get(*state)) { + webpage.append(""); + } + webpage.append(WEBPAGE_START2); + } +#endif /* BUILD_HTTP */ + return true; +} + +bool display_output_http::end_draw_text() { + webpage.append(WEBPAGE_END); + return true; +} + +bool display_output_http::draw_string(const char *s, int w) { + std::string::size_type origlen = webpage.length(); + webpage.append(s); + webpage = string_replace_all(webpage, "\n", "
", origlen); + webpage = string_replace_all(webpage, " ", "  ", origlen); + webpage = string_replace_all(webpage, "  ", "  ", origlen); + webpage.append("
"); + return true; +} #endif diff --git a/src/display-http.hh b/src/display-http.hh index fd635d7f..08a86cf1 100644 --- a/src/display-http.hh +++ b/src/display-http.hh @@ -47,7 +47,15 @@ class display_output_http : public display_output_base { virtual bool initialize(); virtual bool shutdown(); + // drawing primitives + virtual bool begin_draw_text(); + virtual bool end_draw_text(); + virtual bool draw_string(const char *s, int w); + // HTTP-specific + private: + // std::string webpage; + // struct MHD_Daemon *httpd; }; } // namespace conky diff --git a/src/display-output.hh b/src/display-output.hh index 6b36d249..8dc6cfcf 100644 --- a/src/display-output.hh +++ b/src/display-output.hh @@ -72,6 +72,11 @@ class display_output_base { virtual bool initialize() { return false; }; virtual bool shutdown() { return false; }; + // drawing primitives + virtual bool begin_draw_text() { return false; }; + virtual bool end_draw_text() { return false; }; + virtual bool draw_string(const char *s, int w) { return false; }; + friend bool conky::initialize_display_outputs(); friend bool conky::shutdown_display_outputs(); @@ -79,6 +84,12 @@ class display_output_base { virtual bool active() { return is_active; }; }; +/* + * The selected and active display output. + * XXX: do we want to support multiple outputs??? + */ +extern display_output_base *active_display_output; + /* * 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