diff --git a/src/colours.cc b/src/colours.cc index 2fe2e517..dbff6491 100644 --- a/src/colours.cc +++ b/src/colours.cc @@ -89,7 +89,7 @@ unsigned int adjust_colours(unsigned int colour) { #ifdef BUILD_GUI -#ifndef BUILD_X11 +#ifdef BUILD_WAYLAND static int hex_nibble_value(char c) { if (c >= '0' && c <= '9') { return c - '0'; @@ -100,34 +100,8 @@ static int hex_nibble_value(char c) { } return -1; } -#endif /* !BUILD_X11 */ -long get_x11_color(const char *name) { -#ifdef BUILD_X11 - 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 0xFF00FF; - } - } - if (XAllocColor(display, DefaultColormap(display, screen), &color) == 0) { - NORM_ERR("can't allocate X color '%s'", name); - } - - return static_cast(color.pixel); -#elif BUILD_WAYLAND +long manually_get_x11_color(const char *name) { unsigned short r, g, b; size_t len = strlen(name); if (OsLookupColor(-1, name, len, &r, &g, &b)) { @@ -157,7 +131,44 @@ long get_x11_color(const char *name) { err: NORM_ERR("can't parse X color '%s' (%d)", name, len); return 0xFF00FF; -#endif +} +#endif /* BUILD_WAYLAND */ + +long get_x11_color(const char *name) { +#ifdef BUILD_X11 +#ifdef BUILD_WAYLAND + if (!display) { + return manually_get_x11_color(name); + } +#endif /*BUILD_WAYLAND*/ + 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 0xFF00FF; + } + } + if (XAllocColor(display, DefaultColormap(display, screen), &color) == 0) { + NORM_ERR("can't allocate X color '%s'", name); + } + + return static_cast(color.pixel); +#endif /*BUILD_X11*/ +#ifdef BUILD_WAYLAND + return manually_get_x11_color(name); +#endif /*BUILD_WAYLAND*/ } long get_x11_color(const std::string &colour) { diff --git a/src/x11.cc b/src/x11.cc index 822ac760..f48781eb 100644 --- a/src/x11.cc +++ b/src/x11.cc @@ -313,8 +313,12 @@ static void init_X11() { ? dispstr.c_str() : nullptr; if ((display = XOpenDisplay(disp)) == nullptr) { +#ifdef BUILD_WAYLAND + return; +#else throw std::runtime_error(std::string("can't open display: ") + XDisplayName(disp)); +#endif } } @@ -877,6 +881,10 @@ void x11_init_window(lua::state &l __attribute__((unused)), bool own) { if (window.window == 0u) { window.window = find_desktop_window(&window.root, &window.desktop); } + if (window.window == 0u) { + return; + } + window.visual = DefaultVisual(display, screen); window.colourmap = DefaultColormap(display, screen);