SDL_properties.h 19 KB

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