SDL_properties.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  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. * An ID that represents a properties set.
  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. * \threadsafety It is safe to call this function from any thread.
  103. *
  104. * \since This function is available since SDL 3.2.0.
  105. */
  106. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetGlobalProperties(void);
  107. /**
  108. * Create a group of properties.
  109. *
  110. * All properties are automatically destroyed when SDL_Quit() is called.
  111. *
  112. * \returns an ID for a new group of properties, or 0 on failure; call
  113. * SDL_GetError() for more information.
  114. *
  115. * \threadsafety It is safe to call this function from any thread.
  116. *
  117. * \since This function is available since SDL 3.2.0.
  118. *
  119. * \sa SDL_DestroyProperties
  120. */
  121. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_CreateProperties(void);
  122. /**
  123. * Copy a group of properties.
  124. *
  125. * Copy all the properties from one group of properties to another, with the
  126. * exception of properties requiring cleanup (set using
  127. * SDL_SetPointerPropertyWithCleanup()), which will not be copied. Any
  128. * property that already exists on `dst` will be overwritten.
  129. *
  130. * \param src the properties to copy.
  131. * \param dst the destination properties.
  132. * \returns true on success or false on failure; call SDL_GetError() for more
  133. * information.
  134. *
  135. * \threadsafety It is safe to call this function from any thread. This
  136. * function acquires simultaneous mutex locks on both the source
  137. * and destination property sets.
  138. *
  139. * \since This function is available since SDL 3.2.0.
  140. */
  141. extern SDL_DECLSPEC bool SDLCALL SDL_CopyProperties(SDL_PropertiesID src, SDL_PropertiesID dst);
  142. /**
  143. * Lock a group of properties.
  144. *
  145. * Obtain a multi-threaded lock for these properties. Other threads will wait
  146. * while trying to lock these properties until they are unlocked. Properties
  147. * must be unlocked before they are destroyed.
  148. *
  149. * The lock is automatically taken when setting individual properties, this
  150. * function is only needed when you want to set several properties atomically
  151. * or want to guarantee that properties being queried aren't freed in another
  152. * thread.
  153. *
  154. * \param props the properties to lock.
  155. * \returns true on success or false on failure; call SDL_GetError() for more
  156. * information.
  157. *
  158. * \threadsafety It is safe to call this function from any thread.
  159. *
  160. * \since This function is available since SDL 3.2.0.
  161. *
  162. * \sa SDL_UnlockProperties
  163. */
  164. extern SDL_DECLSPEC bool SDLCALL SDL_LockProperties(SDL_PropertiesID props);
  165. /**
  166. * Unlock a group of properties.
  167. *
  168. * \param props the properties to unlock.
  169. *
  170. * \threadsafety It is safe to call this function from any thread.
  171. *
  172. * \since This function is available since SDL 3.2.0.
  173. *
  174. * \sa SDL_LockProperties
  175. */
  176. extern SDL_DECLSPEC void SDLCALL SDL_UnlockProperties(SDL_PropertiesID props);
  177. /**
  178. * A callback used to free resources when a property is deleted.
  179. *
  180. * This should release any resources associated with `value` that are no
  181. * longer needed.
  182. *
  183. * This callback is set per-property. Different properties in the same group
  184. * can have different cleanup callbacks.
  185. *
  186. * This callback will be called _during_ SDL_SetPointerPropertyWithCleanup if
  187. * the function fails for any reason.
  188. *
  189. * \param userdata an app-defined pointer passed to the callback.
  190. * \param value the pointer assigned to the property to clean up.
  191. *
  192. * \threadsafety This callback may fire without any locks held; if this is a
  193. * concern, the app should provide its own locking.
  194. *
  195. * \since This datatype is available since SDL 3.2.0.
  196. *
  197. * \sa SDL_SetPointerPropertyWithCleanup
  198. */
  199. typedef void (SDLCALL *SDL_CleanupPropertyCallback)(void *userdata, void *value);
  200. /**
  201. * Set a pointer property in a group of properties with a cleanup function
  202. * that is called when the property is deleted.
  203. *
  204. * The cleanup function is also called if setting the property fails for any
  205. * reason.
  206. *
  207. * For simply setting basic data types, like numbers, bools, or strings, use
  208. * SDL_SetNumberProperty, SDL_SetBooleanProperty, or SDL_SetStringProperty
  209. * instead, as those functions will handle cleanup on your behalf. This
  210. * function is only for more complex, custom data.
  211. *
  212. * \param props the properties to modify.
  213. * \param name the name of the property to modify.
  214. * \param value the new value of the property, or NULL to delete the property.
  215. * \param cleanup the function to call when this property is deleted, or NULL
  216. * if no cleanup is necessary.
  217. * \param userdata a pointer that is passed to the cleanup function.
  218. * \returns true on success or false on failure; call SDL_GetError() for more
  219. * information.
  220. *
  221. * \threadsafety It is safe to call this function from any thread.
  222. *
  223. * \since This function is available since SDL 3.2.0.
  224. *
  225. * \sa SDL_GetPointerProperty
  226. * \sa SDL_SetPointerProperty
  227. * \sa SDL_CleanupPropertyCallback
  228. */
  229. extern SDL_DECLSPEC bool SDLCALL SDL_SetPointerPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *value, SDL_CleanupPropertyCallback cleanup, void *userdata);
  230. /**
  231. * Set a pointer property in a group of properties.
  232. *
  233. * \param props the properties to modify.
  234. * \param name the name of the property to modify.
  235. * \param value the new value of the property, or NULL to delete the property.
  236. * \returns true on success or false on failure; call SDL_GetError() for more
  237. * information.
  238. *
  239. * \threadsafety It is safe to call this function from any thread.
  240. *
  241. * \since This function is available since SDL 3.2.0.
  242. *
  243. * \sa SDL_GetPointerProperty
  244. * \sa SDL_HasProperty
  245. * \sa SDL_SetBooleanProperty
  246. * \sa SDL_SetFloatProperty
  247. * \sa SDL_SetNumberProperty
  248. * \sa SDL_SetPointerPropertyWithCleanup
  249. * \sa SDL_SetStringProperty
  250. */
  251. extern SDL_DECLSPEC bool SDLCALL SDL_SetPointerProperty(SDL_PropertiesID props, const char *name, void *value);
  252. /**
  253. * Set a string property in a group of properties.
  254. *
  255. * This function makes a copy of the string; the caller does not have to
  256. * preserve the data after this call completes.
  257. *
  258. * \param props the properties to modify.
  259. * \param name the name of the property to modify.
  260. * \param value the new value of the property, or NULL to delete the property.
  261. * \returns true on success or false on failure; call SDL_GetError() for more
  262. * information.
  263. *
  264. * \threadsafety It is safe to call this function from any thread.
  265. *
  266. * \since This function is available since SDL 3.2.0.
  267. *
  268. * \sa SDL_GetStringProperty
  269. */
  270. extern SDL_DECLSPEC bool SDLCALL SDL_SetStringProperty(SDL_PropertiesID props, const char *name, const char *value);
  271. /**
  272. * Set an integer property in a group of properties.
  273. *
  274. * \param props the properties to modify.
  275. * \param name the name of the property to modify.
  276. * \param value the new value of the property.
  277. * \returns true on success or false on failure; call SDL_GetError() for more
  278. * information.
  279. *
  280. * \threadsafety It is safe to call this function from any thread.
  281. *
  282. * \since This function is available since SDL 3.2.0.
  283. *
  284. * \sa SDL_GetNumberProperty
  285. */
  286. extern SDL_DECLSPEC bool SDLCALL SDL_SetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 value);
  287. /**
  288. * Set a floating point property in a group of properties.
  289. *
  290. * \param props the properties to modify.
  291. * \param name the name of the property to modify.
  292. * \param value the new value of the property.
  293. * \returns true on success or false on failure; call SDL_GetError() for more
  294. * information.
  295. *
  296. * \threadsafety It is safe to call this function from any thread.
  297. *
  298. * \since This function is available since SDL 3.2.0.
  299. *
  300. * \sa SDL_GetFloatProperty
  301. */
  302. extern SDL_DECLSPEC bool SDLCALL SDL_SetFloatProperty(SDL_PropertiesID props, const char *name, float value);
  303. /**
  304. * Set a boolean property in a group of properties.
  305. *
  306. * \param props the properties to modify.
  307. * \param name the name of the property to modify.
  308. * \param value the new value of the property.
  309. * \returns true on success or false on failure; call SDL_GetError() for more
  310. * information.
  311. *
  312. * \threadsafety It is safe to call this function from any thread.
  313. *
  314. * \since This function is available since SDL 3.2.0.
  315. *
  316. * \sa SDL_GetBooleanProperty
  317. */
  318. extern SDL_DECLSPEC bool SDLCALL SDL_SetBooleanProperty(SDL_PropertiesID props, const char *name, bool value);
  319. /**
  320. * Return whether a property exists in a group of properties.
  321. *
  322. * \param props the properties to query.
  323. * \param name the name of the property to query.
  324. * \returns true if the property exists, or false if it doesn't.
  325. *
  326. * \threadsafety It is safe to call this function from any thread.
  327. *
  328. * \since This function is available since SDL 3.2.0.
  329. *
  330. * \sa SDL_GetPropertyType
  331. */
  332. extern SDL_DECLSPEC bool SDLCALL SDL_HasProperty(SDL_PropertiesID props, const char *name);
  333. /**
  334. * Get the type of a property in a group of properties.
  335. *
  336. * \param props the properties to query.
  337. * \param name the name of the property to query.
  338. * \returns the type of the property, or SDL_PROPERTY_TYPE_INVALID if it is
  339. * not set.
  340. *
  341. * \threadsafety It is safe to call this function from any thread.
  342. *
  343. * \since This function is available since SDL 3.2.0.
  344. *
  345. * \sa SDL_HasProperty
  346. */
  347. extern SDL_DECLSPEC SDL_PropertyType SDLCALL SDL_GetPropertyType(SDL_PropertiesID props, const char *name);
  348. /**
  349. * Get a pointer property from a group of properties.
  350. *
  351. * By convention, the names of properties that SDL exposes on objects will
  352. * start with "SDL.", and properties that SDL uses internally will start with
  353. * "SDL.internal.". These should be considered read-only and should not be
  354. * modified by applications.
  355. *
  356. * \param props the properties to query.
  357. * \param name the name of the property to query.
  358. * \param default_value the default value of the property.
  359. * \returns the value of the property, or `default_value` if it is not set or
  360. * not a pointer property.
  361. *
  362. * \threadsafety It is safe to call this function from any thread, although
  363. * the data returned is not protected and could potentially be
  364. * freed if you call SDL_SetPointerProperty() or
  365. * SDL_ClearProperty() on these properties from another thread.
  366. * If you need to avoid this, use SDL_LockProperties() and
  367. * SDL_UnlockProperties().
  368. *
  369. * \since This function is available since SDL 3.2.0.
  370. *
  371. * \sa SDL_GetBooleanProperty
  372. * \sa SDL_GetFloatProperty
  373. * \sa SDL_GetNumberProperty
  374. * \sa SDL_GetPropertyType
  375. * \sa SDL_GetStringProperty
  376. * \sa SDL_HasProperty
  377. * \sa SDL_SetPointerProperty
  378. */
  379. extern SDL_DECLSPEC void * SDLCALL SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *default_value);
  380. /**
  381. * Get a string property from a group of properties.
  382. *
  383. * \param props the properties to query.
  384. * \param name the name of the property to query.
  385. * \param default_value the default value of the property.
  386. * \returns the value of the property, or `default_value` if it is not set or
  387. * not a string property.
  388. *
  389. * \threadsafety It is safe to call this function from any thread, although
  390. * the data returned is not protected and could potentially be
  391. * freed if you call SDL_SetStringProperty() or
  392. * SDL_ClearProperty() on these properties from another thread.
  393. * If you need to avoid this, use SDL_LockProperties() and
  394. * SDL_UnlockProperties().
  395. *
  396. * \since This function is available since SDL 3.2.0.
  397. *
  398. * \sa SDL_GetPropertyType
  399. * \sa SDL_HasProperty
  400. * \sa SDL_SetStringProperty
  401. */
  402. extern SDL_DECLSPEC const char * SDLCALL SDL_GetStringProperty(SDL_PropertiesID props, const char *name, const char *default_value);
  403. /**
  404. * Get a number property from a group of properties.
  405. *
  406. * You can use SDL_GetPropertyType() to query whether the property exists and
  407. * is a number property.
  408. *
  409. * \param props the properties to query.
  410. * \param name the name of the property to query.
  411. * \param default_value the default value of the property.
  412. * \returns the value of the property, or `default_value` if it is not set or
  413. * not a number property.
  414. *
  415. * \threadsafety It is safe to call this function from any thread.
  416. *
  417. * \since This function is available since SDL 3.2.0.
  418. *
  419. * \sa SDL_GetPropertyType
  420. * \sa SDL_HasProperty
  421. * \sa SDL_SetNumberProperty
  422. */
  423. extern SDL_DECLSPEC Sint64 SDLCALL SDL_GetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 default_value);
  424. /**
  425. * Get a floating point property from a group of properties.
  426. *
  427. * You can use SDL_GetPropertyType() to query whether the property exists and
  428. * is a floating point property.
  429. *
  430. * \param props the properties to query.
  431. * \param name the name of the property to query.
  432. * \param default_value the default value of the property.
  433. * \returns the value of the property, or `default_value` if it is not set or
  434. * not a float property.
  435. *
  436. * \threadsafety It is safe to call this function from any thread.
  437. *
  438. * \since This function is available since SDL 3.2.0.
  439. *
  440. * \sa SDL_GetPropertyType
  441. * \sa SDL_HasProperty
  442. * \sa SDL_SetFloatProperty
  443. */
  444. extern SDL_DECLSPEC float SDLCALL SDL_GetFloatProperty(SDL_PropertiesID props, const char *name, float default_value);
  445. /**
  446. * Get a boolean property from a group of properties.
  447. *
  448. * You can use SDL_GetPropertyType() to query whether the property exists and
  449. * is a boolean property.
  450. *
  451. * \param props the properties to query.
  452. * \param name the name of the property to query.
  453. * \param default_value the default value of the property.
  454. * \returns the value of the property, or `default_value` if it is not set or
  455. * not a boolean property.
  456. *
  457. * \threadsafety It is safe to call this function from any thread.
  458. *
  459. * \since This function is available since SDL 3.2.0.
  460. *
  461. * \sa SDL_GetPropertyType
  462. * \sa SDL_HasProperty
  463. * \sa SDL_SetBooleanProperty
  464. */
  465. extern SDL_DECLSPEC bool SDLCALL SDL_GetBooleanProperty(SDL_PropertiesID props, const char *name, bool default_value);
  466. /**
  467. * Clear a property from a group of properties.
  468. *
  469. * \param props the properties to modify.
  470. * \param name the name of the property to clear.
  471. * \returns true on success or false on failure; call SDL_GetError() for more
  472. * information.
  473. *
  474. * \threadsafety It is safe to call this function from any thread.
  475. *
  476. * \since This function is available since SDL 3.2.0.
  477. */
  478. extern SDL_DECLSPEC bool SDLCALL SDL_ClearProperty(SDL_PropertiesID props, const char *name);
  479. /**
  480. * A callback used to enumerate all the properties in a group of properties.
  481. *
  482. * This callback is called from SDL_EnumerateProperties(), and is called once
  483. * per property in the set.
  484. *
  485. * \param userdata an app-defined pointer passed to the callback.
  486. * \param props the SDL_PropertiesID that is being enumerated.
  487. * \param name the next property name in the enumeration.
  488. *
  489. * \threadsafety SDL_EnumerateProperties holds a lock on `props` during this
  490. * callback.
  491. *
  492. * \since This datatype is available since SDL 3.2.0.
  493. *
  494. * \sa SDL_EnumerateProperties
  495. */
  496. typedef void (SDLCALL *SDL_EnumeratePropertiesCallback)(void *userdata, SDL_PropertiesID props, const char *name);
  497. /**
  498. * Enumerate the properties contained in a group of properties.
  499. *
  500. * The callback function is called for each property in the group of
  501. * properties. The properties are locked during enumeration.
  502. *
  503. * \param props the properties to query.
  504. * \param callback the function to call for each property.
  505. * \param userdata a pointer that is passed to `callback`.
  506. * \returns true on success or false on failure; call SDL_GetError() for more
  507. * information.
  508. *
  509. * \threadsafety It is safe to call this function from any thread.
  510. *
  511. * \since This function is available since SDL 3.2.0.
  512. */
  513. extern SDL_DECLSPEC bool SDLCALL SDL_EnumerateProperties(SDL_PropertiesID props, SDL_EnumeratePropertiesCallback callback, void *userdata);
  514. /**
  515. * Destroy a group of properties.
  516. *
  517. * All properties are deleted and their cleanup functions will be called, if
  518. * any.
  519. *
  520. * \param props the properties to destroy.
  521. *
  522. * \threadsafety This function should not be called while these properties are
  523. * locked or other threads might be setting or getting values
  524. * from these properties.
  525. *
  526. * \since This function is available since SDL 3.2.0.
  527. *
  528. * \sa SDL_CreateProperties
  529. */
  530. extern SDL_DECLSPEC void SDLCALL SDL_DestroyProperties(SDL_PropertiesID props);
  531. /* Ends C function definitions when using C++ */
  532. #ifdef __cplusplus
  533. }
  534. #endif
  535. #include <SDL3/SDL_close_code.h>
  536. #endif /* SDL_properties_h_ */