annotate src/sys/x11/settings.cc @ 354:9aaf1e788896

core/endian: fix compile error under clang
author Paper <paper@paper.us.eu.org>
date Sun, 14 Jul 2024 23:39:42 -0400
parents 2f094656e775
children 99c961c91809
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"
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #include "core/endian.h"
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 #include <cstring>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <cstdint>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <climits>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <string_view>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #include <memory>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 #include <array>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 #include <optional>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 #include <iostream>
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
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 std::vector<SettingsItem> ParseAllItems(void);
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:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 /* byte order values */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 enum {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 LSBFirst = 0,
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 MSBFirst = 1,
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 bool ReadData(T& ret) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57 if (offset_ + sizeof(T) >= size_)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 return false;
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 ret = *reinterpret_cast<T*>(bytes_ + offset_);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 Advance(sizeof(T));
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 return true;
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
354
9aaf1e788896 core/endian: fix compile error under clang
Paper <paper@paper.us.eu.org>
parents: 353
diff changeset
65 /* will fail on signed integers; xsettings has none of those though */
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 bool ReadInt(T& ret) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68 static_assert(std::is_integral<T>::value);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 if (!ReadData<T>(ret))
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
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 switch (byte_order_) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 case LSBFirst:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 ret = Endian::byteswap_little_to_host(ret);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 break;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 case MSBFirst:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 ret = Endian::byteswap_big_to_host(ret);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 break;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80 default:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 /* can't know for sure. punt */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88 bool ReadString(std::string& str, std::size_t size);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 bool Advance(std::size_t amount);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 /* raw data */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 std::uint8_t *bytes_ = nullptr;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 std::size_t offset_ = 0;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 std::size_t size_ = 0;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96 /* parsed in the constructor */
353
2f094656e775 sys/x11/settings: misc fixups
Paper <paper@paper.us.eu.org>
parents: 351
diff changeset
97 std::uint8_t byte_order_ = 0;
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 std::uint32_t serial_ = 0;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99 std::uint32_t total_items_ = 0;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100 };
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 std::uint32_t Parser::GetTotalItems(void) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103 return total_items_;
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
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106 bool Parser::ReadString(std::string& str, std::size_t size) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 if (offset_ + size >= size_)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110 str.assign(reinterpret_cast<const char *>(bytes_ + offset_), size);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
111 Advance(size);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115 bool Parser::Advance(std::size_t amount) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116 if (offset_ + amount >= size_)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
118
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 offset_ += amount;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
121 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123 Parser::Parser(std::uint8_t *bytes, std::size_t size) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
124 bytes_ = bytes;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125 size_ = size;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
126
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
127 if (!ReadData<std::uint8_t>(byte_order_))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128 return;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 Advance(3);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
131
353
2f094656e775 sys/x11/settings: misc fixups
Paper <paper@paper.us.eu.org>
parents: 351
diff changeset
132 if (!ReadInt<std::uint32_t>(serial_))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
133 return;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
134
353
2f094656e775 sys/x11/settings: misc fixups
Paper <paper@paper.us.eu.org>
parents: 351
diff changeset
135 if (!ReadInt<std::uint32_t>(total_items_))
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
136 return;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
137 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
138
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
139 std::optional<SettingsItem> Parser::ParseNextItem(void) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
140 SettingsItem item;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
141
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
142 /* read one byte */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
143 if (!ReadInt<std::uint8_t>(item.type))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
144 return std::nullopt;
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 if (!item.VerifyType())
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
147 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
148
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
149 /* skip padding */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
150 if (!Advance(1))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
151 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
152
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
153 /* parse the name */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
154 std::uint16_t name_size;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
155 if (!ReadInt<std::uint16_t>(name_size))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
156 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
157
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
158 if (!ReadString(item.name, name_size))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
159 return std::nullopt;
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 /* padding */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
162 if (!Advance(GetPadding(name_size, 4)))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
163 return std::nullopt;
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 if (!ReadInt<std::uint32_t>(item.serial))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
166 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
167
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
168 switch (item.type) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
169 case SettingsItem::TypeInt: {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
170 if (!ReadInt<std::uint32_t>(item.data.integer))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
171 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
172
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
173 break;
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 case SettingsItem::TypeStr: {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
176 std::uint32_t size;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
177 if (!ReadInt<std::uint32_t>(size))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
178 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
179
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
180 if (!ReadString(item.data.string, size))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
181 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
182
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
183 /* padding */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
184 if (!Advance(GetPadding(size, 4)))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
185 return std::nullopt;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
186
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
187 break;
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 case SettingsItem::TypeRgba: {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
190 /* The order here is important!! */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
191 if (!ReadInt<std::uint16_t>(item.data.rgba.red)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
192 || !ReadInt<std::uint16_t>(item.data.rgba.blue)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
193 || !ReadInt<std::uint16_t>(item.data.rgba.green)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
194 || !ReadInt<std::uint16_t>(item.data.rgba.alpha))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
195 return std::nullopt;
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 break;
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 default:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
200 /* can't do anything now, can we? */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
201 return std::nullopt;
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
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
204 return item;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
205 }
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 std::vector<SettingsItem> Parser::ParseAllItems(void) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
208 offset_ = 0;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
209
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
210 std::uint32_t i;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
211 std::vector<SettingsItem> items;
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 for (i = 0; i < total_items_; i++) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
214 std::optional<SettingsItem> item = ParseNextItem();
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
215 if (!item)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
216 break;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
217
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
218 items.push_back(item.value());
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
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
221 return items;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
222 }
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 /* ------------------------------------------------------------------------- */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
225 /* real X11 code */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
226
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
227 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
228 struct MallocDestructor {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
229 void operator()(T *t) const { std::free(t); };
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 struct XcbConnectionDestructor {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
233 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
234 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
235
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
236 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
237 using MallocPtr = std::unique_ptr<T, MallocDestructor<T>>;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
238
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
239 using XcbConnectionPtr = std::unique_ptr<xcb_connection_t, XcbConnectionDestructor>;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
240
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
241 /* RAII is nice */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
242 struct XcbGrabber {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
243 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
244 ~XcbGrabber() { ::xcb_ungrab_server(conn_); }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
245
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
246 private:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
247 ::xcb_connection_t *conn_;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
248 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
249
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
250 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
251 ::xcb_window_t owner = XCB_NONE;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
252 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
253
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
254 if (reply)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
255 owner = reply->owner;
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 return owner;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
258 }
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 static bool GetRawSettingsData(std::vector<uint8_t>& bytes) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
261 int screen;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
262
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
263 XcbConnectionPtr conn(::xcb_connect(nullptr, &screen));
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
264 if (::xcb_connection_has_error(conn.get()))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
265 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
266
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
267 /* get our needed atoms, available as atoms[Atom] */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
268 enum Atom {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
269 XSETTINGS_SCREEN, /* _XSETTINGS_S[N] */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
270 XSETTINGS_SETTINGS, /* _XSETTINGS_SETTINGS */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
271 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
272
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
273 std::map<Atom, ::xcb_atom_t> atoms;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
274 {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
275 std::map<Atom, std::string> names = {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
276 {XSETTINGS_SCREEN, fmt::format("_XSETTINGS_S{}", screen)},
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
277 {XSETTINGS_SETTINGS, "_XSETTINGS_SETTINGS"},
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
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
280 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
281 for (const auto& name : names)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
282 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
283
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
284 for (const auto& cookie : atom_cookies) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
285 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
286 if (!reply || reply->atom == XCB_NONE)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
287 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
288
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
289 atoms[cookie.first] = reply->atom;
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 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
292
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
293 MallocPtr<xcb_get_property_reply_t> reply;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
294 {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
295 /* grab the X server as *required* by xsettings docs */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
296 const XcbGrabber grabber(conn.get());
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 ::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
299 if (win == XCB_NONE)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
300 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
301
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
302 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
303 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
304 if (!reply)
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 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
308 int size = xcb_get_property_value_length(reply.get());
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
309 if (size < 0)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
310 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
311
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
312 bytes.assign(data, data + size);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
313
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
314 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
315 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
316
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
317 /* ------------------------------------------------------------------------- */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
318 /* 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
319
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
320 bool GetSettings(std::vector<SettingsItem>& settings) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
321 std::vector<std::uint8_t> xsettings_raw;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
322 if (!GetRawSettingsData(xsettings_raw))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
323 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
324
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
325 Parser parser(xsettings_raw.data(), xsettings_raw.size());
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
326 settings = parser.ParseAllItems();
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
327
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
328 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
329 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
330
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
331 bool FindSetting(const std::string& name, SettingsItem& setting) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
332 std::vector<std::uint8_t> xsettings_raw;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
333 if (!GetRawSettingsData(xsettings_raw))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
334 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
335
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
336 Parser parser(xsettings_raw.data(), xsettings_raw.size());
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
337
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
338 std::uint32_t total = parser.GetTotalItems();
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
339
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
340 for (; total; total--) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
341 std::optional<SettingsItem> opt_item = parser.ParseNextItem();
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
342 if (!opt_item)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
343 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
344
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
345 SettingsItem& item = opt_item.value();
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
346 if (item.name == name) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
347 setting = item;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
348 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
349 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
350 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
352 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
353 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
354
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
355 } // namespace x11