From ec24079a3a270170215e4bd59333c09213415149 Mon Sep 17 00:00:00 2001 From: bi4k8 Date: Thu, 5 Jan 2023 20:54:30 +0000 Subject: [PATCH] remove x11 dependency for color parsing this means we can use the same, portable color-parsing code everywhere, including ncurses --- src/colours.cc | 51 +++----------------------------------------------- src/colours.h | 4 ++-- src/core.cc | 30 ++++++----------------------- src/gui.h | 2 +- 4 files changed, 12 insertions(+), 75 deletions(-) diff --git a/src/colours.cc b/src/colours.cc index 0456dc6e..bda40d84 100644 --- a/src/colours.cc +++ b/src/colours.cc @@ -91,7 +91,6 @@ unsigned int adjust_colours(unsigned int colour) { } #ifdef BUILD_GUI -#ifdef BUILD_WAYLAND static int hex_nibble_value(char c) { if (c >= '0' && c <= '9') { return c - '0'; @@ -139,7 +138,7 @@ Colour Colour::from_ncurses(int nccolor) { } #endif /*BUILD_NCURSES*/ -Colour manually_get_x11_color(const char *name) { +Colour parse_color(const char *name) { unsigned short r, g, b; size_t len = strlen(name); if (OsLookupColor(-1, name, len, &r, &g, &b)) { @@ -172,52 +171,8 @@ err: NORM_ERR("can't parse X color '%s' (%d)", name, len); return error_colour; } -#endif /* BUILD_WAYLAND */ -Colour get_x11_color(const char *name) { -#ifdef BUILD_X11 -#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()); +Colour parse_color(const std::string &colour) { + return parse_color(colour.c_str()); } #endif /*BUILD_GUI*/ diff --git a/src/colours.h b/src/colours.h index f7275469..6a32a95e 100644 --- a/src/colours.h +++ b/src/colours.h @@ -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 -Colour get_x11_color(const char *); +Colour parse_color(const char *); #endif /* _COLOURS_H */ diff --git a/src/core.cc b/src/core.cc index cbae7b45..58b0a85d 100644 --- a/src/core.cc +++ b/src/core.cc @@ -403,7 +403,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long line, #ifdef BUILD_GUI 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; } else #endif /* BUILD_GUI */ @@ -750,33 +750,15 @@ struct text_object *construct_text_object(char *s, const char *arg, long line, #endif /* BUILD_GUI */ END OBJ(color, nullptr) #ifdef BUILD_GUI - if (out_to_gui(*state)) { - Colour c = arg != nullptr ? get_x11_color(arg) : default_color.get(*state); + if (out_to_gui(*state)) { + Colour c = arg != nullptr ? parse_color(arg) : default_color.get(*state); obj->data.l = c.to_argb32(); set_current_text_color(c); } #endif /* BUILD_GUI */ #ifdef BUILD_NCURSES if (out_to_ncurses.get(*state)) { - obj->data.l = COLOR_WHITE; - 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); + Colour c = arg != nullptr ? parse_color(arg) : default_color.get(*state); obj->data.l = c.to_argb32(); set_current_text_color(c); 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) #ifdef BUILD_GUI 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(); obj->callbacks.print = &new_bg; #endif /* BUILD_GUI */ END OBJ(outlinecolor, nullptr) #ifdef BUILD_GUI 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(); obj->callbacks.print = &new_outline; #endif /* BUILD_GUI */ diff --git a/src/gui.h b/src/gui.h index 99d769da..04a94ed1 100644 --- a/src/gui.h +++ b/src/gui.h @@ -107,7 +107,7 @@ struct colour_traits { static inline std::pair convert(lua::state &l, int index, const std::string &) { - return {get_x11_color(l.tostring(index)), true}; + return {parse_color(l.tostring(index)), true}; } };