mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-12-26 12:27:52 +00:00
Move HTTP output code from conky.cc to display-http
This commit is contained in:
parent
17ceb82079
commit
7c8d486b9f
113
src/conky.cc
113
src/conky.cc
@ -135,9 +135,6 @@
|
||||
#elif defined(__OpenBSD__)
|
||||
#include "openbsd.h"
|
||||
#endif /* __OpenBSD__ */
|
||||
#ifdef BUILD_HTTP
|
||||
#include <microhttpd.h>
|
||||
#endif /* BUILD_HTTP */
|
||||
|
||||
#ifdef BUILD_HSV_GRADIENT
|
||||
#include "hsv_gradient.h"
|
||||
@ -328,63 +325,6 @@ static conky::simple_config_setting<std::string> 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<bool> 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<bool> {
|
||||
typedef conky::simple_config_setting<bool> 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<bool> 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", "<br />", origlen);
|
||||
webpage = string_replace_all(webpage, " ", " ", origlen);
|
||||
webpage = string_replace_all(webpage, " ", " ", origlen);
|
||||
webpage.append("<br />");
|
||||
}
|
||||
#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 \
|
||||
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " \
|
||||
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html " \
|
||||
"xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta " \
|
||||
"http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\" />"
|
||||
#define WEBPAGE_START2 \
|
||||
"<title>Conky</title></head><body style=\"font-family: monospace\"><p>"
|
||||
#define WEBPAGE_END "</p></body></html>"
|
||||
if (out_to_http.get(*state)) {
|
||||
webpage = WEBPAGE_START1;
|
||||
if (http_refresh.get(*state)) {
|
||||
webpage.append("<meta http-equiv=\"refresh\" content=\"");
|
||||
std::stringstream update_interval_str;
|
||||
update_interval_str << update_interval.get(*state);
|
||||
webpage.append(update_interval_str.str());
|
||||
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() {
|
||||
|
@ -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 <sstream>
|
||||
#include <unordered_map>
|
||||
|
||||
#ifdef BUILD_HTTP
|
||||
#include <microhttpd.h>
|
||||
#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<bool> 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<bool> {
|
||||
typedef conky::simple_config_setting<bool> 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 \
|
||||
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " \
|
||||
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html " \
|
||||
"xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta " \
|
||||
"http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\" />"
|
||||
#define WEBPAGE_START2 \
|
||||
"<title>Conky</title></head><body style=\"font-family: monospace\"><p>"
|
||||
#define WEBPAGE_END "</p></body></html>"
|
||||
if (out_to_http.get(*state)) {
|
||||
webpage = WEBPAGE_START1;
|
||||
if (http_refresh.get(*state)) {
|
||||
webpage.append("<meta http-equiv=\"refresh\" content=\"");
|
||||
std::stringstream update_interval_str;
|
||||
update_interval_str << update_interval.get(*state);
|
||||
webpage.append(update_interval_str.str());
|
||||
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", "<br />", origlen);
|
||||
webpage = string_replace_all(webpage, " ", " ", origlen);
|
||||
webpage = string_replace_all(webpage, " ", " ", origlen);
|
||||
webpage.append("<br />");
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user