annotate src/sys/x11/settings.cc @ 367:8d45d892be88 default tip

*: instead of pugixml, use Qt XML features this means we have one extra Qt dependency though...
author Paper <paper@tflc.us>
date Sun, 17 Nov 2024 22:55:47 -0500
parents 99c961c91809
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include "sys/x11/settings.h"
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
2 #include "core/byte_stream.h"
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
4 #include <cassert>
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <cstring>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <cstdint>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <climits>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #include <string_view>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 #include <memory>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 #include <array>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 #include <optional>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 #include <map>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 #include <xcb/xcb.h>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 #include "fmt/core.h"
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 namespace x11 {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 bool SettingsItem::VerifyType() {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 switch (type) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 case SettingsItem::TypeInt:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 case SettingsItem::TypeStr:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 case SettingsItem::TypeRgba:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 default:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 /* -------------------------------------------------------------------------- */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 /* xsettings parser */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34 static constexpr std::size_t GetPadding(std::size_t length, std::size_t increment) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 /* ripped from xsettingsd */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36 return (increment - (length % increment)) % increment;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 class Parser {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 public:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 Parser(std::uint8_t *bytes, std::size_t size);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
43 bool ParseHeader(void);
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 std::optional<SettingsItem> ParseNextItem(void);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 std::uint32_t GetTotalItems(void);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 private:
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
49 enum ByteOrder {
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 LSBFirst = 0,
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 MSBFirst = 1,
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
54 ByteStream stream;
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 /* parsed in the constructor */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57 std::uint32_t serial_ = 0;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 std::uint32_t total_items_ = 0;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 std::uint32_t Parser::GetTotalItems(void) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 return total_items_;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
65 Parser::Parser(std::uint8_t *bytes, std::size_t size) : stream(bytes, size) {
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
68 bool Parser::ParseHeader(void) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
69 std::uint8_t byte_order;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
70 if (!stream.ReadBinary<std::uint8_t>(byte_order))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
73 switch (byte_order) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
74 case MSBFirst:
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
75 stream.SetEndianness(ByteStream::ByteOrder::Big);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
76 break;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
77 case LSBFirst:
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
78 stream.SetEndianness(ByteStream::ByteOrder::Little);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
79 break;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
80 default:
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
81 return false; /* errr */
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
82 }
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
84 stream.Advance(3);
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
86 if (!stream.ReadInt<std::uint32_t>(serial_))
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
87 return false;
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
89 if (!stream.ReadInt<std::uint32_t>(total_items_))
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
90 return false;
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
92 return true;
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 std::optional<SettingsItem> Parser::ParseNextItem(void) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96 SettingsItem item;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 /* read one byte */
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
99 if (!stream.ReadInt<std::uint8_t>(item.type))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102 if (!item.VerifyType())
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105 /* skip padding */
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
106 if (!stream.Advance(1))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109 /* parse the name */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110 std::uint16_t name_size;
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
111 if (!stream.ReadInt<std::uint16_t>(name_size))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
114 if (!stream.ReadString(item.name, name_size))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117 /* padding */
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
118 if (!stream.Advance(GetPadding(name_size, 4)))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
121 if (!stream.ReadInt<std::uint32_t>(item.serial))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
124 switch (item.type) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125 case SettingsItem::TypeInt: {
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
126 if (!stream.ReadInt<std::int32_t>(item.data.integer))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
127 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129 break;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
131 case SettingsItem::TypeStr: {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
132 std::uint32_t size;
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
133 if (!stream.ReadInt<std::uint32_t>(size))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
134 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
135
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
136 if (!stream.ReadString(item.data.string, size))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
137 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
138
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
139 /* don't fail if advancing fails on this padding,
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
140 * because this causes parsing to fail for strings
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
141 * at the end of the data */
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
142 stream.Advance(GetPadding(size, 4));
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
143
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
144 break;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
145 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
146 case SettingsItem::TypeRgba: {
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
147 /* it's actually RBGA, but whatever. */
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
148 if (!stream.ReadInt<std::uint16_t>(item.data.rgba.red)
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
149 || !stream.ReadInt<std::uint16_t>(item.data.rgba.blue)
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
150 || !stream.ReadInt<std::uint16_t>(item.data.rgba.green)
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
151 || !stream.ReadInt<std::uint16_t>(item.data.rgba.alpha))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
152 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
153
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
154 break;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
155 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
156 default:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
157 /* can't do anything now, can we? */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
158 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
159 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
160
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
161 return item;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
162 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
163
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
164 /* ------------------------------------------------------------------------- */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
165 /* real X11 code */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
166
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
167 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
168 struct MallocDestructor {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
169 void operator()(T *t) const { std::free(t); };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
170 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
171
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
172 struct XcbConnectionDestructor {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
173 void operator()(xcb_connection_t *conn) const { ::xcb_disconnect(conn); };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
174 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
175
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
176 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
177 using MallocPtr = std::unique_ptr<T, MallocDestructor<T>>;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
178
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
179 using XcbConnectionPtr = std::unique_ptr<xcb_connection_t, XcbConnectionDestructor>;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
180
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
181 /* RAII is nice */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
182 struct XcbGrabber {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
183 XcbGrabber(::xcb_connection_t *conn) { ::xcb_grab_server(conn); conn_ = conn; }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
184 ~XcbGrabber() { ::xcb_ungrab_server(conn_); }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
185
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
186 private:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
187 ::xcb_connection_t *conn_;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
188 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
189
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
190 static ::xcb_window_t GetSelectionOwner(::xcb_connection_t *conn, ::xcb_atom_t selection) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
191 ::xcb_window_t owner = XCB_NONE;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
192 MallocPtr<::xcb_get_selection_owner_reply_t> reply(::xcb_get_selection_owner_reply(conn, ::xcb_get_selection_owner(conn, selection), nullptr));
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
193
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
194 if (reply)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
195 owner = reply->owner;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
196
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
197 return owner;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
198 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
199
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
200 static bool GetRawSettingsData(std::vector<uint8_t>& bytes) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
201 int screen;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
202
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
203 XcbConnectionPtr conn(::xcb_connect(nullptr, &screen));
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
204 if (::xcb_connection_has_error(conn.get()))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
205 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
206
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
207 /* get our needed atoms, available as atoms[Atom] */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
208 enum Atom {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
209 XSETTINGS_SCREEN, /* _XSETTINGS_S[N] */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
210 XSETTINGS_SETTINGS, /* _XSETTINGS_SETTINGS */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
211 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
212
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
213 std::map<Atom, ::xcb_atom_t> atoms;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
214 {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
215 std::map<Atom, std::string> names = {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
216 {XSETTINGS_SCREEN, fmt::format("_XSETTINGS_S{}", screen)},
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
217 {XSETTINGS_SETTINGS, "_XSETTINGS_SETTINGS"},
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
218 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
219
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
220 std::map<Atom, ::xcb_intern_atom_cookie_t> atom_cookies;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
221 for (const auto& name : names)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
222 atom_cookies[name.first] = ::xcb_intern_atom(conn.get(), false, name.second.size(), name.second.data());
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
223
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
224 for (const auto& cookie : atom_cookies) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
225 MallocPtr<::xcb_intern_atom_reply_t> reply(::xcb_intern_atom_reply(conn.get(), cookie.second, nullptr));
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
226 if (!reply || reply->atom == XCB_NONE)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
227 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
228
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
229 atoms[cookie.first] = reply->atom;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
230 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
231 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
232
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
233 MallocPtr<xcb_get_property_reply_t> reply;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
234 {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
235 /* grab the X server as *required* by xsettings docs */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
236 const XcbGrabber grabber(conn.get());
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
237
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
238 ::xcb_window_t win = GetSelectionOwner(conn.get(), atoms[XSETTINGS_SCREEN]);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
239 if (win == XCB_NONE)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
240 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
241
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
242 reply.reset(::xcb_get_property_reply(conn.get(), ::xcb_get_property(conn.get(), 0, win, atoms[XSETTINGS_SETTINGS], XCB_ATOM_ANY, 0L, UINT_MAX), nullptr));
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
243 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
244 if (!reply)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
245 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
246
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
247 uint8_t *data = reinterpret_cast<uint8_t *>(xcb_get_property_value(reply.get()));
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
248 int size = xcb_get_property_value_length(reply.get());
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
249 if (size < 0)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
250 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
251
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
252 bytes.assign(data, data + size);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
253
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
254 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
255 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
256
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
257 /* ------------------------------------------------------------------------- */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
258 /* now for the actual all-important public API stringing all this together */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
259
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
260 bool GetSettings(std::vector<SettingsItem>& settings) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
261 std::vector<std::uint8_t> xsettings_raw;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
262 if (!GetRawSettingsData(xsettings_raw))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
263 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
264
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
265 Parser parser(xsettings_raw.data(), xsettings_raw.size());
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
266 if (!parser.ParseHeader())
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
267 return false;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
268
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
269 std::uint32_t total = parser.GetTotalItems();
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
270
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
271 while (total--) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
272 std::optional<SettingsItem> opt_item = parser.ParseNextItem();
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
273 if (!opt_item)
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
274 break;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
275
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
276 settings.push_back(opt_item.value());
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
277 }
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
278
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
279 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
280 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
281
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
282 bool FindSetting(const std::string& name, SettingsItem& setting) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
283 std::vector<std::uint8_t> xsettings_raw;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
284 if (!GetRawSettingsData(xsettings_raw))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
285 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
286
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
287 Parser parser(xsettings_raw.data(), xsettings_raw.size());
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
288 if (!parser.ParseHeader())
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
289 return false;
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
290
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
291 std::uint32_t total = parser.GetTotalItems();
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
292
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents: 354
diff changeset
293 while (total--) {
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
294 std::optional<SettingsItem> opt_item = parser.ParseNextItem();
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
295 if (!opt_item)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
296 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
297
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
298 SettingsItem& item = opt_item.value();
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
299 if (item.name == name) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
300 setting = item;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
301 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
302 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
303 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
304
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
305 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
306 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
307
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
308 } // namespace x11