Blender V5.3
recents.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "toml.hpp"
12
13#include <atomic>
14#include <condition_variable>
15#include <map>
16#include <optional>
17#include <thread>
18#include <vector>
19
20#include "BKE_appdir.hh"
21#include "BKE_blender_version.h"
22#include "BKE_global.hh"
23#include "BKE_recents.hh"
24
25#include "BLI_fileops.hh"
26#include "BLI_mutex.hh"
27#include "BLI_path_utils.hh"
28
29#include "MEM_guardedalloc.h"
30
31namespace blender::recents {
32
33constexpr toml::spec version = toml::spec::v(1, 0, 0);
34
35/* TOML storage. Protected by recents_mutex. */
36static Mutex recents_mutex; /* protects TOML values */
37static Mutex recents_init_mutex; /* used with cv for init wait */
38static std::atomic<bool> recents_ready{false};
39static std::condition_variable_any recents_init_cv;
40static std::once_flag recents_init_once;
41
42static std::string recents_file_path(const bool create_folder = false)
43{
44 std::optional<std::string> datafiles_path = create_folder ?
46 nullptr) :
48 if (!datafiles_path.has_value()) {
49 return {};
50 }
51 datafiles_path->append(SEP_STR);
52 datafiles_path->append(BLENDER_RECENTS_FILE);
53 return std::move(*datafiles_path);
54}
55
56static void recents_print_errors(const std::vector<toml::error_info> &errors)
57{
58 for (const toml::error_info &error : errors) {
59 std::string msg = toml::format_error(error);
60 fprintf(stderr, "%s\n", msg.c_str());
61 }
62}
63
64static const toml::value &defaults_get()
65{
66 static toml::value value = []() -> toml::value {
67 const StringRef default_str = R"_delim_(
68title = "Saved UI State Settings"
69name = "Blender"
70
71["temp.window.dimensions"]
72_default = [100.0, 900.0, 200.0, 800.0]
73PREFERENCES = [100.0, 940.0, 350.0, 900.0]
74FILE_BROWSER = [100.0, 1160.0, 350.0, 950.0]
75IMAGE_EDITOR = [50.0, 1360.0, 50.0, 830.0]
76GRAPH_EDITOR = [50.0, 950.0, 200.0, 780.0]
77INFO = [100.0, 1000.0, 300.0, 880.0]
78OUTLINER = [100.0, 550.0, 350.0, 800.0]
79PROJECT = [100.0, 800.0, 500.0, 850.0]
80
81["panel.sortorder"]
82_default = -1
83
84["panel.open"]
85_default = true
86
87)_delim_";
88 toml::result default_result = toml::try_parse_str(default_str, version);
89 if (!default_result.is_ok()) {
90 recents_print_errors(default_result.unwrap_err());
91 return toml::value();
92 }
93 return default_result.unwrap();
94 }();
95 return value;
96}
97
98static toml::value &current_get()
99{
100 static toml::value value = defaults_get();
101 return value;
102}
103
104static void recents_init_impl()
105{
106 defaults_get();
107
108 /* Load from on-disk file if found. */
109 /* In background mode avoid any file I/O or console error output. The
110 * in-memory defaults are still parsed above so API calls will work. */
111 if (!G.background) {
112 const std::string path = recents_file_path();
113 if (!path.empty() && BLI_exists(path.c_str())) {
114 /* Read with Blender file API for UTF-8 path support on Windows. */
115 size_t size = 0;
116 char *data = BLI_file_read_text_as_mem(path.c_str(), 0, &size);
117 std::string content = data ? std::string(data, size) : std::string();
118 MEM_delete(data);
119 toml::result file_result = toml::try_parse_str(std::move(content), version);
120 if (file_result.is_ok()) {
121 std::lock_guard lock(recents_mutex);
122 current_get() = file_result.unwrap();
123 }
124 else {
125 recents_print_errors(file_result.unwrap_err());
126 }
127 }
128 }
130 /* Mark ready and wake any waiters. Set while holding the mutex, so a waiter can't
131 * miss the notification between checking the flag and starting to wait. */
132 {
133 std::lock_guard lock(recents_init_mutex);
134 recents_ready.store(true, std::memory_order_release);
135 }
136 recents_init_cv.notify_all();
137}
138
139void init()
140{
142}
143
144void init_async()
145{
146 std::call_once(recents_init_once, []() { std::thread([]() { recents_init_impl(); }).detach(); });
147}
148
150{
151 if (recents_ready.load(std::memory_order_acquire)) {
152 return;
153 }
154
155 /* If async init wasn't started for some reason (like background mode), start
156 * init synchronously. If async init is already scheduled then this call_once
157 * will do nothing and we will wait below for the background thread to finish. */
158 std::call_once(recents_init_once, recents_init_impl);
159
160 if (recents_ready.load(std::memory_order_acquire)) {
161 return;
162 }
163
164 /* Wait using BLI Mutex + condition_variable_any. */
165 std::unique_lock<Mutex> lock(recents_init_mutex);
166 recents_init_cv.wait(lock, [] { return recents_ready.load(std::memory_order_acquire); });
167}
168
169bool save()
170{
171 ensure_init();
172
173 std::lock_guard lock(recents_mutex);
174 toml::value &current = current_get();
175 if (current.is_empty()) {
176 return false;
177 }
178
179 /* Stamp the Blender version that wrote this file, so a future version can tell
180 * how old the on-disk data is and adjust how it's read if the format changes. */
181 current["blender_version"] = BLENDER_VERSION;
183 /* Don't write files when running in background/headless mode. */
184 if (G.background) {
185 return false;
186 }
187
188 std::string toml_as_string = toml::format(current, version);
189 const std::string path = recents_file_path(true);
190 if (path.empty()) {
191 return false;
192 }
193 FILE *file_handle = BLI_fopen(path.c_str(), "w");
194 if (file_handle == nullptr) {
195 return false;
196 }
197 fputs(toml_as_string.c_str(), file_handle);
198 fclose(file_handle);
199 return true;
200}
201
202static const toml::value *recents_find_in(const toml::value &root,
203 const std::string &section_name,
204 const std::string &item_key)
205{
206 if (!root.is_table()) {
207 /* Only tables can contain key/value pairs. */
208 return nullptr;
209 }
210
211 const toml::table &root_table = root.as_table();
212
213 if (section_name.empty()) {
214 /* No section name, so look in the root of the document. */
215 toml::table::const_iterator root_iter = root_table.find(item_key);
216 if (root_iter != root_table.end()) {
217 /* std::pair. first is key, second is value. */
218 return &root_iter->second;
219 }
220 return nullptr;
221 }
222
223 toml::table::const_iterator section_iter = root_table.find(section_name);
224 if (section_iter == root_table.end()) {
225 /* The named section does not exist. */
226 return nullptr;
227 }
228 const toml::value &section_value = section_iter->second;
229 if (!section_value.is_table()) {
230 /* The named section is not a table. */
231 return nullptr;
232 }
233
234 /* Search the named section table for the item key value. */
235 const toml::table &section_table = section_value.as_table();
236 toml::table::const_iterator result = section_table.find(item_key);
237 if (result != section_table.end()) {
238 return &result->second;
239 }
240
241 return nullptr;
242}
243
244template<typename T> T Section::get(const std::string &item_key) const
245{
246 ensure_init();
247
248 std::lock_guard lock(recents_mutex);
249
250 const T default_value{};
251 const toml::value &current = current_get();
252
253 /* Try user value first. */
254 if (const toml::value *v = recents_find_in(current, section_, item_key)) {
255 return toml::get_or(*v, default_value);
256 }
257
258 const toml::value &defaults = defaults_get();
259
260 /* Per-key default value. */
261 if (const toml::value *v = recents_find_in(defaults, section_, item_key)) {
262 return toml::get_or(*v, default_value);
263 }
264
265 /* User's section-level "_default" (user override of section default). */
266 if (!section_.empty()) {
267 if (const toml::value *v = recents_find_in(current, section_, "_default")) {
268 return toml::get_or(*v, default_value);
269 }
270 }
271
272 /* Builtin section-level "_default". */
273 if (!section_.empty()) {
274 if (const toml::value *v = recents_find_in(defaults, section_, "_default")) {
275 return toml::get_or(*v, default_value);
276 }
277 }
278
279 /* Final fallback. False, 0, 0,0f, {}, etc. */
280 return default_value;
281}
282
284
285 * `key = [\n { a = 1, b = 2 },\n]`.
286 */
287static void recents_format_inline_tables(toml::value &value)
288{
289 if (!value.is_array()) {
290 return;
291 }
292
293 bool has_tables = false;
294 for (toml::value &item : value.as_array()) {
295 if (item.is_table()) {
296 item.as_table_fmt().fmt = toml::table_format::oneline;
297 has_tables = true;
298 }
299 }
300 if (has_tables) {
301 value.as_array_fmt().fmt = toml::array_format::multiline;
302 }
303}
304
305template<typename T> void Section::set(const std::string &item_key, const T &value)
306{
307 ensure_init();
308 toml::value &current = current_get();
309
310 std::lock_guard lock(recents_mutex);
311 toml::value *table = &current;
312 if (!section_.empty()) {
313 table = &current[section_];
314 /* Replace with empty table if it's a different data type, to avoid exception. */
315 if (!table->is_table()) {
316 *table = toml::table{};
317 }
318 /* Ensure there is an explicit header like `[project]` in case there
319 * was only implicit `[project.recent]`. Otherwise adding other entries
320 * under `[project]` throws an exception. */
321 else if (table->as_table_fmt().fmt == toml::table_format::implicit) {
322 table->as_table_fmt().fmt = toml::table_format::multiline;
323 }
324 }
325
326 toml::value &item = (*table)[item_key];
327 item = value;
329}
330
331void Section::remove(const std::string &item_key)
332{
333 ensure_init();
334 std::lock_guard lock(recents_mutex);
335 toml::value &current = current_get();
336 if (!current.is_table()) {
337 return;
338 }
339 toml::table &root_table = current.as_table();
340 if (section_.empty()) {
341 root_table.erase(item_key);
342 return;
343 }
344 toml::table::iterator section_iter = root_table.find(section_);
345 if (section_iter == root_table.end()) {
346 return;
347 }
348 toml::value &section_value = section_iter->second;
349 if (!section_value.is_table()) {
350 return;
351 }
352 toml::table &section_table = section_value.as_table();
353 section_table.erase(item_key);
354}
355
357{
358 ensure_init();
359 std::lock_guard lock(recents_mutex);
360 toml::value &current = current_get();
361 if (section_.empty() || !current.is_table()) {
362 return;
363 }
364 toml::table &root_table = current.as_table();
365 root_table.erase(section_);
366}
367
368template std::string Section::get<std::string>(const std::string &item_key) const;
369template void Section::set<std::string>(const std::string &item_key, const std::string &value);
370
371template char Section::get<char>(const std::string &item_key) const;
372template void Section::set<char>(const std::string &item_key, const char &value);
373template bool Section::get<bool>(const std::string &item_key) const;
374template void Section::set<bool>(const std::string &item_key, const bool &value);
375
376template int16_t Section::get<int16_t>(const std::string &item_key) const;
377template void Section::set<int16_t>(const std::string &item_key, const int16_t &value);
378
379template uint16_t Section::get<uint16_t>(const std::string &item_key) const;
380template void Section::set<uint16_t>(const std::string &item_key, const uint16_t &value);
382template int32_t Section::get<int32_t>(const std::string &item_key) const;
383template void Section::set<int32_t>(const std::string &item_key, const int32_t &value);
385template uint32_t Section::get<uint32_t>(const std::string &item_key) const;
386template void Section::set<uint32_t>(const std::string &item_key, const uint32_t &value);
387
388template int64_t Section::get<int64_t>(const std::string &item_key) const;
389template void Section::set<int64_t>(const std::string &item_key, const int64_t &value);
390
391template uint64_t Section::get<uint64_t>(const std::string &item_key) const;
392template void Section::set<uint64_t>(const std::string &item_key, const uint64_t &value);
393
394template float Section::get<float>(const std::string &item_key) const;
395template void Section::set<float>(const std::string &item_key, const float &value);
396
397template double Section::get<double>(const std::string &item_key) const;
398template void Section::set<double>(const std::string &item_key, const double &value);
399
400template std::vector<int> Section::get<std::vector<int>>(const std::string &item_key) const;
401template void Section::set<std::vector<int>>(const std::string &item_key,
402 const std::vector<int> &value);
403
404template std::vector<double> Section::get<std::vector<double>>(const std::string &item_key) const;
405template void Section::set<std::vector<double>>(const std::string &item_key,
406 const std::vector<double> &value);
407
408template std::vector<float> Section::get<std::vector<float>>(const std::string &item_key) const;
409template void Section::set<std::vector<float>>(const std::string &item_key,
410 const std::vector<float> &value);
411
412template std::vector<std::map<std::string, std::string>> Section::get<
413 std::vector<std::map<std::string, std::string>>>(const std::string &item_key) const;
415 const std::string &item_key, const std::vector<std::map<std::string, std::string>> &value);
416
417} // namespace blender::recents
#define BLENDER_RECENTS_FILE
#define BLENDER_VERSION
File and directory operations.
Read Blender memory allocation and freeing.
volatile int lock
void init()
T get(const std::string &item_key) const
Definition recents.cc:224
void set(const std::string &item_key, const T &value)
Definition recents.cc:285
void remove(const std::string &item_key)
Definition recents.cc:311
#define T
int size(const AssetLibraryReference *library_reference)
static std::string recents_file_path(const bool create_folder=false)
Definition recents.cc:42
constexpr toml::spec version
Definition recents.cc:33
static Mutex recents_init_mutex
Definition recents.cc:37
void init_async()
Definition recents.cc:124
void ensure_init()
Definition recents.cc:129
static void recents_init_impl()
Definition recents.cc:84
static std::once_flag recents_init_once
Definition recents.cc:40
static std::condition_variable_any recents_init_cv
Definition recents.cc:39
static const toml::value & defaults_get()
Definition recents.cc:64
static const toml::value * recents_find_in(const toml::value &root, const std::string &section_name, const std::string &item_key)
Definition recents.cc:182
static toml::value & current_get()
Definition recents.cc:78
static void recents_print_errors(const std::vector< toml::error_info > &errors)
Definition recents.cc:56
static Mutex recents_mutex
Definition recents.cc:36
static std::atomic< bool > recents_ready
Definition recents.cc:38
static void recents_format_inline_tables(toml::value &value)
Definition recents.cc:267
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
std::optional< std::string > BKE_appdir_folder_id_create(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:822
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMVert * v
Global G
Definition blender.cc:57
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:539
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:762
BMesh const char void * data
std::mutex Mutex
Definition BLI_mutex.hh:51
signed int int32_t
Definition BKE_text.h:78
@ BLENDER_USER_CONFIG
signed short int16_t
Definition BKE_text.h:77
#define SEP_STR
Definition unit.cc:41