SDL_properties.h 17 KB

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