Changeset View
Changeset View
Standalone View
Standalone View
kcms/keyboard/tastenbrett/xkbobject.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU General Public License as | ||||
6 | published by the Free Software Foundation; either version 2 of | ||||
7 | the License or (at your option) version 3 or any later version | ||||
8 | accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | defined in Section 14 of version 3 of the license. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "xkbobject.h" | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | #include <QFile> | ||||
25 | #include <QHash> | ||||
26 | | ||||
27 | // hash of name to color from rgb.txt | ||||
28 | static QHash<QString, QColor> x11Colors() | ||||
29 | { | ||||
30 | QHash<QString, QColor> colors; | ||||
31 | | ||||
32 | static QStringList txtPaths = { | ||||
33 | "/usr/share/X11/rgb.txt" | ||||
34 | }; | ||||
35 | | ||||
36 | for (const auto &path : txtPaths) { | ||||
37 | QFile file(path); | ||||
38 | if (!file.open(QIODevice::ReadOnly)) { | ||||
39 | continue; | ||||
40 | } | ||||
41 | | ||||
42 | QByteArray line; | ||||
43 | QStringList list; | ||||
44 | while (!file.atEnd()) { | ||||
45 | line = file.readLine(); | ||||
46 | | ||||
47 | int red = 0; | ||||
48 | int green = 0; | ||||
49 | int blue = 0; | ||||
50 | int pos = 0; | ||||
51 | if (sscanf(line, "%d %d %d%n", &red, &green, &blue, &pos) == 3) { | ||||
52 | const QString name = line.mid(pos).trimmed(); | ||||
53 | const QColor color(red, green, blue); | ||||
54 | if (color.isValid()) { | ||||
55 | colors[name] = color; | ||||
56 | } | ||||
57 | } | ||||
58 | } | ||||
59 | | ||||
60 | break; // found a file | ||||
61 | } | ||||
62 | | ||||
63 | return colors; | ||||
64 | } | ||||
65 | | ||||
66 | QColor XkbObject::fromXKBColor(const XkbColorRec &color) | ||||
67 | { | ||||
68 | // Unfortunately the color spec is explicitly not defined, so | ||||
69 | // it's up to the xserver implementation. Also unfortunate | ||||
70 | // is that Qt no longer seems to have capabilities of | ||||
71 | // natively handling x11 colors specs, so this is literally | ||||
72 | // a shot in the dark. | ||||
73 | // XParseColor is how one would get that data, but then we need | ||||
74 | // a color map, and for that we need a visual and a window.... | ||||
75 | // all in all a pile of crap. | ||||
76 | // | ||||
77 | // https://en.wikipedia.org/wiki/X11_color_names | ||||
78 | | ||||
79 | static auto colors = x11Colors(); | ||||
80 | | ||||
81 | const QString spec = QString::fromUtf8(color.spec); | ||||
82 | | ||||
83 | // Resolve through rgb.txt | ||||
84 | QColor ret = colors.value(spec); | ||||
85 | if (ret.isValid()) { | ||||
86 | return ret; | ||||
87 | } | ||||
88 | | ||||
89 | // Fall back to maybe resolve it internally somehow. | ||||
90 | // This will pretty much never work as most colors aren't | ||||
91 | // straight forward black, green, etc. | ||||
92 | ret.setNamedColor(spec); | ||||
93 | if (ret.isValid()) { | ||||
94 | return ret; | ||||
95 | } | ||||
96 | | ||||
97 | // NB: we implicitly rely on some values not mapping! | ||||
98 | // e.g. doodad indicators may have green30 as off_color | ||||
99 | // green30 is not a valid color, so this is implicitly | ||||
100 | // meant to go nowhere as otherwise on would be green and off too | ||||
101 | // ... quite the mess. | ||||
102 | | ||||
103 | qDebug() << "failed to map x11 color!" << color.spec; | ||||
104 | | ||||
105 | return QColor(Qt::gray); | ||||
106 | } |