2018-10-05 01:48:59 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
2018-10-18 22:51:08 +00:00
|
|
|
* Copyright (C) 2018 François Revol et al.
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2024-02-22 13:33:31 +00:00
|
|
|
* Copyright (c) 2005-2024 Brenden Matthews, Philip Kovacs, et. al.
|
2018-10-18 22:51:08 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
2018-10-05 01:48:59 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2023-02-05 05:47:06 +00:00
|
|
|
#include "colours.h"
|
2023-02-24 13:43:15 +00:00
|
|
|
#include "conky.h"
|
2018-10-05 01:48:59 +00:00
|
|
|
#include "display-ncurses.hh"
|
2023-02-05 05:47:06 +00:00
|
|
|
#include "gui.h"
|
2018-10-05 01:48:59 +00:00
|
|
|
#include "nc.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <unordered_map>
|
2018-10-05 03:46:50 +00:00
|
|
|
#ifdef BUILD_NCURSES
|
|
|
|
#include <ncurses.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BUILD_NCURSES
|
2023-02-24 13:43:15 +00:00
|
|
|
extern WINDOW* ncurses_window;
|
2018-10-05 03:46:50 +00:00
|
|
|
#endif
|
2018-10-05 01:48:59 +00:00
|
|
|
|
|
|
|
namespace conky {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
#ifdef BUILD_NCURSES
|
|
|
|
conky::display_output_ncurses ncurses_output;
|
|
|
|
#else
|
|
|
|
conky::disabled_display_output ncurses_output_disabled("ncurses",
|
|
|
|
"BUILD_NCURSES");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace
|
2021-10-26 00:09:35 +00:00
|
|
|
extern void init_ncurses_output() {}
|
2018-10-05 01:48:59 +00:00
|
|
|
|
2018-10-05 03:46:50 +00:00
|
|
|
// namespace priv {
|
|
|
|
|
|
|
|
//} // namespace priv
|
2018-10-05 01:48:59 +00:00
|
|
|
|
|
|
|
#ifdef BUILD_NCURSES
|
|
|
|
|
2023-02-05 05:47:06 +00:00
|
|
|
#define COLORS_BUILTIN 8
|
|
|
|
|
|
|
|
Colour ncurses_colors[COLORS_BUILTIN + COLORS_CUSTOM] = {
|
2023-02-24 13:43:15 +00:00
|
|
|
{0x00, 0x00, 0x00, 0xff}, // BLACK
|
|
|
|
{0xff, 0x00, 0x00, 0xff}, // RED
|
|
|
|
{0x00, 0xff, 0x00, 0xff}, // GREEN
|
|
|
|
{0xff, 0xff, 0x00, 0xff}, // YELLOW
|
|
|
|
{0x00, 0x00, 0xff, 0xff}, // BLUE
|
|
|
|
{0xff, 0x00, 0xff, 0xff}, // MAGENTA
|
|
|
|
{0x00, 0xff, 0xff, 0xff}, // CYAN
|
|
|
|
{0xff, 0xff, 0xff, 0xff}, // WHITE
|
2023-02-05 05:47:06 +00:00
|
|
|
};
|
|
|
|
|
2023-02-24 13:43:15 +00:00
|
|
|
// Find the nearest ncurses color.
|
|
|
|
int to_ncurses(const Colour& c) {
|
|
|
|
int mindiff = INT_MAX;
|
|
|
|
int best_nccolor = 0;
|
|
|
|
for (int nccolor = 0; nccolor < COLORS_BUILTIN + COLORS_CUSTOM; nccolor++) {
|
|
|
|
const Colour& other = ncurses_colors[nccolor];
|
|
|
|
int diff = abs(c.red - other.red) + abs(c.green - other.green) +
|
|
|
|
abs(c.blue - other.blue);
|
|
|
|
|
|
|
|
if (diff < mindiff) {
|
|
|
|
mindiff = diff;
|
|
|
|
best_nccolor = nccolor;
|
2023-02-05 05:47:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-24 13:43:15 +00:00
|
|
|
return best_nccolor;
|
|
|
|
}
|
2023-02-05 05:47:06 +00:00
|
|
|
|
|
|
|
Colour from_ncurses(int nccolor) {
|
2023-02-24 13:43:15 +00:00
|
|
|
if (nccolor >= 0 && nccolor < COLORS_BUILTIN + COLORS_CUSTOM) {
|
|
|
|
return ncurses_colors[nccolor];
|
|
|
|
}
|
|
|
|
return error_colour;
|
2023-02-05 05:47:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 01:48:59 +00:00
|
|
|
display_output_ncurses::display_output_ncurses()
|
|
|
|
: display_output_console("ncurses") {
|
|
|
|
priority = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool display_output_ncurses::detect() {
|
|
|
|
if (out_to_ncurses.get(*state)) {
|
2020-12-26 17:49:30 +00:00
|
|
|
DBGP2("Display output '%s' enabled in config.", name.c_str());
|
2018-10-05 01:48:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-05 03:46:50 +00:00
|
|
|
bool display_output_ncurses::initialize() {
|
2023-02-24 13:43:15 +00:00
|
|
|
for (int i = 0; i < COLORS_CUSTOM; i++) {
|
2023-02-05 05:47:06 +00:00
|
|
|
Colour c = color[i].get(*state);
|
2023-02-24 13:43:15 +00:00
|
|
|
init_color(COLORS_BUILTIN + i, (1000 * c.red) / 255, (1000 * c.green) / 255,
|
|
|
|
(1000 * c.blue) / 255);
|
2023-02-05 05:47:06 +00:00
|
|
|
ncurses_colors[COLORS_BUILTIN + i] = c;
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:54:55 +00:00
|
|
|
is_active = ncurses_window != nullptr;
|
|
|
|
return is_active;
|
2018-10-05 03:46:50 +00:00
|
|
|
}
|
2018-10-05 01:48:59 +00:00
|
|
|
|
|
|
|
bool display_output_ncurses::shutdown() { return false; }
|
|
|
|
|
2023-01-02 23:54:53 +00:00
|
|
|
void display_output_ncurses::set_foreground_color(Colour c) {
|
2023-02-05 05:47:06 +00:00
|
|
|
int nccolor = to_ncurses(c);
|
2024-02-22 13:33:31 +00:00
|
|
|
init_pair(nccolor + 1, nccolor, COLOR_BLACK);
|
|
|
|
attron(COLOR_PAIR(nccolor + 1));
|
2018-10-05 03:46:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 22:54:55 +00:00
|
|
|
void display_output_ncurses::begin_draw_text() {
|
2018-10-05 03:46:50 +00:00
|
|
|
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
|
|
|
|
attron(COLOR_PAIR(COLOR_WHITE));
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:54:55 +00:00
|
|
|
void display_output_ncurses::end_draw_text() {}
|
2018-10-05 03:46:50 +00:00
|
|
|
|
2023-02-24 13:43:15 +00:00
|
|
|
void display_output_ncurses::draw_string(const char* s, int) {
|
2018-10-05 03:46:50 +00:00
|
|
|
printw("%s", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void display_output_ncurses::line_inner_done() { printw("\n"); }
|
|
|
|
|
|
|
|
int display_output_ncurses::getx() {
|
|
|
|
int x, y;
|
|
|
|
getyx(ncurses_window, y, x);
|
2021-10-25 18:17:19 +00:00
|
|
|
(void)y;
|
2018-10-05 03:46:50 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int display_output_ncurses::gety() {
|
|
|
|
int x, y;
|
|
|
|
getyx(ncurses_window, y, x);
|
2021-10-25 18:17:19 +00:00
|
|
|
(void)x;
|
2018-10-05 03:46:50 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:54:55 +00:00
|
|
|
void display_output_ncurses::gotox(int x) {
|
2018-10-05 03:46:50 +00:00
|
|
|
int y, old_x;
|
|
|
|
getyx(ncurses_window, y, old_x);
|
2021-10-25 18:17:19 +00:00
|
|
|
(void)old_x;
|
2018-10-05 03:46:50 +00:00
|
|
|
move(y, x);
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:54:55 +00:00
|
|
|
void display_output_ncurses::gotoy(int y) {
|
2018-10-05 03:46:50 +00:00
|
|
|
int x, old_y;
|
|
|
|
getyx(ncurses_window, old_y, x);
|
2021-10-25 18:17:19 +00:00
|
|
|
(void)old_y;
|
2018-10-05 03:46:50 +00:00
|
|
|
move(y, x);
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:54:55 +00:00
|
|
|
void display_output_ncurses::gotoxy(int x, int y) { move(y, x); }
|
2018-10-05 03:46:50 +00:00
|
|
|
|
2018-10-18 22:54:55 +00:00
|
|
|
void display_output_ncurses::flush() {
|
2018-10-05 03:46:50 +00:00
|
|
|
refresh();
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:48:59 +00:00
|
|
|
#endif /* BUILD_NCURSES */
|
|
|
|
|
|
|
|
} // namespace conky
|