SDL_properties.h 19 KB

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