SDL_properties.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. /**
  19. * # CategoryProperties
  20. *
  21. * A property is a variable that can be created and retrieved by name at
  22. * runtime.
  23. *
  24. * All properties are part of a property group (SDL_PropertiesID). A property
  25. * group can be created with the SDL_CreateProperties function and destroyed
  26. * with the SDL_DestroyProperties function.
  27. *
  28. * Properties can be added to and retrieved from a property group through the
  29. * following functions:
  30. *
  31. * - SDL_SetPointerProperty and SDL_GetPointerProperty operate on `void*`
  32. * pointer types.
  33. * - SDL_SetStringProperty and SDL_GetStringProperty operate on string types.
  34. * - SDL_SetNumberProperty and SDL_GetNumberProperty operate on signed 64-bit
  35. * integer types.
  36. * - SDL_SetFloatProperty and SDL_GetFloatProperty operate on floating point
  37. * types.
  38. * - SDL_SetBooleanProperty and SDL_GetBooleanProperty operate on boolean
  39. * types.
  40. *
  41. * Properties can be removed from a group by using SDL_ClearProperty.
  42. */
  43. #ifndef SDL_properties_h_
  44. #define SDL_properties_h_
  45. #include <SDL3/SDL_stdinc.h>
  46. #include <SDL3/SDL_error.h>
  47. #include <SDL3/SDL_begin_code.h>
  48. /* Set up for C function definitions, even when using C++ */
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /**
  53. * SDL properties ID
  54. *
  55. * \since This datatype is available since SDL 3.2.0.
  56. */
  57. typedef Uint32 SDL_PropertiesID;
  58. /**
  59. * SDL property type
  60. *
  61. * \since This enum is available since SDL 3.2.0.
  62. */
  63. typedef enum SDL_PropertyType
  64. {
  65. SDL_PROPERTY_TYPE_INVALID,
  66. SDL_PROPERTY_TYPE_POINTER,
  67. SDL_PROPERTY_TYPE_STRING,
  68. SDL_PROPERTY_TYPE_NUMBER,
  69. SDL_PROPERTY_TYPE_FLOAT,
  70. SDL_PROPERTY_TYPE_BOOLEAN
  71. } SDL_PropertyType;
  72. /**
  73. * A generic property for naming things.
  74. *
  75. * This property is intended to be added to any SDL_PropertiesID that needs a
  76. * generic name associated with the property set. It is not guaranteed that
  77. * any property set will include this key, but it is convenient to have a
  78. * standard key that any piece of code could reasonably agree to use.
  79. *
  80. * For example, the properties associated with an SDL_Texture might have a
  81. * name string of "player sprites", or an SDL_AudioStream might have
  82. * "background music", etc. This might also be useful for an SDL_IOStream to
  83. * list the path to its asset.
  84. *
  85. * There is no format for the value set with this key; it is expected to be
  86. * human-readable and informational in nature, possibly for logging or
  87. * debugging purposes.
  88. *
  89. * SDL does not currently set this property on any objects it creates, but
  90. * this may change in later versions; it is currently expected that apps and
  91. * external libraries will take advantage of it, when appropriate.
  92. *
  93. * \since This macro is available since SDL 3.4.0.
  94. */
  95. #define SDL_PROP_NAME_STRING "SDL.name"
  96. /**
  97. * Get the global SDL properties.
  98. *
  99. * \returns a valid property ID on success or 0 on failure; call
  100. * SDL_GetError() for more information.
  101. *
  102. * \since This function is available since SDL 3.2.0.
  103. */
  104. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetGlobalProperties(void);
  105. /**
  106. * Create a group of properties.
  107. *
  108. * All properties are automatically destroyed when SDL_Quit() is called.
  109. *
  110. * \returns an ID for a new group of properties, or 0 on failure; call
  111. * SDL_GetError() for more information.
  112. *
  113. * \threadsafety It is safe to call this function from any thread.
  114. *
  115. * \since This function is available since SDL 3.2.0.
  116. *
  117. * \sa SDL_DestroyProperties
  118. */
  119. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_CreateProperties(void);
  120. /**
  121. * Copy a group of properties.
  122. *
  123. * Copy all the properties from one group of properties to another, with the
  124. * exception of properties requiring cleanup (set using
  125. * SDL_SetPointerPropertyWithCleanup()), which will not be copied. Any
  126. * property that already exists on `dst` will be overwritten.
  127. *
  128. * \param src the properties to copy.
  129. * \param dst the destination properties.
  130. * \returns true on success or false on failure; call SDL_GetError() for more
  131. * information.
  132. *
  133. * \threadsafety It is safe to call this function from any thread. This
  134. * function acquires simultaneous mutex locks on both the source
  135. * and destination property sets.
  136. *
  137. * \since This function is available since SDL 3.2.0.
  138. */
  139. extern SDL_DECLSPEC bool SDLCALL SDL_CopyProperties(SDL_PropertiesID src, SDL_PropertiesID dst);
  140. /**
  141. * Lock a group of properties.
  142. *
  143. * Obtain a multi-threaded lock for these properties. Other threads will wait
  144. * while trying to lock these properties until they are unlocked. Properties
  145. * must be unlocked before they are destroyed.
  146. *
  147. * The lock is automatically taken when setting individual properties, this
  148. * function is only needed when you want to set several properties atomically
  149. * or want to guarantee that properties being queried aren't freed in another
  150. * thread.
  151. *
  152. * \param props the properties to lock.
  153. * \returns true on success or false on failure; call SDL_GetError() for more
  154. * information.
  155. *
  156. * \threadsafety It is safe to call this function from any thread.
  157. *
  158. * \since This function is available since SDL 3.2.0.
  159. *
  160. * \sa SDL_UnlockProperties
  161. */
  162. extern SDL_DECLSPEC bool SDLCALL SDL_LockProperties(SDL_PropertiesID props);
  163. /**
  164. * Unlock a group of properties.
  165. *
  166. * \param props the properties to unlock.
  167. *
  168. * \threadsafety It is safe to call this function from any thread.
  169. *
  170. * \since This function is available since SDL 3.2.0.
  171. *
  172. * \sa SDL_LockProperties
  173. */
  174. extern SDL_DECLSPEC void SDLCALL SDL_UnlockProperties(SDL_PropertiesID props);
  175. /**
  176. * A callback used to free resources when a property is deleted.
  177. *
  178. * This should release any resources associated with `value` that are no
  179. * longer needed.
  180. *
  181. * This callback is set per-property. Different properties in the same group
  182. * can have different cleanup callbacks.
  183. *
  184. * This callback will be called _during_ SDL_SetPointerPropertyWithCleanup if
  185. * the function fails for any reason.
  186. *
  187. * \param userdata an app-defined pointer passed to the callback.
  188. * \param value the pointer assigned to the property to clean up.
  189. *
  190. * \threadsafety This callback may fire without any locks held; if this is a
  191. * concern, the app should provide its own locking.
  192. *
  193. * \since This datatype is available since SDL 3.2.0.
  194. *
  195. * \sa SDL_SetPointerPropertyWithCleanup
  196. */
  197. typedef void (SDLCALL *SDL_CleanupPropertyCallback)(void *userdata, void *value);
  198. /**
  199. * Set a pointer property in a group of properties with a cleanup function
  200. * that is called when the property is deleted.
  201. *
  202. * The cleanup function is also called if setting the property fails for any
  203. * reason.
  204. *
  205. * For simply setting basic data types, like numbers, bools, or strings, use
  206. * SDL_SetNumberProperty, SDL_SetBooleanProperty, or SDL_SetStringProperty
  207. * instead, as those functions will handle cleanup on your behalf. This
  208. * function is only for more complex, custom data.
  209. *
  210. * \param props the properties to modify.
  211. * \param name the name of the property to modify.
  212. * \param value the new value of the property, or NULL to delete the property.
  213. * \param cleanup the function to call when this property is deleted, or NULL
  214. * if no cleanup is necessary.
  215. * \param userdata a pointer that is passed to the cleanup function.
  216. * \returns true on success or false on failure; call SDL_GetError() for more
  217. * information.
  218. *
  219. * \threadsafety It is safe to call this function from any thread.
  220. *
  221. * \since This function is available since SDL 3.2.0.
  222. *
  223. * \sa SDL_GetPointerProperty
  224. * \sa SDL_SetPointerProperty
  225. * \sa SDL_CleanupPropertyCallback
  226. */
  227. extern SDL_DECLSPEC bool SDLCALL SDL_SetPointerPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *value, SDL_CleanupPropertyCallback cleanup, void *userdata);
  228. /**
  229. * Set a pointer property in a group of properties.
  230. *
  231. * \param props the properties to modify.
  232. * \param name the name of the property to modify.
  233. * \param value the new value of the property, or NULL to delete the property.
  234. * \returns true on success or false on failure; call SDL_GetError() for more
  235. * information.
  236. *
  237. * \threadsafety It is safe to call this function from any thread.
  238. *
  239. * \since This function is available since SDL 3.2.0.
  240. *
  241. * \sa SDL_GetPointerProperty
  242. * \sa SDL_HasProperty
  243. * \sa SDL_SetBooleanProperty
  244. * \sa SDL_SetFloatProperty
  245. * \sa SDL_SetNumberProperty
  246. * \sa SDL_SetPointerPropertyWithCleanup
  247. * \sa SDL_SetStringProperty
  248. */
  249. extern SDL_DECLSPEC bool SDLCALL SDL_SetPointerProperty(SDL_PropertiesID props, const char *name, void *value);
  250. /**
  251. * Set a string property in a group of properties.
  252. *
  253. * This function makes a copy of the string; the caller does not have to
  254. * preserve the data after this call completes.
  255. *
  256. * \param props the properties to modify.
  257. * \param name the name of the property to modify.
  258. * \param value the new value of the property, or NULL to delete the property.
  259. * \returns true on success or false on failure; call SDL_GetError() for more
  260. * information.
  261. *
  262. * \threadsafety It is safe to call this function from any thread.
  263. *
  264. * \since This function is available since SDL 3.2.0.
  265. *
  266. * \sa SDL_GetStringProperty
  267. */
  268. extern SDL_DECLSPEC bool SDLCALL SDL_SetStringProperty(SDL_PropertiesID props, const char *name, const char *value);
  269. /**
  270. * Set an integer property in a group of properties.
  271. *
  272. * \param props the properties to modify.
  273. * \param name the name of the property to modify.
  274. * \param value the new value of the property.
  275. * \returns true on success or false on failure; call SDL_GetError() for more
  276. * information.
  277. *
  278. * \threadsafety It is safe to call this function from any thread.
  279. *
  280. * \since This function is available since SDL 3.2.0.
  281. *
  282. * \sa SDL_GetNumberProperty
  283. */
  284. extern SDL_DECLSPEC bool SDLCALL SDL_SetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 value);
  285. /**
  286. * Set a floating point property in a group of properties.
  287. *
  288. * \param props the properties to modify.
  289. * \param name the name of the property to modify.
  290. * \param value the new value of the property.
  291. * \returns true on success or false on failure; call SDL_GetError() for more
  292. * information.
  293. *
  294. * \threadsafety It is safe to call this function from any thread.
  295. *
  296. * \since This function is available since SDL 3.2.0.
  297. *
  298. * \sa SDL_GetFloatProperty
  299. */
  300. extern SDL_DECLSPEC bool SDLCALL SDL_SetFloatProperty(SDL_PropertiesID props, const char *name, float value);
  301. /**
  302. * Set a boolean property in a group of properties.
  303. *
  304. * \param props the properties to modify.
  305. * \param name the name of the property to modify.
  306. * \param value the new value of the property.
  307. * \returns true on success or false on failure; call SDL_GetError() for more
  308. * information.
  309. *
  310. * \threadsafety It is safe to call this function from any thread.
  311. *
  312. * \since This function is available since SDL 3.2.0.
  313. *
  314. * \sa SDL_GetBooleanProperty
  315. */
  316. extern SDL_DECLSPEC bool SDLCALL SDL_SetBooleanProperty(SDL_PropertiesID props, const char *name, bool value);
  317. /**
  318. * Return whether a property exists in a group of properties.
  319. *
  320. * \param props the properties to query.
  321. * \param name the name of the property to query.
  322. * \returns true if the property exists, or false if it doesn't.
  323. *
  324. * \threadsafety It is safe to call this function from any thread.
  325. *
  326. * \since This function is available since SDL 3.2.0.
  327. *
  328. * \sa SDL_GetPropertyType
  329. */
  330. extern SDL_DECLSPEC bool SDLCALL SDL_HasProperty(SDL_PropertiesID props, const char *name);
  331. /**
  332. * Get the type of a property in a group of properties.
  333. *
  334. * \param props the properties to query.
  335. * \param name the name of the property to query.
  336. * \returns the type of the property, or SDL_PROPERTY_TYPE_INVALID if it is
  337. * not set.
  338. *
  339. * \threadsafety It is safe to call this function from any thread.
  340. *
  341. * \since This function is available since SDL 3.2.0.
  342. *
  343. * \sa SDL_HasProperty
  344. */
  345. extern SDL_DECLSPEC SDL_PropertyType SDLCALL SDL_GetPropertyType(SDL_PropertiesID props, const char *name);
  346. /**
  347. * Get a pointer property from a group of properties.
  348. *
  349. * By convention, the names of properties that SDL exposes on objects will
  350. * start with "SDL.", and properties that SDL uses internally will start with
  351. * "SDL.internal.". These should be considered read-only and should not be
  352. * modified by applications.
  353. *
  354. * \param props the properties to query.
  355. * \param name the name of the property to query.
  356. * \param default_value the default value of the property.
  357. * \returns the value of the property, or `default_value` if it is not set or
  358. * not a pointer property.
  359. *
  360. * \threadsafety It is safe to call this function from any thread, although
  361. * the data returned is not protected and could potentially be
  362. * freed if you call SDL_SetPointerProperty() or
  363. * SDL_ClearProperty() on these properties from another thread.
  364. * If you need to avoid this, use SDL_LockProperties() and
  365. * SDL_UnlockProperties().
  366. *
  367. * \since This function is available since SDL 3.2.0.
  368. *
  369. * \sa SDL_GetBooleanProperty
  370. * \sa SDL_GetFloatProperty
  371. * \sa SDL_GetNumberProperty
  372. * \sa SDL_GetPropertyType
  373. * \sa SDL_GetStringProperty
  374. * \sa SDL_HasProperty
  375. * \sa SDL_SetPointerProperty
  376. */
  377. extern SDL_DECLSPEC void * SDLCALL SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *default_value);
  378. /**
  379. * Get a string property from a group of properties.
  380. *
  381. * \param props the properties to query.
  382. * \param name the name of the property to query.
  383. * \param default_value the default value of the property.
  384. * \returns the value of the property, or `default_value` if it is not set or
  385. * not a string property.
  386. *
  387. * \threadsafety It is safe to call this function from any thread, although
  388. * the data returned is not protected and could potentially be
  389. * freed if you call SDL_SetStringProperty() or
  390. * SDL_ClearProperty() on these properties from another thread.
  391. * If you need to avoid this, use SDL_LockProperties() and
  392. * SDL_UnlockProperties().
  393. *
  394. * \since This function is available since SDL 3.2.0.
  395. *
  396. * \sa SDL_GetPropertyType
  397. * \sa SDL_HasProperty
  398. * \sa SDL_SetStringProperty
  399. */
  400. extern SDL_DECLSPEC const char * SDLCALL SDL_GetStringProperty(SDL_PropertiesID props, const char *name, const char *default_value);
  401. /**
  402. * Get a number property from a group of properties.
  403. *
  404. * You can use SDL_GetPropertyType() to query whether the property exists and
  405. * is a number property.
  406. *
  407. * \param props the properties to query.
  408. * \param name the name of the property to query.
  409. * \param default_value the default value of the property.
  410. * \returns the value of the property, or `default_value` if it is not set or
  411. * not a number property.
  412. *
  413. * \threadsafety It is safe to call this function from any thread.
  414. *
  415. * \since This function is available since SDL 3.2.0.
  416. *
  417. * \sa SDL_GetPropertyType
  418. * \sa SDL_HasProperty
  419. * \sa SDL_SetNumberProperty
  420. */
  421. extern SDL_DECLSPEC Sint64 SDLCALL SDL_GetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 default_value);
  422. /**
  423. * Get a floating point property from a group of properties.
  424. *
  425. * You can use SDL_GetPropertyType() to query whether the property exists and
  426. * is a floating point property.
  427. *
  428. * \param props the properties to query.
  429. * \param name the name of the property to query.
  430. * \param default_value the default value of the property.
  431. * \returns the value of the property, or `default_value` if it is not set or
  432. * not a float property.
  433. *
  434. * \threadsafety It is safe to call this function from any thread.
  435. *
  436. * \since This function is available since SDL 3.2.0.
  437. *
  438. * \sa SDL_GetPropertyType
  439. * \sa SDL_HasProperty
  440. * \sa SDL_SetFloatProperty
  441. */
  442. extern SDL_DECLSPEC float SDLCALL SDL_GetFloatProperty(SDL_PropertiesID props, const char *name, float default_value);
  443. /**
  444. * Get a boolean property from a group of properties.
  445. *
  446. * You can use SDL_GetPropertyType() to query whether the property exists and
  447. * is a boolean property.
  448. *
  449. * \param props the properties to query.
  450. * \param name the name of the property to query.
  451. * \param default_value the default value of the property.
  452. * \returns the value of the property, or `default_value` if it is not set or
  453. * not a boolean property.
  454. *
  455. * \threadsafety It is safe to call this function from any thread.
  456. *
  457. * \since This function is available since SDL 3.2.0.
  458. *
  459. * \sa SDL_GetPropertyType
  460. * \sa SDL_HasProperty
  461. * \sa SDL_SetBooleanProperty
  462. */
  463. extern SDL_DECLSPEC bool SDLCALL SDL_GetBooleanProperty(SDL_PropertiesID props, const char *name, bool default_value);
  464. /**
  465. * Clear a property from a group of properties.
  466. *
  467. * \param props the properties to modify.
  468. * \param name the name of the property to clear.
  469. * \returns true on success or false on failure; call SDL_GetError() for more
  470. * information.
  471. *
  472. * \threadsafety It is safe to call this function from any thread.
  473. *
  474. * \since This function is available since SDL 3.2.0.
  475. */
  476. extern SDL_DECLSPEC bool SDLCALL SDL_ClearProperty(SDL_PropertiesID props, const char *name);
  477. /**
  478. * A callback used to enumerate all the properties in a group of properties.
  479. *
  480. * This callback is called from SDL_EnumerateProperties(), and is called once
  481. * per property in the set.
  482. *
  483. * \param userdata an app-defined pointer passed to the callback.
  484. * \param props the SDL_PropertiesID that is being enumerated.
  485. * \param name the next property name in the enumeration.
  486. *
  487. * \threadsafety SDL_EnumerateProperties holds a lock on `props` during this
  488. * callback.
  489. *
  490. * \since This datatype is available since SDL 3.2.0.
  491. *
  492. * \sa SDL_EnumerateProperties
  493. */
  494. typedef void (SDLCALL *SDL_EnumeratePropertiesCallback)(void *userdata, SDL_PropertiesID props, const char *name);
  495. /**
  496. * Enumerate the properties contained in a group of properties.
  497. *
  498. * The callback function is called for each property in the group of
  499. * properties. The properties are locked during enumeration.
  500. *
  501. * \param props the properties to query.
  502. * \param callback the function to call for each property.
  503. * \param userdata a pointer that is passed to `callback`.
  504. * \returns true on success or false on failure; call SDL_GetError() for more
  505. * information.
  506. *
  507. * \threadsafety It is safe to call this function from any thread.
  508. *
  509. * \since This function is available since SDL 3.2.0.
  510. */
  511. extern SDL_DECLSPEC bool SDLCALL SDL_EnumerateProperties(SDL_PropertiesID props, SDL_EnumeratePropertiesCallback callback, void *userdata);
  512. /**
  513. * Destroy a group of properties.
  514. *
  515. * All properties are deleted and their cleanup functions will be called, if
  516. * any.
  517. *
  518. * \param props the properties to destroy.
  519. *
  520. * \threadsafety This function should not be called while these properties are
  521. * locked or other threads might be setting or getting values
  522. * from these properties.
  523. *
  524. * \since This function is available since SDL 3.2.0.
  525. *
  526. * \sa SDL_CreateProperties
  527. */
  528. extern SDL_DECLSPEC void SDLCALL SDL_DestroyProperties(SDL_PropertiesID props);
  529. /* Ends C function definitions when using C++ */
  530. #ifdef __cplusplus
  531. }
  532. #endif
  533. #include <SDL3/SDL_close_code.h>
  534. #endif /* SDL_properties_h_ */