SDL_properties.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 SDLCALL SDL_FreeProperty(void *data, const void *key, const void *value)
  66. {
  67. SDL_FreePropertyWithCleanup(key, value, data, true);
  68. }
  69. static void SDL_FreeProperties(SDL_Properties *properties)
  70. {
  71. if (properties) {
  72. SDL_DestroyHashTable(properties->props);
  73. SDL_DestroyMutex(properties->lock);
  74. SDL_free(properties);
  75. }
  76. }
  77. bool SDL_InitProperties(void)
  78. {
  79. if (!SDL_ShouldInit(&SDL_properties_init)) {
  80. return true;
  81. }
  82. SDL_properties = SDL_CreateHashTable(0, true, SDL_HashID, SDL_KeyMatchID, NULL, NULL);
  83. const bool initialized = (SDL_properties != NULL);
  84. SDL_SetInitialized(&SDL_properties_init, initialized);
  85. return initialized;
  86. }
  87. static bool SDLCALL FreeOneProperties(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
  88. {
  89. SDL_FreeProperties((SDL_Properties *)value);
  90. return true; // keep iterating.
  91. }
  92. void SDL_QuitProperties(void)
  93. {
  94. if (!SDL_ShouldQuit(&SDL_properties_init)) {
  95. return;
  96. }
  97. SDL_PropertiesID props;
  98. do {
  99. props = SDL_GetAtomicU32(&SDL_global_properties);
  100. } while (!SDL_CompareAndSwapAtomicU32(&SDL_global_properties, props, 0));
  101. if (props) {
  102. SDL_DestroyProperties(props);
  103. }
  104. // this can't just DestroyHashTable with SDL_FreeProperties as the destructor, because
  105. // other destructors under this might cause use to attempt a recursive lock on SDL_properties,
  106. // which isn't allowed with rwlocks. So manually iterate and free everything.
  107. SDL_HashTable *properties = SDL_properties;
  108. SDL_properties = NULL;
  109. SDL_IterateHashTable(properties, FreeOneProperties, NULL);
  110. SDL_DestroyHashTable(properties);
  111. SDL_SetInitialized(&SDL_properties_init, false);
  112. }
  113. static bool SDL_CheckInitProperties(void)
  114. {
  115. return SDL_InitProperties();
  116. }
  117. SDL_PropertiesID SDL_GetGlobalProperties(void)
  118. {
  119. SDL_PropertiesID props = SDL_GetAtomicU32(&SDL_global_properties);
  120. if (!props) {
  121. props = SDL_CreateProperties();
  122. if (!SDL_CompareAndSwapAtomicU32(&SDL_global_properties, 0, props)) {
  123. // Somebody else created global properties before us, just use those
  124. SDL_DestroyProperties(props);
  125. props = SDL_GetAtomicU32(&SDL_global_properties);
  126. }
  127. }
  128. return props;
  129. }
  130. SDL_PropertiesID SDL_CreateProperties(void)
  131. {
  132. if (!SDL_CheckInitProperties()) {
  133. return 0;
  134. }
  135. SDL_Properties *properties = (SDL_Properties *)SDL_calloc(1, sizeof(*properties));
  136. if (!properties) {
  137. return 0;
  138. }
  139. properties->lock = SDL_CreateMutex();
  140. if (!properties->lock) {
  141. SDL_free(properties);
  142. return 0;
  143. }
  144. properties->props = SDL_CreateHashTable(0, false, SDL_HashString, SDL_KeyMatchString, SDL_FreeProperty, NULL);
  145. if (!properties->props) {
  146. SDL_DestroyMutex(properties->lock);
  147. SDL_free(properties);
  148. return 0;
  149. }
  150. SDL_PropertiesID props = 0;
  151. while (true) {
  152. props = (SDL_GetAtomicU32(&SDL_last_properties_id) + 1);
  153. if (props == 0) {
  154. continue;
  155. } else if (SDL_CompareAndSwapAtomicU32(&SDL_last_properties_id, props - 1, props)) {
  156. break;
  157. }
  158. }
  159. SDL_assert(!SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, NULL)); // should NOT be in the hash table already.
  160. if (!SDL_InsertIntoHashTable(SDL_properties, (const void *)(uintptr_t)props, properties, false)) {
  161. SDL_FreeProperties(properties);
  162. return 0;
  163. }
  164. return props; // All done!
  165. }
  166. typedef struct CopyOnePropertyData
  167. {
  168. SDL_Properties *dst_properties;
  169. bool result;
  170. } CopyOnePropertyData;
  171. static bool SDLCALL CopyOneProperty(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
  172. {
  173. const SDL_Property *src_property = (const SDL_Property *)value;
  174. if (src_property->cleanup) {
  175. // Can't copy properties with cleanup functions, we don't know how to duplicate the data
  176. return true; // keep iterating.
  177. }
  178. CopyOnePropertyData *data = (CopyOnePropertyData *) userdata;
  179. SDL_Properties *dst_properties = data->dst_properties;
  180. const char *src_name = (const char *)key;
  181. SDL_Property *dst_property;
  182. char *dst_name = SDL_strdup(src_name);
  183. if (!dst_name) {
  184. data->result = false;
  185. return true; // keep iterating (I guess...?)
  186. }
  187. dst_property = (SDL_Property *)SDL_malloc(sizeof(*dst_property));
  188. if (!dst_property) {
  189. SDL_free(dst_name);
  190. data->result = false;
  191. return true; // keep iterating (I guess...?)
  192. }
  193. SDL_copyp(dst_property, src_property);
  194. if (src_property->type == SDL_PROPERTY_TYPE_STRING) {
  195. dst_property->value.string_value = SDL_strdup(src_property->value.string_value);
  196. if (!dst_property->value.string_value) {
  197. SDL_free(dst_name);
  198. SDL_free(dst_property);
  199. data->result = false;
  200. return true; // keep iterating (I guess...?)
  201. }
  202. }
  203. if (!SDL_InsertIntoHashTable(dst_properties->props, dst_name, dst_property, true)) {
  204. SDL_FreePropertyWithCleanup(dst_name, dst_property, NULL, false);
  205. data->result = false;
  206. }
  207. return true; // keep iterating.
  208. }
  209. bool SDL_CopyProperties(SDL_PropertiesID src, SDL_PropertiesID dst)
  210. {
  211. if (!src) {
  212. return SDL_InvalidParamError("src");
  213. }
  214. if (!dst) {
  215. return SDL_InvalidParamError("dst");
  216. }
  217. SDL_Properties *src_properties = NULL;
  218. SDL_Properties *dst_properties = NULL;
  219. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)src, (const void **)&src_properties);
  220. if (!src_properties) {
  221. return SDL_InvalidParamError("src");
  222. }
  223. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)dst, (const void **)&dst_properties);
  224. if (!dst_properties) {
  225. return SDL_InvalidParamError("dst");
  226. }
  227. bool result = true;
  228. SDL_LockMutex(src_properties->lock);
  229. SDL_LockMutex(dst_properties->lock);
  230. {
  231. CopyOnePropertyData data = { dst_properties, true };
  232. SDL_IterateHashTable(src_properties->props, CopyOneProperty, &data);
  233. result = data.result;
  234. }
  235. SDL_UnlockMutex(dst_properties->lock);
  236. SDL_UnlockMutex(src_properties->lock);
  237. return result;
  238. }
  239. bool SDL_LockProperties(SDL_PropertiesID props)
  240. {
  241. SDL_Properties *properties = NULL;
  242. if (!props) {
  243. return SDL_InvalidParamError("props");
  244. }
  245. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  246. if (!properties) {
  247. return SDL_InvalidParamError("props");
  248. }
  249. SDL_LockMutex(properties->lock);
  250. return true;
  251. }
  252. void SDL_UnlockProperties(SDL_PropertiesID props)
  253. {
  254. SDL_Properties *properties = NULL;
  255. if (!props) {
  256. return;
  257. }
  258. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  259. if (!properties) {
  260. return;
  261. }
  262. SDL_UnlockMutex(properties->lock);
  263. }
  264. static bool SDL_PrivateSetProperty(SDL_PropertiesID props, const char *name, SDL_Property *property)
  265. {
  266. SDL_Properties *properties = NULL;
  267. bool result = true;
  268. if (!props) {
  269. SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
  270. return SDL_InvalidParamError("props");
  271. }
  272. if (!name || !*name) {
  273. SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
  274. return SDL_InvalidParamError("name");
  275. }
  276. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  277. if (!properties) {
  278. SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
  279. return SDL_InvalidParamError("props");
  280. }
  281. SDL_LockMutex(properties->lock);
  282. {
  283. SDL_RemoveFromHashTable(properties->props, name);
  284. if (property) {
  285. char *key = SDL_strdup(name);
  286. if (!key || !SDL_InsertIntoHashTable(properties->props, key, property, false)) {
  287. SDL_FreePropertyWithCleanup(key, property, NULL, true);
  288. result = false;
  289. }
  290. }
  291. }
  292. SDL_UnlockMutex(properties->lock);
  293. return result;
  294. }
  295. bool SDL_SetPointerPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *value, SDL_CleanupPropertyCallback cleanup, void *userdata)
  296. {
  297. SDL_Property *property;
  298. if (!value) {
  299. if (cleanup) {
  300. cleanup(userdata, value);
  301. }
  302. return SDL_ClearProperty(props, name);
  303. }
  304. property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  305. if (!property) {
  306. if (cleanup) {
  307. cleanup(userdata, value);
  308. }
  309. SDL_FreePropertyWithCleanup(NULL, property, NULL, false);
  310. return false;
  311. }
  312. property->type = SDL_PROPERTY_TYPE_POINTER;
  313. property->value.pointer_value = value;
  314. property->cleanup = cleanup;
  315. property->userdata = userdata;
  316. return SDL_PrivateSetProperty(props, name, property);
  317. }
  318. bool SDL_SetPointerProperty(SDL_PropertiesID props, const char *name, void *value)
  319. {
  320. SDL_Property *property;
  321. if (!value) {
  322. return SDL_ClearProperty(props, name);
  323. }
  324. property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  325. if (!property) {
  326. return false;
  327. }
  328. property->type = SDL_PROPERTY_TYPE_POINTER;
  329. property->value.pointer_value = value;
  330. return SDL_PrivateSetProperty(props, name, property);
  331. }
  332. static void SDLCALL CleanupFreeableProperty(void *userdata, void *value)
  333. {
  334. SDL_free(value);
  335. }
  336. bool SDL_SetFreeableProperty(SDL_PropertiesID props, const char *name, void *value)
  337. {
  338. return SDL_SetPointerPropertyWithCleanup(props, name, value, CleanupFreeableProperty, NULL);
  339. }
  340. static void SDLCALL CleanupSurface(void *userdata, void *value)
  341. {
  342. SDL_Surface *surface = (SDL_Surface *)value;
  343. //SDL_DestroySurface(surface);
  344. }
  345. bool SDL_SetSurfaceProperty(SDL_PropertiesID props, const char *name, SDL_Surface *surface)
  346. {
  347. return SDL_SetPointerPropertyWithCleanup(props, name, surface, CleanupSurface, NULL);
  348. }
  349. bool SDL_SetStringProperty(SDL_PropertiesID props, const char *name, const char *value)
  350. {
  351. SDL_Property *property;
  352. if (!value) {
  353. return SDL_ClearProperty(props, name);
  354. }
  355. property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  356. if (!property) {
  357. return false;
  358. }
  359. property->type = SDL_PROPERTY_TYPE_STRING;
  360. property->value.string_value = SDL_strdup(value);
  361. if (!property->value.string_value) {
  362. SDL_free(property);
  363. return false;
  364. }
  365. return SDL_PrivateSetProperty(props, name, property);
  366. }
  367. bool SDL_SetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 value)
  368. {
  369. SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  370. if (!property) {
  371. return false;
  372. }
  373. property->type = SDL_PROPERTY_TYPE_NUMBER;
  374. property->value.number_value = value;
  375. return SDL_PrivateSetProperty(props, name, property);
  376. }
  377. bool SDL_SetFloatProperty(SDL_PropertiesID props, const char *name, float value)
  378. {
  379. SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  380. if (!property) {
  381. return false;
  382. }
  383. property->type = SDL_PROPERTY_TYPE_FLOAT;
  384. property->value.float_value = value;
  385. return SDL_PrivateSetProperty(props, name, property);
  386. }
  387. bool SDL_SetBooleanProperty(SDL_PropertiesID props, const char *name, bool value)
  388. {
  389. SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
  390. if (!property) {
  391. return false;
  392. }
  393. property->type = SDL_PROPERTY_TYPE_BOOLEAN;
  394. property->value.boolean_value = value ? true : false;
  395. return SDL_PrivateSetProperty(props, name, property);
  396. }
  397. bool SDL_HasProperty(SDL_PropertiesID props, const char *name)
  398. {
  399. return (SDL_GetPropertyType(props, name) != SDL_PROPERTY_TYPE_INVALID);
  400. }
  401. SDL_PropertyType SDL_GetPropertyType(SDL_PropertiesID props, const char *name)
  402. {
  403. SDL_Properties *properties = NULL;
  404. SDL_PropertyType type = SDL_PROPERTY_TYPE_INVALID;
  405. if (!props) {
  406. return SDL_PROPERTY_TYPE_INVALID;
  407. }
  408. if (!name || !*name) {
  409. return SDL_PROPERTY_TYPE_INVALID;
  410. }
  411. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  412. if (!properties) {
  413. return SDL_PROPERTY_TYPE_INVALID;
  414. }
  415. SDL_LockMutex(properties->lock);
  416. {
  417. SDL_Property *property = NULL;
  418. if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
  419. type = property->type;
  420. }
  421. }
  422. SDL_UnlockMutex(properties->lock);
  423. return type;
  424. }
  425. void *SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *default_value)
  426. {
  427. SDL_Properties *properties = NULL;
  428. void *value = default_value;
  429. if (!props) {
  430. return value;
  431. }
  432. if (!name || !*name) {
  433. return value;
  434. }
  435. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  436. if (!properties) {
  437. return value;
  438. }
  439. // Note that taking the lock here only guarantees that we won't read the
  440. // hashtable while it's being modified. The value itself can easily be
  441. // freed from another thread after it is returned here.
  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. typedef struct EnumerateOnePropertyData
  629. {
  630. SDL_EnumeratePropertiesCallback callback;
  631. void *userdata;
  632. SDL_PropertiesID props;
  633. } EnumerateOnePropertyData;
  634. static bool SDLCALL EnumerateOneProperty(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
  635. {
  636. (void) table;
  637. (void) value;
  638. const EnumerateOnePropertyData *data = (const EnumerateOnePropertyData *) userdata;
  639. data->callback(data->userdata, data->props, (const char *)key);
  640. return true; // keep iterating.
  641. }
  642. bool SDL_EnumerateProperties(SDL_PropertiesID props, SDL_EnumeratePropertiesCallback callback, void *userdata)
  643. {
  644. SDL_Properties *properties = NULL;
  645. if (!props) {
  646. return SDL_InvalidParamError("props");
  647. }
  648. if (!callback) {
  649. return SDL_InvalidParamError("callback");
  650. }
  651. SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
  652. if (!properties) {
  653. return SDL_InvalidParamError("props");
  654. }
  655. SDL_LockMutex(properties->lock);
  656. {
  657. EnumerateOnePropertyData data = { callback, userdata, props };
  658. SDL_IterateHashTable(properties->props, EnumerateOneProperty, &data);
  659. }
  660. SDL_UnlockMutex(properties->lock);
  661. return true;
  662. }
  663. static void SDLCALL SDL_DumpPropertiesCallback(void *userdata, SDL_PropertiesID props, const char *name)
  664. {
  665. switch (SDL_GetPropertyType(props, name)) {
  666. case SDL_PROPERTY_TYPE_POINTER:
  667. SDL_Log("%s: %p", name, SDL_GetPointerProperty(props, name, NULL));
  668. break;
  669. case SDL_PROPERTY_TYPE_STRING:
  670. SDL_Log("%s: \"%s\"", name, SDL_GetStringProperty(props, name, ""));
  671. break;
  672. case SDL_PROPERTY_TYPE_NUMBER:
  673. {
  674. Sint64 value = SDL_GetNumberProperty(props, name, 0);
  675. SDL_Log("%s: %" SDL_PRIs64 " (%" SDL_PRIx64 ")", name, value, value);
  676. }
  677. break;
  678. case SDL_PROPERTY_TYPE_FLOAT:
  679. SDL_Log("%s: %g", name, SDL_GetFloatProperty(props, name, 0.0f));
  680. break;
  681. case SDL_PROPERTY_TYPE_BOOLEAN:
  682. SDL_Log("%s: %s", name, SDL_GetBooleanProperty(props, name, false) ? "true" : "false");
  683. break;
  684. default:
  685. SDL_Log("%s UNKNOWN TYPE", name);
  686. break;
  687. }
  688. }
  689. bool SDL_DumpProperties(SDL_PropertiesID props)
  690. {
  691. return SDL_EnumerateProperties(props, SDL_DumpPropertiesCallback, NULL);
  692. }
  693. void SDL_DestroyProperties(SDL_PropertiesID props)
  694. {
  695. if (props) {
  696. // this can't just use RemoveFromHashTable with SDL_FreeProperties as the destructor, because
  697. // other destructors under this might cause use to attempt a recursive lock on SDL_properties,
  698. // which isn't allowed with rwlocks. So manually look it up and remove/free it.
  699. SDL_Properties *properties = NULL;
  700. if (SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties)) {
  701. SDL_FreeProperties(properties);
  702. SDL_RemoveFromHashTable(SDL_properties, (const void *)(uintptr_t)props);
  703. }
  704. }
  705. }