SDL 3.0
SDL_properties.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryProperties
24 *
25 * A property is a variable that can be created and retrieved by name at
26 * runtime.
27 *
28 * All properties are part of a property group (SDL_PropertiesID). A property
29 * group can be created with the SDL_CreateProperties function and destroyed
30 * with the SDL_DestroyProperties function.
31 *
32 * Properties can be added to and retrieved from a property group through the
33 * following functions:
34 *
35 * - SDL_SetPointerProperty and SDL_GetPointerProperty operate on `void*`
36 * pointer types.
37 * - SDL_SetStringProperty and SDL_GetStringProperty operate on string types.
38 * - SDL_SetNumberProperty and SDL_GetNumberProperty operate on signed 64-bit
39 * integer types.
40 * - SDL_SetFloatProperty and SDL_GetFloatProperty operate on floating point
41 * types.
42 * - SDL_SetBooleanProperty and SDL_GetBooleanProperty operate on boolean
43 * types.
44 *
45 * Properties can be removed from a group by using SDL_ClearProperty.
46 */
47
48
49#ifndef SDL_properties_h_
50#define SDL_properties_h_
51
52#include <SDL3/SDL_stdinc.h>
53#include <SDL3/SDL_error.h>
54
55#include <SDL3/SDL_begin_code.h>
56/* Set up for C function definitions, even when using C++ */
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61/**
62 * SDL properties ID
63 *
64 * \since This datatype is available since SDL 3.2.0.
65 */
67
68/**
69 * SDL property type
70 *
71 * \since This enum is available since SDL 3.2.0.
72 */
82
83/**
84 * Get the global SDL properties.
85 *
86 * \returns a valid property ID on success or 0 on failure; call
87 * SDL_GetError() for more information.
88 *
89 * \since This function is available since SDL 3.2.0.
90 */
91extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetGlobalProperties(void);
92
93/**
94 * Create a group of properties.
95 *
96 * All properties are automatically destroyed when SDL_Quit() is called.
97 *
98 * \returns an ID for a new group of properties, or 0 on failure; call
99 * SDL_GetError() for more information.
100 *
101 * \threadsafety It is safe to call this function from any thread.
102 *
103 * \since This function is available since SDL 3.2.0.
104 *
105 * \sa SDL_DestroyProperties
106 */
107extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_CreateProperties(void);
108
109/**
110 * Copy a group of properties.
111 *
112 * Copy all the properties from one group of properties to another, with the
113 * exception of properties requiring cleanup (set using
114 * SDL_SetPointerPropertyWithCleanup()), which will not be copied. Any
115 * property that already exists on `dst` will be overwritten.
116 *
117 * \param src the properties to copy.
118 * \param dst the destination properties.
119 * \returns true on success or false on failure; call SDL_GetError() for more
120 * information.
121 *
122 * \threadsafety It is safe to call this function from any thread. This
123 * function acquires simultaneous mutex locks on both the source
124 * and destination property sets.
125 *
126 * \since This function is available since SDL 3.2.0.
127 */
128extern SDL_DECLSPEC bool SDLCALL SDL_CopyProperties(SDL_PropertiesID src, SDL_PropertiesID dst);
129
130/**
131 * Lock a group of properties.
132 *
133 * Obtain a multi-threaded lock for these properties. Other threads will wait
134 * while trying to lock these properties until they are unlocked. Properties
135 * must be unlocked before they are destroyed.
136 *
137 * The lock is automatically taken when setting individual properties, this
138 * function is only needed when you want to set several properties atomically
139 * or want to guarantee that properties being queried aren't freed in another
140 * thread.
141 *
142 * \param props the properties to lock.
143 * \returns true on success or false on failure; call SDL_GetError() for more
144 * information.
145 *
146 * \threadsafety It is safe to call this function from any thread.
147 *
148 * \since This function is available since SDL 3.2.0.
149 *
150 * \sa SDL_UnlockProperties
151 */
152extern SDL_DECLSPEC bool SDLCALL SDL_LockProperties(SDL_PropertiesID props);
153
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 */
165extern SDL_DECLSPEC void SDLCALL SDL_UnlockProperties(SDL_PropertiesID props);
166
167/**
168 * A callback used to free resources when a property is deleted.
169 *
170 * This should release any resources associated with `value` that are no
171 * longer needed.
172 *
173 * This callback is set per-property. Different properties in the same group
174 * can have different cleanup callbacks.
175 *
176 * This callback will be called _during_ SDL_SetPointerPropertyWithCleanup if
177 * the function fails for any reason.
178 *
179 * \param userdata an app-defined pointer passed to the callback.
180 * \param value the pointer assigned to the property to clean up.
181 *
182 * \threadsafety This callback may fire without any locks held; if this is a
183 * concern, the app should provide its own locking.
184 *
185 * \since This datatype is available since SDL 3.2.0.
186 *
187 * \sa SDL_SetPointerPropertyWithCleanup
188 */
189typedef void (SDLCALL *SDL_CleanupPropertyCallback)(void *userdata, void *value);
190
191/**
192 * Set a pointer property in a group of properties with a cleanup function
193 * that is called when the property is deleted.
194 *
195 * The cleanup function is also called if setting the property fails for any
196 * reason.
197 *
198 * For simply setting basic data types, like numbers, bools, or strings, use
199 * SDL_SetNumberProperty, SDL_SetBooleanProperty, or SDL_SetStringProperty
200 * instead, as those functions will handle cleanup on your behalf. This
201 * function is only for more complex, custom data.
202 *
203 * \param props the properties to modify.
204 * \param name the name of the property to modify.
205 * \param value the new value of the property, or NULL to delete the property.
206 * \param cleanup the function to call when this property is deleted, or NULL
207 * if no cleanup is necessary.
208 * \param userdata a pointer that is passed to the cleanup function.
209 * \returns true on success or false on failure; call SDL_GetError() for more
210 * information.
211 *
212 * \threadsafety It is safe to call this function from any thread.
213 *
214 * \since This function is available since SDL 3.2.0.
215 *
216 * \sa SDL_GetPointerProperty
217 * \sa SDL_SetPointerProperty
218 * \sa SDL_CleanupPropertyCallback
219 */
220extern SDL_DECLSPEC bool SDLCALL SDL_SetPointerPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *value, SDL_CleanupPropertyCallback cleanup, void *userdata);
221
222/**
223 * Set a pointer property in a group of properties.
224 *
225 * \param props the properties to modify.
226 * \param name the name of the property to modify.
227 * \param value the new value of the property, or NULL to delete the property.
228 * \returns true on success or false on failure; call SDL_GetError() for more
229 * information.
230 *
231 * \threadsafety It is safe to call this function from any thread.
232 *
233 * \since This function is available since SDL 3.2.0.
234 *
235 * \sa SDL_GetPointerProperty
236 * \sa SDL_HasProperty
237 * \sa SDL_SetBooleanProperty
238 * \sa SDL_SetFloatProperty
239 * \sa SDL_SetNumberProperty
240 * \sa SDL_SetPointerPropertyWithCleanup
241 * \sa SDL_SetStringProperty
242 */
243extern SDL_DECLSPEC bool SDLCALL SDL_SetPointerProperty(SDL_PropertiesID props, const char *name, void *value);
244
245/**
246 * Set a string property in a group of properties.
247 *
248 * This function makes a copy of the string; the caller does not have to
249 * preserve the data after this call completes.
250 *
251 * \param props the properties to modify.
252 * \param name the name of the property to modify.
253 * \param value the new value of the property, or NULL to delete the property.
254 * \returns true on success or false on failure; call SDL_GetError() for more
255 * information.
256 *
257 * \threadsafety It is safe to call this function from any thread.
258 *
259 * \since This function is available since SDL 3.2.0.
260 *
261 * \sa SDL_GetStringProperty
262 */
263extern SDL_DECLSPEC bool SDLCALL SDL_SetStringProperty(SDL_PropertiesID props, const char *name, const char *value);
264
265/**
266 * Set an integer property in a group of properties.
267 *
268 * \param props the properties to modify.
269 * \param name the name of the property to modify.
270 * \param value the new value of the property.
271 * \returns true on success or false on failure; call SDL_GetError() for more
272 * information.
273 *
274 * \threadsafety It is safe to call this function from any thread.
275 *
276 * \since This function is available since SDL 3.2.0.
277 *
278 * \sa SDL_GetNumberProperty
279 */
280extern SDL_DECLSPEC bool SDLCALL SDL_SetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 value);
281
282/**
283 * Set a floating point property in a group of properties.
284 *
285 * \param props the properties to modify.
286 * \param name the name of the property to modify.
287 * \param value the new value of the property.
288 * \returns true on success or false on failure; call SDL_GetError() for more
289 * information.
290 *
291 * \threadsafety It is safe to call this function from any thread.
292 *
293 * \since This function is available since SDL 3.2.0.
294 *
295 * \sa SDL_GetFloatProperty
296 */
297extern SDL_DECLSPEC bool SDLCALL SDL_SetFloatProperty(SDL_PropertiesID props, const char *name, float value);
298
299/**
300 * Set a boolean property in a group of properties.
301 *
302 * \param props the properties to modify.
303 * \param name the name of the property to modify.
304 * \param value the new value of the property.
305 * \returns true on success or false on failure; call SDL_GetError() for more
306 * information.
307 *
308 * \threadsafety It is safe to call this function from any thread.
309 *
310 * \since This function is available since SDL 3.2.0.
311 *
312 * \sa SDL_GetBooleanProperty
313 */
314extern SDL_DECLSPEC bool SDLCALL SDL_SetBooleanProperty(SDL_PropertiesID props, const char *name, bool value);
315
316/**
317 * Return whether a property exists in a group of properties.
318 *
319 * \param props the properties to query.
320 * \param name the name of the property to query.
321 * \returns true if the property exists, or false if it doesn't.
322 *
323 * \threadsafety It is safe to call this function from any thread.
324 *
325 * \since This function is available since SDL 3.2.0.
326 *
327 * \sa SDL_GetPropertyType
328 */
329extern SDL_DECLSPEC bool SDLCALL SDL_HasProperty(SDL_PropertiesID props, const char *name);
330
331/**
332 * Get the type of a property in a group of properties.
333 *
334 * \param props the properties to query.
335 * \param name the name of the property to query.
336 * \returns the type of the property, or SDL_PROPERTY_TYPE_INVALID if it is
337 * not set.
338 *
339 * \threadsafety It is safe to call this function from any thread.
340 *
341 * \since This function is available since SDL 3.2.0.
342 *
343 * \sa SDL_HasProperty
344 */
345extern SDL_DECLSPEC SDL_PropertyType SDLCALL SDL_GetPropertyType(SDL_PropertiesID props, const char *name);
346
347/**
348 * Get a pointer property from a group of properties.
349 *
350 * By convention, the names of properties that SDL exposes on objects will
351 * start with "SDL.", and properties that SDL uses internally will start with
352 * "SDL.internal.". These should be considered read-only and should not be
353 * modified by applications.
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 pointer 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_SetPointerProperty() 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_GetBooleanProperty
371 * \sa SDL_GetFloatProperty
372 * \sa SDL_GetNumberProperty
373 * \sa SDL_GetPropertyType
374 * \sa SDL_GetStringProperty
375 * \sa SDL_HasProperty
376 * \sa SDL_SetPointerProperty
377 */
378extern SDL_DECLSPEC void * SDLCALL SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *default_value);
379
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 */
402extern SDL_DECLSPEC const char * SDLCALL SDL_GetStringProperty(SDL_PropertiesID props, const char *name, const char *default_value);
403
404/**
405 * Get a number property from a group of properties.
406 *
407 * You can use SDL_GetPropertyType() to query whether the property exists and
408 * is a number property.
409 *
410 * \param props the properties to query.
411 * \param name the name of the property to query.
412 * \param default_value the default value of the property.
413 * \returns the value of the property, or `default_value` if it is not set or
414 * not a number property.
415 *
416 * \threadsafety It is safe to call this function from any thread.
417 *
418 * \since This function is available since SDL 3.2.0.
419 *
420 * \sa SDL_GetPropertyType
421 * \sa SDL_HasProperty
422 * \sa SDL_SetNumberProperty
423 */
424extern SDL_DECLSPEC Sint64 SDLCALL SDL_GetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 default_value);
425
426/**
427 * Get a floating point property from a group of properties.
428 *
429 * You can use SDL_GetPropertyType() to query whether the property exists and
430 * is a floating point property.
431 *
432 * \param props the properties to query.
433 * \param name the name of the property to query.
434 * \param default_value the default value of the property.
435 * \returns the value of the property, or `default_value` if it is not set or
436 * not a float property.
437 *
438 * \threadsafety It is safe to call this function from any thread.
439 *
440 * \since This function is available since SDL 3.2.0.
441 *
442 * \sa SDL_GetPropertyType
443 * \sa SDL_HasProperty
444 * \sa SDL_SetFloatProperty
445 */
446extern SDL_DECLSPEC float SDLCALL SDL_GetFloatProperty(SDL_PropertiesID props, const char *name, float default_value);
447
448/**
449 * Get a boolean property from a group of properties.
450 *
451 * You can use SDL_GetPropertyType() to query whether the property exists and
452 * is a boolean property.
453 *
454 * \param props the properties to query.
455 * \param name the name of the property to query.
456 * \param default_value the default value of the property.
457 * \returns the value of the property, or `default_value` if it is not set or
458 * not a boolean property.
459 *
460 * \threadsafety It is safe to call this function from any thread.
461 *
462 * \since This function is available since SDL 3.2.0.
463 *
464 * \sa SDL_GetPropertyType
465 * \sa SDL_HasProperty
466 * \sa SDL_SetBooleanProperty
467 */
468extern SDL_DECLSPEC bool SDLCALL SDL_GetBooleanProperty(SDL_PropertiesID props, const char *name, bool default_value);
469
470/**
471 * Clear a property from a group of properties.
472 *
473 * \param props the properties to modify.
474 * \param name the name of the property to clear.
475 * \returns true on success or false on failure; call SDL_GetError() for more
476 * information.
477 *
478 * \threadsafety It is safe to call this function from any thread.
479 *
480 * \since This function is available since SDL 3.2.0.
481 */
482extern SDL_DECLSPEC bool SDLCALL SDL_ClearProperty(SDL_PropertiesID props, const char *name);
483
484/**
485 * A callback used to enumerate all the properties in a group of properties.
486 *
487 * This callback is called from SDL_EnumerateProperties(), and is called once
488 * per property in the set.
489 *
490 * \param userdata an app-defined pointer passed to the callback.
491 * \param props the SDL_PropertiesID that is being enumerated.
492 * \param name the next property name in the enumeration.
493 *
494 * \threadsafety SDL_EnumerateProperties holds a lock on `props` during this
495 * callback.
496 *
497 * \since This datatype is available since SDL 3.2.0.
498 *
499 * \sa SDL_EnumerateProperties
500 */
501typedef void (SDLCALL *SDL_EnumeratePropertiesCallback)(void *userdata, SDL_PropertiesID props, const char *name);
502
503/**
504 * Enumerate the properties contained in a group of properties.
505 *
506 * The callback function is called for each property in the group of
507 * properties. The properties are locked during enumeration.
508 *
509 * \param props the properties to query.
510 * \param callback the function to call for each property.
511 * \param userdata a pointer that is passed to `callback`.
512 * \returns true on success or false on failure; call SDL_GetError() for more
513 * information.
514 *
515 * \threadsafety It is safe to call this function from any thread.
516 *
517 * \since This function is available since SDL 3.2.0.
518 */
519extern SDL_DECLSPEC bool SDLCALL SDL_EnumerateProperties(SDL_PropertiesID props, SDL_EnumeratePropertiesCallback callback, void *userdata);
520
521/**
522 * Destroy a group of properties.
523 *
524 * All properties are deleted and their cleanup functions will be called, if
525 * any.
526 *
527 * \param props the properties to destroy.
528 *
529 * \threadsafety This function should not be called while these properties are
530 * locked or other threads might be setting or getting values
531 * from these properties.
532 *
533 * \since This function is available since SDL 3.2.0.
534 *
535 * \sa SDL_CreateProperties
536 */
537extern SDL_DECLSPEC void SDLCALL SDL_DestroyProperties(SDL_PropertiesID props);
538
539/* Ends C function definitions when using C++ */
540#ifdef __cplusplus
541}
542#endif
543#include <SDL3/SDL_close_code.h>
544
545#endif /* SDL_properties_h_ */
Sint64 SDL_GetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 default_value)
bool SDL_SetPointerProperty(SDL_PropertiesID props, const char *name, void *value)
bool SDL_GetBooleanProperty(SDL_PropertiesID props, const char *name, bool default_value)
float SDL_GetFloatProperty(SDL_PropertiesID props, const char *name, float default_value)
bool SDL_EnumerateProperties(SDL_PropertiesID props, SDL_EnumeratePropertiesCallback callback, void *userdata)
bool SDL_SetBooleanProperty(SDL_PropertiesID props, const char *name, bool value)
bool SDL_CopyProperties(SDL_PropertiesID src, SDL_PropertiesID dst)
Uint32 SDL_PropertiesID
SDL_PropertiesID SDL_GetGlobalProperties(void)
bool SDL_SetPointerPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *value, SDL_CleanupPropertyCallback cleanup, void *userdata)
void(* SDL_EnumeratePropertiesCallback)(void *userdata, SDL_PropertiesID props, const char *name)
const char * SDL_GetStringProperty(SDL_PropertiesID props, const char *name, const char *default_value)
bool SDL_SetFloatProperty(SDL_PropertiesID props, const char *name, float value)
bool SDL_SetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 value)
bool SDL_ClearProperty(SDL_PropertiesID props, const char *name)
SDL_PropertyType SDL_GetPropertyType(SDL_PropertiesID props, const char *name)
bool SDL_HasProperty(SDL_PropertiesID props, const char *name)
SDL_PropertyType
@ SDL_PROPERTY_TYPE_NUMBER
@ SDL_PROPERTY_TYPE_FLOAT
@ SDL_PROPERTY_TYPE_BOOLEAN
@ SDL_PROPERTY_TYPE_INVALID
@ SDL_PROPERTY_TYPE_POINTER
@ SDL_PROPERTY_TYPE_STRING
SDL_PropertiesID SDL_CreateProperties(void)
void SDL_DestroyProperties(SDL_PropertiesID props)
bool SDL_SetStringProperty(SDL_PropertiesID props, const char *name, const char *value)
void SDL_UnlockProperties(SDL_PropertiesID props)
bool SDL_LockProperties(SDL_PropertiesID props)
void * SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *default_value)
void(* SDL_CleanupPropertyCallback)(void *userdata, void *value)
int64_t Sint64
Definition SDL_stdinc.h:472
uint32_t Uint32
Definition SDL_stdinc.h:461