sdl3_sys/generated/timer.rs
1//! SDL provides time management functionality. It is useful for dealing with
2//! (usually) small durations of time.
3//!
4//! This is not to be confused with _calendar time_ management, which is
5//! provided by [CategoryTime](CategoryTime).
6//!
7//! This category covers measuring time elapsed ([`SDL_GetTicks()`],
8//! [`SDL_GetPerformanceCounter()`]), putting a thread to sleep for a certain
9//! amount of time ([`SDL_Delay()`], [`SDL_DelayNS()`], [`SDL_DelayPrecise()`]), and firing
10//! a callback function after a certain amount of time has elapsed
11//! ([`SDL_AddTimer()`], etc).
12//!
13//! There are also useful macros to convert between time units, like
14//! [`SDL_SECONDS_TO_NS()`] and such.
15
16use super::stdinc::*;
17
18use super::error::*;
19
20/// Number of milliseconds in a second.
21///
22/// This is always 1000.
23///
24/// ## Availability
25/// This macro is available since SDL 3.2.0.
26pub const SDL_MS_PER_SECOND: ::core::primitive::i32 = 1000;
27
28/// Number of microseconds in a second.
29///
30/// This is always 1000000.
31///
32/// ## Availability
33/// This macro is available since SDL 3.2.0.
34pub const SDL_US_PER_SECOND: ::core::primitive::i32 = 1000000;
35
36/// Number of nanoseconds in a second.
37///
38/// This is always 1000000000.
39///
40/// ## Availability
41/// This macro is available since SDL 3.2.0.
42pub const SDL_NS_PER_SECOND: ::core::primitive::i64 = 1000000000_i64;
43
44/// Number of nanoseconds in a millisecond.
45///
46/// This is always 1000000.
47///
48/// ## Availability
49/// This macro is available since SDL 3.2.0.
50pub const SDL_NS_PER_MS: ::core::primitive::i32 = 1000000;
51
52/// Number of nanoseconds in a microsecond.
53///
54/// This is always 1000.
55///
56/// ## Availability
57/// This macro is available since SDL 3.2.0.
58pub const SDL_NS_PER_US: ::core::primitive::i32 = 1000;
59
60/// Convert seconds to nanoseconds.
61///
62/// This only converts whole numbers, not fractional seconds.
63///
64/// ## Parameters
65/// - `S`: the number of seconds to convert.
66///
67/// ## Return value
68/// Returns S, expressed in nanoseconds.
69///
70/// ## Thread safety
71/// It is safe to call this macro from any thread.
72///
73/// ## Availability
74/// This macro is available since SDL 3.2.0.
75#[inline(always)]
76pub const fn SDL_SECONDS_TO_NS(S: Uint64) -> Uint64 {
77 (S * (SDL_NS_PER_SECOND as Uint64))
78}
79
80/// Convert nanoseconds to seconds.
81///
82/// This performs a division, so the results can be dramatically different if
83/// `NS` is an integer or floating point value.
84///
85/// ## Parameters
86/// - `NS`: the number of nanoseconds to convert.
87///
88/// ## Return value
89/// Returns NS, expressed in seconds.
90///
91/// ## Thread safety
92/// It is safe to call this macro from any thread.
93///
94/// ## Availability
95/// This macro is available since SDL 3.2.0.
96#[inline(always)]
97pub const fn SDL_NS_TO_SECONDS(NS: Uint64) -> Uint64 {
98 (NS / (SDL_NS_PER_SECOND as Uint64))
99}
100
101/// Convert milliseconds to nanoseconds.
102///
103/// This only converts whole numbers, not fractional milliseconds.
104///
105/// ## Parameters
106/// - `MS`: the number of milliseconds to convert.
107///
108/// ## Return value
109/// Returns MS, expressed in nanoseconds.
110///
111/// ## Thread safety
112/// It is safe to call this macro from any thread.
113///
114/// ## Availability
115/// This macro is available since SDL 3.2.0.
116#[inline(always)]
117pub const fn SDL_MS_TO_NS(MS: Uint64) -> Uint64 {
118 (MS * (SDL_NS_PER_MS as Uint64))
119}
120
121/// Convert nanoseconds to milliseconds.
122///
123/// This performs a division, so the results can be dramatically different if
124/// `NS` is an integer or floating point value.
125///
126/// ## Parameters
127/// - `NS`: the number of nanoseconds to convert.
128///
129/// ## Return value
130/// Returns NS, expressed in milliseconds.
131///
132/// ## Thread safety
133/// It is safe to call this macro from any thread.
134///
135/// ## Availability
136/// This macro is available since SDL 3.2.0.
137#[inline(always)]
138pub const fn SDL_NS_TO_MS(NS: Uint64) -> Uint64 {
139 (NS / (SDL_NS_PER_MS as Uint64))
140}
141
142/// Convert microseconds to nanoseconds.
143///
144/// This only converts whole numbers, not fractional microseconds.
145///
146/// ## Parameters
147/// - `US`: the number of microseconds to convert.
148///
149/// ## Return value
150/// Returns US, expressed in nanoseconds.
151///
152/// ## Thread safety
153/// It is safe to call this macro from any thread.
154///
155/// ## Availability
156/// This macro is available since SDL 3.2.0.
157#[inline(always)]
158pub const fn SDL_US_TO_NS(US: Uint64) -> Uint64 {
159 (US * (SDL_NS_PER_US as Uint64))
160}
161
162/// Convert nanoseconds to microseconds.
163///
164/// This performs a division, so the results can be dramatically different if
165/// `NS` is an integer or floating point value.
166///
167/// ## Parameters
168/// - `NS`: the number of nanoseconds to convert.
169///
170/// ## Return value
171/// Returns NS, expressed in microseconds.
172///
173/// ## Thread safety
174/// It is safe to call this macro from any thread.
175///
176/// ## Availability
177/// This macro is available since SDL 3.2.0.
178#[inline(always)]
179pub const fn SDL_NS_TO_US(NS: Uint64) -> Uint64 {
180 (NS / (SDL_NS_PER_US as Uint64))
181}
182
183unsafe extern "C" {
184 /// Get the number of milliseconds that have elapsed since the SDL library
185 /// initialization.
186 ///
187 /// ## Return value
188 /// Returns an unsigned 64‑bit integer that represents the number of
189 /// milliseconds that have elapsed since the SDL library was
190 /// initialized (typically via a call to [`SDL_Init`]).
191 ///
192 /// ## Thread safety
193 /// It is safe to call this function from any thread.
194 ///
195 /// ## Availability
196 /// This function is available since SDL 3.2.0.
197 ///
198 /// ## See also
199 /// - [`SDL_GetTicksNS`]
200 pub fn SDL_GetTicks() -> Uint64;
201}
202
203unsafe extern "C" {
204 /// Get the number of nanoseconds since SDL library initialization.
205 ///
206 /// ## Return value
207 /// Returns an unsigned 64-bit value representing the number of nanoseconds
208 /// since the SDL library initialized.
209 ///
210 /// ## Thread safety
211 /// It is safe to call this function from any thread.
212 ///
213 /// ## Availability
214 /// This function is available since SDL 3.2.0.
215 pub fn SDL_GetTicksNS() -> Uint64;
216}
217
218unsafe extern "C" {
219 /// Get the current value of the high resolution counter.
220 ///
221 /// This function is typically used for profiling.
222 ///
223 /// The counter values are only meaningful relative to each other. Differences
224 /// between values can be converted to times by using
225 /// [`SDL_GetPerformanceFrequency()`].
226 ///
227 /// ## Return value
228 /// Returns the current counter value.
229 ///
230 /// ## Thread safety
231 /// It is safe to call this function from any thread.
232 ///
233 /// ## Availability
234 /// This function is available since SDL 3.2.0.
235 ///
236 /// ## See also
237 /// - [`SDL_GetPerformanceFrequency`]
238 pub fn SDL_GetPerformanceCounter() -> Uint64;
239}
240
241unsafe extern "C" {
242 /// Get the count per second of the high resolution counter.
243 ///
244 /// ## Return value
245 /// Returns a platform-specific count per second.
246 ///
247 /// ## Thread safety
248 /// It is safe to call this function from any thread.
249 ///
250 /// ## Availability
251 /// This function is available since SDL 3.2.0.
252 ///
253 /// ## See also
254 /// - [`SDL_GetPerformanceCounter`]
255 pub fn SDL_GetPerformanceFrequency() -> Uint64;
256}
257
258unsafe extern "C" {
259 /// Wait a specified number of milliseconds before returning.
260 ///
261 /// This function waits a specified number of milliseconds before returning. It
262 /// waits at least the specified time, but possibly longer due to OS
263 /// scheduling.
264 ///
265 /// ## Parameters
266 /// - `ms`: the number of milliseconds to delay.
267 ///
268 /// ## Thread safety
269 /// It is safe to call this function from any thread.
270 ///
271 /// ## Availability
272 /// This function is available since SDL 3.2.0.
273 ///
274 /// ## See also
275 /// - [`SDL_DelayNS`]
276 /// - [`SDL_DelayPrecise`]
277 pub fn SDL_Delay(ms: Uint32);
278}
279
280unsafe extern "C" {
281 /// Wait a specified number of nanoseconds before returning.
282 ///
283 /// This function waits a specified number of nanoseconds before returning. It
284 /// waits at least the specified time, but possibly longer due to OS
285 /// scheduling.
286 ///
287 /// ## Parameters
288 /// - `ns`: the number of nanoseconds to delay.
289 ///
290 /// ## Thread safety
291 /// It is safe to call this function from any thread.
292 ///
293 /// ## Availability
294 /// This function is available since SDL 3.2.0.
295 ///
296 /// ## See also
297 /// - [`SDL_Delay`]
298 /// - [`SDL_DelayPrecise`]
299 pub fn SDL_DelayNS(ns: Uint64);
300}
301
302unsafe extern "C" {
303 /// Wait a specified number of nanoseconds before returning.
304 ///
305 /// This function waits a specified number of nanoseconds before returning. It
306 /// will attempt to wait as close to the requested time as possible, busy
307 /// waiting if necessary, but could return later due to OS scheduling.
308 ///
309 /// ## Parameters
310 /// - `ns`: the number of nanoseconds to delay.
311 ///
312 /// ## Thread safety
313 /// It is safe to call this function from any thread.
314 ///
315 /// ## Availability
316 /// This function is available since SDL 3.2.0.
317 ///
318 /// ## See also
319 /// - [`SDL_Delay`]
320 /// - [`SDL_DelayNS`]
321 pub fn SDL_DelayPrecise(ns: Uint64);
322}
323
324/// Definition of the timer ID type.
325///
326/// ## Availability
327/// This datatype is available since SDL 3.2.0.
328#[repr(transparent)]
329#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
330#[cfg_attr(feature = "debug-impls", derive(Debug))]
331pub struct SDL_TimerID(pub Uint32);
332
333impl ::core::cmp::PartialEq<Uint32> for SDL_TimerID {
334 #[inline(always)]
335 fn eq(&self, other: &Uint32) -> bool {
336 &self.0 == other
337 }
338}
339
340impl ::core::cmp::PartialEq<SDL_TimerID> for Uint32 {
341 #[inline(always)]
342 fn eq(&self, other: &SDL_TimerID) -> bool {
343 self == &other.0
344 }
345}
346
347impl From<SDL_TimerID> for Uint32 {
348 #[inline(always)]
349 fn from(value: SDL_TimerID) -> Self {
350 value.0
351 }
352}
353
354#[cfg(feature = "metadata")]
355impl sdl3_sys::metadata::GroupMetadata for SDL_TimerID {
356 const GROUP_METADATA: &'static sdl3_sys::metadata::Group =
357 &crate::metadata::timer::METADATA_SDL_TimerID;
358}
359
360/// Function prototype for the millisecond timer callback function.
361///
362/// The callback function is passed the current timer interval and returns the
363/// next timer interval, in milliseconds. If the returned value is the same as
364/// the one passed in, the periodic alarm continues, otherwise a new alarm is
365/// scheduled. If the callback returns 0, the periodic alarm is canceled and
366/// will be removed.
367///
368/// ## Parameters
369/// - `userdata`: an arbitrary pointer provided by the app through
370/// [`SDL_AddTimer`], for its own use.
371/// - `timerID`: the current timer being processed.
372/// - `interval`: the current callback time interval.
373///
374/// ## Return value
375/// Returns the new callback time interval, or 0 to disable further runs of
376/// the callback.
377///
378/// ## Thread safety
379/// SDL may call this callback at any time from a background
380/// thread; the application is responsible for locking resources
381/// the callback touches that need to be protected.
382///
383/// ## Availability
384/// This datatype is available since SDL 3.2.0.
385///
386/// ## See also
387/// - [`SDL_AddTimer`]
388pub type SDL_TimerCallback = ::core::option::Option<
389 unsafe extern "C" fn(
390 userdata: *mut ::core::ffi::c_void,
391 timerID: SDL_TimerID,
392 interval: Uint32,
393 ) -> Uint32,
394>;
395
396unsafe extern "C" {
397 /// Call a callback function at a future time.
398 ///
399 /// The callback function is passed the current timer interval and the user
400 /// supplied parameter from the [`SDL_AddTimer()`] call and should return the next
401 /// timer interval. If the value returned from the callback is 0, the timer is
402 /// canceled and will be removed.
403 ///
404 /// The callback is run on a separate thread, and for short timeouts can
405 /// potentially be called before this function returns.
406 ///
407 /// Timers take into account the amount of time it took to execute the
408 /// callback. For example, if the callback took 250 ms to execute and returned
409 /// 1000 (ms), the timer would only wait another 750 ms before its next
410 /// iteration.
411 ///
412 /// Timing may be inexact due to OS scheduling. Be sure to note the current
413 /// time with [`SDL_GetTicksNS()`] or [`SDL_GetPerformanceCounter()`] in case your
414 /// callback needs to adjust for variances.
415 ///
416 /// ## Parameters
417 /// - `interval`: the timer delay, in milliseconds, passed to `callback`.
418 /// - `callback`: the [`SDL_TimerCallback`] function to call when the specified
419 /// `interval` elapses.
420 /// - `userdata`: a pointer that is passed to `callback`.
421 ///
422 /// ## Return value
423 /// Returns a timer ID or 0 on failure; call [`SDL_GetError()`] for more
424 /// information.
425 ///
426 /// ## Thread safety
427 /// It is safe to call this function from any thread.
428 ///
429 /// ## Availability
430 /// This function is available since SDL 3.2.0.
431 ///
432 /// ## See also
433 /// - [`SDL_AddTimerNS`]
434 /// - [`SDL_RemoveTimer`]
435 pub fn SDL_AddTimer(
436 interval: Uint32,
437 callback: SDL_TimerCallback,
438 userdata: *mut ::core::ffi::c_void,
439 ) -> SDL_TimerID;
440}
441
442/// Function prototype for the nanosecond timer callback function.
443///
444/// The callback function is passed the current timer interval and returns the
445/// next timer interval, in nanoseconds. If the returned value is the same as
446/// the one passed in, the periodic alarm continues, otherwise a new alarm is
447/// scheduled. If the callback returns 0, the periodic alarm is canceled and
448/// will be removed.
449///
450/// ## Parameters
451/// - `userdata`: an arbitrary pointer provided by the app through
452/// [`SDL_AddTimer`], for its own use.
453/// - `timerID`: the current timer being processed.
454/// - `interval`: the current callback time interval.
455///
456/// ## Return value
457/// Returns the new callback time interval, or 0 to disable further runs of
458/// the callback.
459///
460/// ## Thread safety
461/// SDL may call this callback at any time from a background
462/// thread; the application is responsible for locking resources
463/// the callback touches that need to be protected.
464///
465/// ## Availability
466/// This datatype is available since SDL 3.2.0.
467///
468/// ## See also
469/// - [`SDL_AddTimerNS`]
470pub type SDL_NSTimerCallback = ::core::option::Option<
471 unsafe extern "C" fn(
472 userdata: *mut ::core::ffi::c_void,
473 timerID: SDL_TimerID,
474 interval: Uint64,
475 ) -> Uint64,
476>;
477
478unsafe extern "C" {
479 /// Call a callback function at a future time.
480 ///
481 /// The callback function is passed the current timer interval and the user
482 /// supplied parameter from the [`SDL_AddTimerNS()`] call and should return the
483 /// next timer interval. If the value returned from the callback is 0, the
484 /// timer is canceled and will be removed.
485 ///
486 /// The callback is run on a separate thread, and for short timeouts can
487 /// potentially be called before this function returns.
488 ///
489 /// Timers take into account the amount of time it took to execute the
490 /// callback. For example, if the callback took 250 ns to execute and returned
491 /// 1000 (ns), the timer would only wait another 750 ns before its next
492 /// iteration.
493 ///
494 /// Timing may be inexact due to OS scheduling. Be sure to note the current
495 /// time with [`SDL_GetTicksNS()`] or [`SDL_GetPerformanceCounter()`] in case your
496 /// callback needs to adjust for variances.
497 ///
498 /// ## Parameters
499 /// - `interval`: the timer delay, in nanoseconds, passed to `callback`.
500 /// - `callback`: the [`SDL_TimerCallback`] function to call when the specified
501 /// `interval` elapses.
502 /// - `userdata`: a pointer that is passed to `callback`.
503 ///
504 /// ## Return value
505 /// Returns a timer ID or 0 on failure; call [`SDL_GetError()`] for more
506 /// information.
507 ///
508 /// ## Thread safety
509 /// It is safe to call this function from any thread.
510 ///
511 /// ## Availability
512 /// This function is available since SDL 3.2.0.
513 ///
514 /// ## See also
515 /// - [`SDL_AddTimer`]
516 /// - [`SDL_RemoveTimer`]
517 pub fn SDL_AddTimerNS(
518 interval: Uint64,
519 callback: SDL_NSTimerCallback,
520 userdata: *mut ::core::ffi::c_void,
521 ) -> SDL_TimerID;
522}
523
524unsafe extern "C" {
525 /// Remove a timer created with [`SDL_AddTimer()`].
526 ///
527 /// ## Parameters
528 /// - `id`: the ID of the timer to remove.
529 ///
530 /// ## Return value
531 /// Returns true on success or false on failure; call [`SDL_GetError()`] for more
532 /// information.
533 ///
534 /// ## Thread safety
535 /// It is safe to call this function from any thread.
536 ///
537 /// ## Availability
538 /// This function is available since SDL 3.2.0.
539 ///
540 /// ## See also
541 /// - [`SDL_AddTimer`]
542 pub fn SDL_RemoveTimer(id: SDL_TimerID) -> ::core::primitive::bool;
543}
544
545#[cfg(doc)]
546use crate::everything::*;