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

colours: define manually_get_x11_color for usage outside X11

This commit is contained in:
bi4k8 2022-11-28 19:23:23 +00:00 committed by Brenden Matthews
parent e914128c32
commit 65f4d4f7eb
No known key found for this signature in database
GPG Key ID: B49ABB7270D9D4FD
2 changed files with 48 additions and 29 deletions

View File

@ -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<long>(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<long>(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) {

View File

@ -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);