SDL_properties.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_hints_c.h"
  20. #include "SDL_properties_c.h"
  21. typedef struct
  22. {
  23. SDL_PropertyType type;
  24. union {
  25. void *pointer_value;
  26. char *string_value;
  27. Sint64 number_value;
  28. float float_value;
  29. bool boolean_value;
  30. } value;
  31. char *string_storage;
  32. SDL_CleanupPropertyCallback cleanup;
  33. void *userdata;
  34. } SDL_Property;
  35. typedef struct
  36. {
  37. SDL_HashTable *props;
  38. SDL_Mutex *lock;
  39. } SDL_Properties;
  40. static SDL_InitState SDL_properties_init;
  41. static SDL_HashTable *SDL_properties;
  42. static SDL_AtomicU32 SDL_last_properties_id;
  43. static SDL_AtomicU32 SDL_global_properties;
  44. static void SDL_FreePropertyWithCleanup(const void *key, const void *value, void *data, bool cleanup)
  45. {
  46. SDL_Property *property = (SDL_Property *)value;
  47. if (property) {
  48. switch (property->type) {
  49. case SDL_PROPERTY_TYPE_POINTER:
  50. if (property->cleanup && cleanup) {
  51. property->cleanup(property->userdata, property->value.pointer_value);
  52. }
  53. break;
  54. case SDL_PROPERTY_TYPE_STRING:
  55. SDL_free(property->value.string_value);
  56. break;
  57. default:
  58. break;
  59. }
  60. SDL_free(property->string_storage);
  61. }
  62. SDL_free((void *)key);
  63. SDL_free((void *)value);
  64. }
  65. static void SDL_FreeProperty(const void *key, const void *value, void *data)
  66. {
  67. SDL_FreePropertyWithCleanup(key, value, data, true);
  68. }
  69. static void SDL_FreeProperties(SDL_Properties *properties)
  70. {
  71. if (properties) {
  72. if (properties->props) {
  73. SDL_DestroyHashTable(properties->props);
  74. properties->props = NULL;
  75. }
  76. if (properties->lock) {
  77. SDL_DestroyMutex(properties->lock);
  78. properties->lock = NULL;
  79. }
  80. SDL_free(properties);
  81. }
  82. }
  83. bool SDL_InitProperties(void)
  84. {
  85. if (!SDL_ShouldInit(&SDL_properties_init)) {
  86. return true;
  87. }
  88. SDL_properties = SDL_CreateHashTable(NULL, 16, SDL_HashID, SDL_KeyMatchID, NULL, true, false);
  89. if (!SDL_properties) {
  90. goto error;
  91. }
  92. SDL_SetInitialized(&SDL_properties_init, true);
  93. return true;
  94. error:
  95. SDL_SetInitialized(&SDL_properties_init, true);
  96. SDL_QuitProperties();
  97. return false;
  98. }
  99. void SDL_QuitProperties(void)
  100. {
  101. if (!SDL_ShouldQuit(&SDL_properties_init)) {
  102. return;
  103. }
  104. SDL_PropertiesID props;
  105. do {
  106. props = SDL_GetAtomicU32(&SDL_global_properties);
  107. } while (!SDL_CompareAndSwapAtomicU32(&SDL_global_properties, props, 0));
  108. if (props) {
  109. SDL_DestroyProperties(props);
  110. }
  111. if (SDL_properties) {
  112. void *iter;
  113. const void *key, *value;
  114. iter = NULL;
  115. while (SDL_IterateHashTable(SDL_properties, &key, &value, &iter)) {
  116. SDL_FreeProperties((SDL_Properties *)value);
  117. }
  118. SDL_DestroyHashTable(SDL_properties);
  119. SDL_properties = NULL;
  120. }
  121. SDL_SetInitialized(&SDL_properties_init, false);
  122. }
  123. static bool SDL_CheckInitProperties(void)
  124. {
  125. return SDL_InitProperties();
  126. }
  127. SDL_PropertiesID SDL_GetGlobalProperties(void)
  128. {
  129. SDL_PropertiesID props = SDL_GetAtomicU32(&SDL_global_properties);
  130. if (!props) {
  131. props = SDL_CreateProperties();
  132. if (!SDL_CompareAndSwapAtomicU32(&SDL_global_properties, 0, props)) {
  133. // Somebody else created global properties before us, just use those
  134. SDL_DestroyProperties(props);
  135. props = SDL_GetAtomicU32(&SDL_global_properties);
  136. }
  137. }
  138. return props;
  139. }
  140. SDL_PropertiesID SDL_CreateProperties(void)
  141. {
  142. SDL_PropertiesID props = 0;
  143. SDL_Properties *properties = NULL;
  144. bool inserted = false;
  145. if (!SDL_CheckInitProperties()) {
  146. return 0;
  147. }
  148. properties = (SDL_Properties *)SDL_calloc(1, sizeof(*properties));
  149. if (!properties) {
  150. goto error;
  151. }
  152. properties->props = SDL_CreateHashTable(NULL, 4, SDL_HashString, SDL_KeyMatchString, SDL_FreeProperty, false, false);
  153. if (!properties->props) {
  154. goto error;
  155. }
  156. // If this fails we'll continue without it.
  157. properties->lock = SDL_CreateMutex();
  158. for ( ; ; ) {
  159. props = (SDL_GetAtomicU32(&SDL_last_properties_id) + 1);
  160. if (props == 0) {
  161. continue;
  162. }
  163. if (SDL_CompareAndSwapAtomicU32(&SDL_last_properties_id, props - 1, props)) {
  164. break;
  165. }
  166. }
  167. if (SDL_InsertIntoHashTable(SDL_properties, (const void *)(uintptr_t)props, properties)) {
  168. inserted = true;
  169. }
  170. if (inserted) {
  171. // All done!
  172. return props;
  173. }
  174. error:
  175. SDL_FreeProperties(properties);
  176. return 0;
  177. }
  178. bool SDL_CopyProperties(SDL_PropertiesID src, SDL_PropertiesID dst)
  179. {
  180. SDL_Properties *src_properties = NULL;
  181. SDL_Properties *dst_properties = NULL;
  182. bool result = true;
  183. if (!src) {
  184. return SDL_InvalidParamError("src");
  185. }
  186. if (!dst) {
  187. return SDL_InvalidParamError("dst");
  188. }
  189. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)src, (const void **)&src_properties);
  190. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)dst, (const void **)&dst_properties);
  191. if (!src_properties) {
  192. return SDL_InvalidParamError("src");
  193. }
  194. if (!dst_properties) {
  195. return SDL_InvalidParamError("dst");
  196. }
  197. SDL_LockMutex(src_properties->lock);
  198. SDL_LockMutex(dst_properties->lock);
  199. {
  200. void *iter;
  201. const void *key, *value;
  202. iter = NULL;
  203. while (SDL_IterateHashTable(src_properties->props, &key, &value, &iter)) {
  204. const char *src_name = (const char *)key;
  205. const SDL_Property *src_property = (const SDL_Property *)value;
  206. char *dst_name;
  207. SDL_Property *dst_property;
  208. if (src_property->cleanup) {
  209. // Can't copy properties with cleanup functions, we don't know how to duplicate the data
  210. continue;
  211. }
  212. SDL_RemoveFromHashTable(dst_properties->props, src_name);
  213. dst_name = SDL_strdup(src_name);
  214. if (!dst_name) {
  215. result = false;
  216. continue;
  217. }
  218. dst_property = (SDL_Property *)SDL_malloc(sizeof(*dst_property));
  219. if (!dst_property) {
  220. SDL_free(dst_name);
  221. result = false;
  222. continue;
  223. }
  224. SDL_copyp(dst_property, src_property);
  225. if (src_property->type == SDL_PROPERTY_TYPE_STRING) {
  226. dst_property->value.string_value = SDL_strdup(src_property->value.string_value);
  227. }
  228. if (!SDL_InsertIntoHashTable(dst_properties->props, dst_name, dst_property)) {
  229. SDL_FreePropertyWithCleanup(dst_name, dst_property, NULL, false);
  230. result = false;
  231. }
  232. }
  233. }
  234. SDL_UnlockMutex(dst_properties->lock);
  235. SDL_UnlockMutex(src_properties->lock);
  236. return result;
  237. }
  238. bool SDL_LockProperties(SDL_PropertiesID props)
  239. {
  240. SDL_Properties *properties = NULL;
  241. if (!props) {
  242. return SDL_InvalidParamError("props");
  243. }
  244. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  245. if (!properties) {
  246. return SDL_InvalidParamError("props");
  247. }
  248. SDL_LockMutex(properties->lock);
  249. return true;
  250. }
  251. void SDL_UnlockProperties(SDL_PropertiesID props)
  252. {
  253. SDL_Properties *properties = NULL;
  254. if (!props) {
  255. return;
  256. }
  257. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  258. if (!properties) {
  259. return;
  260. }
  261. SDL_UnlockMutex(properties->lock);
  262. }
  263. static bool SDL_PrivateSetProperty(SDL_PropertiesID props, const char *name, SDL_Property *property)
  264. {
  265. SDL_Properties *properties = NULL;
  266. bool result = true;
  267. if (!props) {
  268. SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
  269. return SDL_InvalidParamError("props");
  270. }
  271. if (!name || !*name) {
  272. SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
  273. return SDL_InvalidParamError("name");
  274. }
  275. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  276. if (!properties) {
  277. SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
  278. return SDL_InvalidParamError("props");
  279. }
  280. SDL_LockMutex(properties->lock);
  281. {
  282. SDL_RemoveFromHashTable(properties->props, name);
  283. if (property) {
  284. char *key = SDL_strdup(name);
  285. if (!SDL_InsertIntoHashTable(properties->props, key, property)) {
  286. SDL_FreePropertyWithCleanup(key, property, NULL, true);
  287. result = false;
  288. }
  289. }
  290. }
  291. SDL_UnlockMutex(properties->lock);
  292. return result;
  293. }
  294. bool SDL_SetPointerPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *value, SDL_CleanupPropertyCallback cleanup, void *userdata)
  295. {
  296. SDL_Property *property;
  297. if (!value) {
  298. if (cleanup) {
  299. cleanup(userdata, value);
  300. }
  301. return SDL_ClearProperty(props, name);
  302. }
  303. property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  304. if (!property) {
  305. if (cleanup) {
  306. cleanup(userdata, value);
  307. }
  308. SDL_FreePropertyWithCleanup(NULL, property, NULL, false);
  309. return false;
  310. }
  311. property->type = SDL_PROPERTY_TYPE_POINTER;
  312. property->value.pointer_value = value;
  313. property->cleanup = cleanup;
  314. property->userdata = userdata;
  315. return SDL_PrivateSetProperty(props, name, property);
  316. }
  317. bool SDL_SetPointerProperty(SDL_PropertiesID props, const char *name, void *value)
  318. {
  319. SDL_Property *property;
  320. if (!value) {
  321. return SDL_ClearProperty(props, name);
  322. }
  323. property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  324. if (!property) {
  325. return false;
  326. }
  327. property->type = SDL_PROPERTY_TYPE_POINTER;
  328. property->value.pointer_value = value;
  329. return SDL_PrivateSetProperty(props, name, property);
  330. }
  331. static void SDLCALL CleanupFreeableProperty(void *userdata, void *value)
  332. {
  333. SDL_free(value);
  334. }
  335. bool SDL_SetFreeableProperty(SDL_PropertiesID props, const char *name, void *value)
  336. {
  337. return SDL_SetPointerPropertyWithCleanup(props, name, value, CleanupFreeableProperty, NULL);
  338. }
  339. static void SDLCALL CleanupSurface(void *userdata, void *value)
  340. {
  341. SDL_Surface *surface = (SDL_Surface *)value;
  342. SDL_DestroySurface(surface);
  343. }
  344. bool SDL_SetSurfaceProperty(SDL_PropertiesID props, const char *name, SDL_Surface *surface)
  345. {
  346. return SDL_SetPointerPropertyWithCleanup(props, name, surface, CleanupSurface, NULL);
  347. }
  348. bool SDL_SetStringProperty(SDL_PropertiesID props, const char *name, const char *value)
  349. {
  350. SDL_Property *property;
  351. if (!value) {
  352. return SDL_ClearProperty(props, name);
  353. }
  354. property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  355. if (!property) {
  356. return false;
  357. }
  358. property->type = SDL_PROPERTY_TYPE_STRING;
  359. property->value.string_value = SDL_strdup(value);
  360. if (!property->value.string_value) {
  361. SDL_free(property);
  362. return false;
  363. }
  364. return SDL_PrivateSetProperty(props, name, property);
  365. }
  366. bool SDL_SetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 value)
  367. {
  368. SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  369. if (!property) {
  370. return false;
  371. }
  372. property->type = SDL_PROPERTY_TYPE_NUMBER;
  373. property->value.number_value = value;
  374. return SDL_PrivateSetProperty(props, name, property);
  375. }
  376. bool SDL_SetFloatProperty(SDL_PropertiesID props, const char *name, float value)
  377. {
  378. SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  379. if (!property) {
  380. return false;
  381. }
  382. property->type = SDL_PROPERTY_TYPE_FLOAT;
  383. property->value.float_value = value;
  384. return SDL_PrivateSetProperty(props, name, property);
  385. }
  386. bool SDL_SetBooleanProperty(SDL_PropertiesID props, const char *name, bool value)
  387. {
  388. SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  389. if (!property) {
  390. return false;
  391. }
  392. property->type = SDL_PROPERTY_TYPE_BOOLEAN;
  393. property->value.boolean_value = value ? true : false;
  394. return SDL_PrivateSetProperty(props, name, property);
  395. }
  396. bool SDL_HasProperty(SDL_PropertiesID props, const char *name)
  397. {
  398. return (SDL_GetPropertyType(props, name) != SDL_PROPERTY_TYPE_INVALID);
  399. }
  400. SDL_PropertyType SDL_GetPropertyType(SDL_PropertiesID props, const char *name)
  401. {
  402. SDL_Properties *properties = NULL;
  403. SDL_PropertyType type = SDL_PROPERTY_TYPE_INVALID;
  404. if (!props) {
  405. return SDL_PROPERTY_TYPE_INVALID;
  406. }
  407. if (!name || !*name) {
  408. return SDL_PROPERTY_TYPE_INVALID;
  409. }
  410. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  411. if (!properties) {
  412. return SDL_PROPERTY_TYPE_INVALID;
  413. }
  414. SDL_LockMutex(properties->lock);
  415. {
  416. SDL_Property *property = NULL;
  417. if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
  418. type = property->type;
  419. }
  420. }
  421. SDL_UnlockMutex(properties->lock);
  422. return type;
  423. }
  424. void *SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *default_value)
  425. {
  426. SDL_Properties *properties = NULL;
  427. void *value = default_value;
  428. if (!props) {
  429. return value;
  430. }
  431. if (!name || !*name) {
  432. return value;
  433. }
  434. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  435. if (!properties) {
  436. return value;
  437. }
  438. /* Note that taking the lock here only guarantees that we won't read the
  439. * hashtable while it's being modified. The value itself can easily be
  440. * freed from another thread after it is returned here.
  441. */
  442. SDL_LockMutex(properties->lock);
  443. {
  444. SDL_Property *property = NULL;
  445. if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
  446. if (property->type == SDL_PROPERTY_TYPE_POINTER) {
  447. value = property->value.pointer_value;
  448. }
  449. }
  450. }
  451. SDL_UnlockMutex(properties->lock);
  452. return value;
  453. }
  454. const char *SDL_GetStringProperty(SDL_PropertiesID props, const char *name, const char *default_value)
  455. {
  456. SDL_Properties *properties = NULL;
  457. const char *value = default_value;
  458. if (!props) {
  459. return value;
  460. }
  461. if (!name || !*name) {
  462. return value;
  463. }
  464. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  465. if (!properties) {
  466. return value;
  467. }
  468. SDL_LockMutex(properties->lock);
  469. {
  470. SDL_Property *property = NULL;
  471. if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
  472. switch (property->type) {
  473. case SDL_PROPERTY_TYPE_STRING:
  474. value = property->value.string_value;
  475. break;
  476. case SDL_PROPERTY_TYPE_NUMBER:
  477. if (property->string_storage) {
  478. value = property->string_storage;
  479. } else {
  480. SDL_asprintf(&property->string_storage, "%" SDL_PRIs64, property->value.number_value);
  481. if (property->string_storage) {
  482. value = property->string_storage;
  483. }
  484. }
  485. break;
  486. case SDL_PROPERTY_TYPE_FLOAT:
  487. if (property->string_storage) {
  488. value = property->string_storage;
  489. } else {
  490. SDL_asprintf(&property->string_storage, "%f", property->value.float_value);
  491. if (property->string_storage) {
  492. value = property->string_storage;
  493. }
  494. }
  495. break;
  496. case SDL_PROPERTY_TYPE_BOOLEAN:
  497. value = property->value.boolean_value ? "true" : "false";
  498. break;
  499. default:
  500. break;
  501. }
  502. }
  503. }
  504. SDL_UnlockMutex(properties->lock);
  505. return value;
  506. }
  507. Sint64 SDL_GetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 default_value)
  508. {
  509. SDL_Properties *properties = NULL;
  510. Sint64 value = default_value;
  511. if (!props) {
  512. return value;
  513. }
  514. if (!name || !*name) {
  515. return value;
  516. }
  517. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  518. if (!properties) {
  519. return value;
  520. }
  521. SDL_LockMutex(properties->lock);
  522. {
  523. SDL_Property *property = NULL;
  524. if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
  525. switch (property->type) {
  526. case SDL_PROPERTY_TYPE_STRING:
  527. value = (Sint64)SDL_strtoll(property->value.string_value, NULL, 0);
  528. break;
  529. case SDL_PROPERTY_TYPE_NUMBER:
  530. value = property->value.number_value;
  531. break;
  532. case SDL_PROPERTY_TYPE_FLOAT:
  533. value = (Sint64)SDL_round((double)property->value.float_value);
  534. break;
  535. case SDL_PROPERTY_TYPE_BOOLEAN:
  536. value = property->value.boolean_value;
  537. break;
  538. default:
  539. break;
  540. }
  541. }
  542. }
  543. SDL_UnlockMutex(properties->lock);
  544. return value;
  545. }
  546. float SDL_GetFloatProperty(SDL_PropertiesID props, const char *name, float default_value)
  547. {
  548. SDL_Properties *properties = NULL;
  549. float value = default_value;
  550. if (!props) {
  551. return value;
  552. }
  553. if (!name || !*name) {
  554. return value;
  555. }
  556. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  557. if (!properties) {
  558. return value;
  559. }
  560. SDL_LockMutex(properties->lock);
  561. {
  562. SDL_Property *property = NULL;
  563. if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
  564. switch (property->type) {
  565. case SDL_PROPERTY_TYPE_STRING:
  566. value = (float)SDL_atof(property->value.string_value);
  567. break;
  568. case SDL_PROPERTY_TYPE_NUMBER:
  569. value = (float)property->value.number_value;
  570. break;
  571. case SDL_PROPERTY_TYPE_FLOAT:
  572. value = property->value.float_value;
  573. break;
  574. case SDL_PROPERTY_TYPE_BOOLEAN:
  575. value = (float)property->value.boolean_value;
  576. break;
  577. default:
  578. break;
  579. }
  580. }
  581. }
  582. SDL_UnlockMutex(properties->lock);
  583. return value;
  584. }
  585. bool SDL_GetBooleanProperty(SDL_PropertiesID props, const char *name, bool default_value)
  586. {
  587. SDL_Properties *properties = NULL;
  588. bool value = default_value ? true : false;
  589. if (!props) {
  590. return value;
  591. }
  592. if (!name || !*name) {
  593. return value;
  594. }
  595. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  596. if (!properties) {
  597. return value;
  598. }
  599. SDL_LockMutex(properties->lock);
  600. {
  601. SDL_Property *property = NULL;
  602. if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
  603. switch (property->type) {
  604. case SDL_PROPERTY_TYPE_STRING:
  605. value = SDL_GetStringBoolean(property->value.string_value, default_value);
  606. break;
  607. case SDL_PROPERTY_TYPE_NUMBER:
  608. value = (property->value.number_value != 0);
  609. break;
  610. case SDL_PROPERTY_TYPE_FLOAT:
  611. value = (property->value.float_value != 0.0f);
  612. break;
  613. case SDL_PROPERTY_TYPE_BOOLEAN:
  614. value = property->value.boolean_value;
  615. break;
  616. default:
  617. break;
  618. }
  619. }
  620. }
  621. SDL_UnlockMutex(properties->lock);
  622. return value;
  623. }
  624. bool SDL_ClearProperty(SDL_PropertiesID props, const char *name)
  625. {
  626. return SDL_PrivateSetProperty(props, name, NULL);
  627. }
  628. bool SDL_EnumerateProperties(SDL_PropertiesID props, SDL_EnumeratePropertiesCallback callback, void *userdata)
  629. {
  630. SDL_Properties *properties = NULL;
  631. if (!props) {
  632. return SDL_InvalidParamError("props");
  633. }
  634. if (!callback) {
  635. return SDL_InvalidParamError("callback");
  636. }
  637. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  638. if (!properties) {
  639. return SDL_InvalidParamError("props");
  640. }
  641. SDL_LockMutex(properties->lock);
  642. {
  643. void *iter;
  644. const void *key, *value;
  645. iter = NULL;
  646. while (SDL_IterateHashTable(properties->props, &key, &value, &iter)) {
  647. callback(userdata, props, (const char *)key);
  648. }
  649. }
  650. SDL_UnlockMutex(properties->lock);
  651. return true;
  652. }
  653. static void SDLCALL SDL_DumpPropertiesCallback(void *userdata, SDL_PropertiesID props, const char *name)
  654. {
  655. switch (SDL_GetPropertyType(props, name)) {
  656. case SDL_PROPERTY_TYPE_POINTER:
  657. SDL_Log("%s: %p\n", name, SDL_GetPointerProperty(props, name, NULL));
  658. break;
  659. case SDL_PROPERTY_TYPE_STRING:
  660. SDL_Log("%s: \"%s\"\n", name, SDL_GetStringProperty(props, name, ""));
  661. break;
  662. case SDL_PROPERTY_TYPE_NUMBER:
  663. {
  664. Sint64 value = SDL_GetNumberProperty(props, name, 0);
  665. SDL_Log("%s: %" SDL_PRIs64 " (%" SDL_PRIx64 ")\n", name, value, value);
  666. }
  667. break;
  668. case SDL_PROPERTY_TYPE_FLOAT:
  669. SDL_Log("%s: %g\n", name, SDL_GetFloatProperty(props, name, 0.0f));
  670. break;
  671. case SDL_PROPERTY_TYPE_BOOLEAN:
  672. SDL_Log("%s: %s\n", name, SDL_GetBooleanProperty(props, name, false) ? "true" : "false");
  673. break;
  674. default:
  675. SDL_Log("%s UNKNOWN TYPE\n", name);
  676. break;
  677. }
  678. }
  679. bool SDL_DumpProperties(SDL_PropertiesID props)
  680. {
  681. return SDL_EnumerateProperties(props, SDL_DumpPropertiesCallback, NULL);
  682. }
  683. void SDL_DestroyProperties(SDL_PropertiesID props)
  684. {
  685. SDL_Properties *properties = NULL;
  686. if (!props) {
  687. return;
  688. }
  689. if (SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties)) {
  690. SDL_FreeProperties(properties);
  691. SDL_RemoveFromHashTable(SDL_properties, (const void *)(uintptr_t)props);
  692. }
  693. }