2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2009-02-22 16:53:30 +00:00
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2021-02-27 15:14:19 +00:00
|
|
|
* Copyright (c) 2005-2021 Brenden Matthews, Philip Kovacs, et. al.
|
2009-02-22 16:53:30 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 "conky.h"
|
2022-11-28 19:23:23 +00:00
|
|
|
#include "gui.h"
|
2022-12-24 14:44:03 +00:00
|
|
|
#include "logging.h"
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_X11
|
2009-02-22 16:53:30 +00:00
|
|
|
#include "x11.h"
|
2022-11-28 19:23:23 +00:00
|
|
|
#endif /*BUILD_X11*/
|
|
|
|
#ifdef BUILD_WAYLAND
|
|
|
|
#include "x11-color.h"
|
|
|
|
#endif /*BUILD_WAYLAND*/
|
2009-02-22 16:53:30 +00:00
|
|
|
|
|
|
|
/* precalculated: 31/255, and 63/255 */
|
|
|
|
#define CONST_8_TO_5_BITS 0.12156862745098
|
|
|
|
#define CONST_8_TO_6_BITS 0.247058823529412
|
|
|
|
|
2020-11-09 15:47:13 +00:00
|
|
|
short colour_depth = 0;
|
|
|
|
long redmask, greenmask, bluemask;
|
2020-11-08 18:08:27 +00:00
|
|
|
|
2020-11-09 15:47:13 +00:00
|
|
|
void set_up_gradient() {
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_X11
|
2018-05-12 16:03:00 +00:00
|
|
|
if (out_to_x.get(*state)) {
|
|
|
|
colour_depth = DisplayPlanes(display, screen);
|
|
|
|
} else
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_X11 */
|
2018-05-12 16:03:00 +00:00
|
|
|
{
|
|
|
|
colour_depth = 16;
|
|
|
|
}
|
|
|
|
if (colour_depth != 24 && colour_depth != 16) {
|
|
|
|
NORM_ERR(
|
|
|
|
"using non-standard colour depth, gradients may look like a "
|
|
|
|
"lolly-pop");
|
|
|
|
}
|
2009-02-22 16:53:30 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
redmask = 0;
|
|
|
|
greenmask = 0;
|
|
|
|
bluemask = 0;
|
2021-03-06 12:58:08 +00:00
|
|
|
for (int i = (colour_depth / 3) - 1; i >= 0; i--) {
|
2018-05-12 16:03:00 +00:00
|
|
|
redmask |= 1 << i;
|
|
|
|
greenmask |= 1 << i;
|
|
|
|
bluemask |= 1 << i;
|
|
|
|
}
|
2019-02-23 19:40:34 +00:00
|
|
|
if (colour_depth % 3 == 1) { greenmask |= 1 << (colour_depth / 3); }
|
2018-05-12 16:03:00 +00:00
|
|
|
redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
|
|
|
|
greenmask = greenmask << (colour_depth / 3);
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 09:14:52 +00:00
|
|
|
/* adjust colour values depending on colour depth */
|
2018-05-12 16:03:00 +00:00
|
|
|
unsigned int adjust_colours(unsigned int colour) {
|
|
|
|
double r, g, b;
|
2009-02-22 16:53:30 +00:00
|
|
|
|
2019-02-23 19:40:34 +00:00
|
|
|
if (colour_depth == 0) { set_up_gradient(); }
|
2018-05-12 16:03:00 +00:00
|
|
|
if (colour_depth == 16) {
|
|
|
|
r = (colour & 0xff0000) >> 16;
|
|
|
|
g = (colour & 0xff00) >> 8;
|
|
|
|
b = colour & 0xff;
|
2018-05-12 23:26:31 +00:00
|
|
|
colour = static_cast<int>(r * CONST_8_TO_5_BITS) << 11;
|
|
|
|
colour |= static_cast<int>(g * CONST_8_TO_6_BITS) << 5;
|
|
|
|
colour |= static_cast<int>(b * CONST_8_TO_5_BITS);
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
return colour;
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:23:23 +00:00
|
|
|
#ifdef BUILD_GUI
|
2022-11-28 19:23:23 +00:00
|
|
|
#ifdef BUILD_WAYLAND
|
2022-11-28 19:23:23 +00:00
|
|
|
static int hex_nibble_value(char c) {
|
2022-12-24 14:44:03 +00:00
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
return c - '0';
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
return c - 'a' + 10;
|
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
|
|
|
return c - 'A' + 10;
|
|
|
|
}
|
|
|
|
return -1;
|
2022-11-28 19:23:23 +00:00
|
|
|
}
|
2009-11-22 13:55:22 +00:00
|
|
|
|
2022-11-28 19:23:23 +00:00
|
|
|
long manually_get_x11_color(const char *name) {
|
2022-12-24 14:44:03 +00:00
|
|
|
unsigned short r, g, b;
|
|
|
|
size_t len = strlen(name);
|
|
|
|
if (OsLookupColor(-1, name, len, &r, &g, &b)) {
|
2023-01-03 00:38:51 +00:00
|
|
|
return 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
|
2022-12-24 14:44:03 +00:00
|
|
|
}
|
|
|
|
if (name[0] == '#') {
|
|
|
|
name++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
if (len == 6 || len == 8) {
|
2023-01-03 00:38:51 +00:00
|
|
|
bool skip_alpha = (len == 6);
|
2022-12-24 14:44:03 +00:00
|
|
|
unsigned char argb[4] = {0xff, 0, 0, 0};
|
|
|
|
for (size_t i = 0; i + 1 < len; i += 2) {
|
|
|
|
int nib1 = hex_nibble_value(name[i]);
|
|
|
|
int nib2 = hex_nibble_value(name[i + 1]);
|
|
|
|
if (nib1 < 0 || nib2 < 0) { goto err; }
|
|
|
|
int val = (nib1 << 4) + nib2;
|
2022-11-28 19:23:23 +00:00
|
|
|
|
2023-01-03 00:38:51 +00:00
|
|
|
argb[skip_alpha + i / 2] = val;
|
2022-11-28 19:23:23 +00:00
|
|
|
}
|
2023-01-03 20:01:48 +00:00
|
|
|
long out = (argb[0] << 24) | (argb[1] << 16) | (argb[2] << 8) | argb[3];
|
2022-12-24 14:44:03 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
NORM_ERR("can't parse X color '%s' (%d)", name, len);
|
|
|
|
return 0xFF00FF;
|
2022-11-28 19:23:23 +00:00
|
|
|
}
|
|
|
|
#endif /* BUILD_WAYLAND */
|
|
|
|
|
|
|
|
long get_x11_color(const char *name) {
|
|
|
|
#ifdef BUILD_X11
|
|
|
|
#ifdef BUILD_WAYLAND
|
2022-12-24 14:44:03 +00:00
|
|
|
if (!display) { return manually_get_x11_color(name); }
|
2022-11-28 19:23:23 +00:00
|
|
|
#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*/
|
2009-11-22 13:55:22 +00:00
|
|
|
}
|
2010-02-28 11:26:18 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
long get_x11_color(const std::string &colour) {
|
|
|
|
return get_x11_color(colour.c_str());
|
|
|
|
}
|
2022-11-28 19:23:23 +00:00
|
|
|
#endif /*BUILD_GUI*/
|