1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-15 09:44:04 +00:00

remove x11 dependency for color parsing

this means we can use the same, portable color-parsing code
everywhere, including ncurses
This commit is contained in:
bi4k8 2023-01-05 20:54:30 +00:00 committed by Brenden Matthews
parent a72902fa35
commit ec24079a3a
4 changed files with 12 additions and 75 deletions

View File

@ -91,7 +91,6 @@ unsigned int adjust_colours(unsigned int colour) {
} }
#ifdef BUILD_GUI #ifdef BUILD_GUI
#ifdef BUILD_WAYLAND
static int hex_nibble_value(char c) { static int hex_nibble_value(char c) {
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
return c - '0'; return c - '0';
@ -139,7 +138,7 @@ Colour Colour::from_ncurses(int nccolor) {
} }
#endif /*BUILD_NCURSES*/ #endif /*BUILD_NCURSES*/
Colour manually_get_x11_color(const char *name) { Colour parse_color(const char *name) {
unsigned short r, g, b; unsigned short r, g, b;
size_t len = strlen(name); size_t len = strlen(name);
if (OsLookupColor(-1, name, len, &r, &g, &b)) { if (OsLookupColor(-1, name, len, &r, &g, &b)) {
@ -172,52 +171,8 @@ err:
NORM_ERR("can't parse X color '%s' (%d)", name, len); NORM_ERR("can't parse X color '%s' (%d)", name, len);
return error_colour; return error_colour;
} }
#endif /* BUILD_WAYLAND */
Colour get_x11_color(const char *name) { Colour parse_color(const std::string &colour) {
#ifdef BUILD_X11 return parse_color(colour.c_str());
#ifdef BUILD_WAYLAND
if (!display) { return manually_get_x11_color(name); }
#endif /*BUILD_WAYLAND*/
if (out_to_x.get(*state)) {
assert(display != nullptr);
XColor color;
color.pixel = 0;
if (XParseColor(display, DefaultColormap(display, screen), name, &color) ==
0) {
/* lets check if it's a hex colour with the # missing in front
* if yes, then do something about it */
char newname[DEFAULT_TEXT_BUFFER_SIZE];
newname[0] = '#';
strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
/* now lets try again */
if (XParseColor(display, DefaultColormap(display, screen), &newname[0],
&color) == 0) {
NORM_ERR("can't parse X color '%s'", name);
return error_colour;
}
}
if (XAllocColor(display, DefaultColormap(display, screen), &color) == 0) {
NORM_ERR("can't allocate X color '%s'", name);
}
Colour out;
out.red = color.red;
out.green = color.green;
out.blue = color.blue;
out.alpha = 0xff;
return out;
}
#endif /*BUILD_X11*/
#ifdef BUILD_WAYLAND
return manually_get_x11_color(name);
#endif /*BUILD_WAYLAND*/
}
Colour get_x11_color(const std::string &colour) {
return get_x11_color(colour.c_str());
} }
#endif /*BUILD_GUI*/ #endif /*BUILD_GUI*/

View File

@ -114,8 +114,8 @@ public:
}; };
Colour get_x11_color(const std::string &colour); Colour parse_color(const std::string &colour);
// XXX: when everyone uses C++ strings, remove this C version // XXX: when everyone uses C++ strings, remove this C version
Colour get_x11_color(const char *); Colour parse_color(const char *);
#endif /* _COLOURS_H */ #endif /* _COLOURS_H */

View File

@ -403,7 +403,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
#ifdef BUILD_GUI #ifdef BUILD_GUI
if (s[0] == '#') { if (s[0] == '#') {
obj->data.l = get_x11_color(s).to_argb32(); obj->data.l = parse_color(s).to_argb32();
obj->callbacks.print = &new_fg; obj->callbacks.print = &new_fg;
} else } else
#endif /* BUILD_GUI */ #endif /* BUILD_GUI */
@ -750,33 +750,15 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
#endif /* BUILD_GUI */ #endif /* BUILD_GUI */
END OBJ(color, nullptr) END OBJ(color, nullptr)
#ifdef BUILD_GUI #ifdef BUILD_GUI
if (out_to_gui(*state)) { if (out_to_gui(*state)) {
Colour c = arg != nullptr ? get_x11_color(arg) : default_color.get(*state); Colour c = arg != nullptr ? parse_color(arg) : default_color.get(*state);
obj->data.l = c.to_argb32(); obj->data.l = c.to_argb32();
set_current_text_color(c); set_current_text_color(c);
} }
#endif /* BUILD_GUI */ #endif /* BUILD_GUI */
#ifdef BUILD_NCURSES #ifdef BUILD_NCURSES
if (out_to_ncurses.get(*state)) { if (out_to_ncurses.get(*state)) {
obj->data.l = COLOR_WHITE; Colour c = arg != nullptr ? parse_color(arg) : default_color.get(*state);
if (arg != nullptr) {
if (strcasecmp(arg, "red") == 0) {
obj->data.l = COLOR_RED;
} else if (strcasecmp(arg, "green") == 0) {
obj->data.l = COLOR_GREEN;
} else if (strcasecmp(arg, "yellow") == 0) {
obj->data.l = COLOR_YELLOW;
} else if (strcasecmp(arg, "blue") == 0) {
obj->data.l = COLOR_BLUE;
} else if (strcasecmp(arg, "magenta") == 0) {
obj->data.l = COLOR_MAGENTA;
} else if (strcasecmp(arg, "cyan") == 0) {
obj->data.l = COLOR_CYAN;
} else if (strcasecmp(arg, "black") == 0) {
obj->data.l = COLOR_BLACK;
}
}
Colour c = Color::from_ncurses(obj->data.l);
obj->data.l = c.to_argb32(); obj->data.l = c.to_argb32();
set_current_text_color(c); set_current_text_color(c);
init_pair(c.to_ncurses(), c.to_ncurses(), COLOR_BLACK); init_pair(c.to_ncurses(), c.to_ncurses(), COLOR_BLACK);
@ -1457,14 +1439,14 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
END OBJ(shadecolor, nullptr) END OBJ(shadecolor, nullptr)
#ifdef BUILD_GUI #ifdef BUILD_GUI
obj->data.l = obj->data.l =
(arg != nullptr ? get_x11_color(arg) : default_shade_color.get(*state)) (arg != nullptr ? parse_color(arg) : default_shade_color.get(*state))
.to_argb32(); .to_argb32();
obj->callbacks.print = &new_bg; obj->callbacks.print = &new_bg;
#endif /* BUILD_GUI */ #endif /* BUILD_GUI */
END OBJ(outlinecolor, nullptr) END OBJ(outlinecolor, nullptr)
#ifdef BUILD_GUI #ifdef BUILD_GUI
obj->data.l = obj->data.l =
(arg != nullptr ? get_x11_color(arg) : default_outline_color.get(*state)) (arg != nullptr ? parse_color(arg) : default_outline_color.get(*state))
.to_argb32(); .to_argb32();
obj->callbacks.print = &new_outline; obj->callbacks.print = &new_outline;
#endif /* BUILD_GUI */ #endif /* BUILD_GUI */

View File

@ -107,7 +107,7 @@ struct colour_traits {
static inline std::pair<Type, bool> convert(lua::state &l, int index, static inline std::pair<Type, bool> convert(lua::state &l, int index,
const std::string &) { const std::string &) {
return {get_x11_color(l.tostring(index)), true}; return {parse_color(l.tostring(index)), true};
} }
}; };