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 = "display-impls")]
355impl ::core::fmt::Display for SDL_TimerID {
356 #[inline(always)]
357 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
358 <Uint32 as ::core::fmt::Display>::fmt(&self.0, f)
359 }
360}
361
362impl SDL_TimerID {
363 /// Initialize a `SDL_TimerID` from a raw value.
364 #[inline(always)]
365 pub const fn new(value: Uint32) -> Self {
366 Self(value)
367 }
368}
369
370impl SDL_TimerID {
371 /// Get a copy of the inner raw value.
372 #[inline(always)]
373 pub const fn value(&self) -> Uint32 {
374 self.0
375 }
376}
377
378#[cfg(feature = "metadata")]
379impl sdl3_sys::metadata::GroupMetadata for SDL_TimerID {
380 const GROUP_METADATA: &'static sdl3_sys::metadata::Group =
381 &crate::metadata::timer::METADATA_SDL_TimerID;
382}
383
384/// Function prototype for the millisecond timer callback function.
385///
386/// The callback function is passed the current timer interval and returns the
387/// next timer interval, in milliseconds. If the returned value is the same as
388/// the one passed in, the periodic alarm continues, otherwise a new alarm is
389/// scheduled. If the callback returns 0, the periodic alarm is canceled and
390/// will be removed.
391///
392/// ## Parameters
393/// - `userdata`: an arbitrary pointer provided by the app through
394/// [`SDL_AddTimer`], for its own use.
395/// - `timerID`: the current timer being processed.
396/// - `interval`: the current callback time interval.
397///
398/// ## Return value
399/// Returns the new callback time interval, or 0 to disable further runs of
400/// the callback.
401///
402/// ## Thread safety
403/// SDL may call this callback at any time from a background
404/// thread; the application is responsible for locking resources
405/// the callback touches that need to be protected.
406///
407/// ## Availability
408/// This datatype is available since SDL 3.2.0.
409///
410/// ## See also
411/// - [`SDL_AddTimer`]
412pub type SDL_TimerCallback = ::core::option::Option<
413 unsafe extern "C" fn(
414 userdata: *mut ::core::ffi::c_void,
415 timerID: SDL_TimerID,
416 interval: Uint32,
417 ) -> Uint32,
418>;
419
420unsafe extern "C" {
421 /// Call a callback function at a future time.
422 ///
423 /// The callback function is passed the current timer interval and the user
424 /// supplied parameter from the [`SDL_AddTimer()`] call and should return the next
425 /// timer interval. If the value returned from the callback is 0, the timer is
426 /// canceled and will be removed.
427 ///
428 /// The callback is run on a separate thread, and for short timeouts can
429 /// potentially be called before this function returns.
430 ///
431 /// Timers take into account the amount of time it took to execute the
432 /// callback. For example, if the callback took 250 ms to execute and returned
433 /// 1000 (ms), the timer would only wait another 750 ms before its next
434 /// iteration.
435 ///
436 /// Timing may be inexact due to OS scheduling. Be sure to note the current
437 /// time with [`SDL_GetTicksNS()`] or [`SDL_GetPerformanceCounter()`] in case your
438 /// callback needs to adjust for variances.
439 ///
440 /// ## Parameters
441 /// - `interval`: the timer delay, in milliseconds, passed to `callback`.
442 /// - `callback`: the [`SDL_TimerCallback`] function to call when the specified
443 /// `interval` elapses.
444 /// - `userdata`: a pointer that is passed to `callback`.
445 ///
446 /// ## Return value
447 /// Returns a timer ID or 0 on failure; call [`SDL_GetError()`] for more
448 /// information.
449 ///
450 /// ## Thread safety
451 /// It is safe to call this function from any thread.
452 ///
453 /// ## Availability
454 /// This function is available since SDL 3.2.0.
455 ///
456 /// ## See also
457 /// - [`SDL_AddTimerNS`]
458 /// - [`SDL_RemoveTimer`]
459 pub fn SDL_AddTimer(
460 interval: Uint32,
461 callback: SDL_TimerCallback,
462 userdata: *mut ::core::ffi::c_void,
463 ) -> SDL_TimerID;
464}
465
466/// Function prototype for the nanosecond timer callback function.
467///
468/// The callback function is passed the current timer interval and returns the
469/// next timer interval, in nanoseconds. If the returned value is the same as
470/// the one passed in, the periodic alarm continues, otherwise a new alarm is
471/// scheduled. If the callback returns 0, the periodic alarm is canceled and
472/// will be removed.
473///
474/// ## Parameters
475/// - `userdata`: an arbitrary pointer provided by the app through
476/// [`SDL_AddTimer`], for its own use.
477/// - `timerID`: the current timer being processed.
478/// - `interval`: the current callback time interval.
479///
480/// ## Return value
481/// Returns the new callback time interval, or 0 to disable further runs of
482/// the callback.
483///
484/// ## Thread safety
485/// SDL may call this callback at any time from a background
486/// thread; the application is responsible for locking resources
487/// the callback touches that need to be protected.
488///
489/// ## Availability
490/// This datatype is available since SDL 3.2.0.
491///
492/// ## See also
493/// - [`SDL_AddTimerNS`]
494pub type SDL_NSTimerCallback = ::core::option::Option<
495 unsafe extern "C" fn(
496 userdata: *mut ::core::ffi::c_void,
497 timerID: SDL_TimerID,
498 interval: Uint64,
499 ) -> Uint64,
500>;
501
502unsafe extern "C" {
503 /// Call a callback function at a future time.
504 ///
505 /// The callback function is passed the current timer interval and the user
506 /// supplied parameter from the [`SDL_AddTimerNS()`] call and should return the
507 /// next timer interval. If the value returned from the callback is 0, the
508 /// timer is canceled and will be removed.
509 ///
510 /// The callback is run on a separate thread, and for short timeouts can
511 /// potentially be called before this function returns.
512 ///
513 /// Timers take into account the amount of time it took to execute the
514 /// callback. For example, if the callback took 250 ns to execute and returned
515 /// 1000 (ns), the timer would only wait another 750 ns before its next
516 /// iteration.
517 ///
518 /// Timing may be inexact due to OS scheduling. Be sure to note the current
519 /// time with [`SDL_GetTicksNS()`] or [`SDL_GetPerformanceCounter()`] in case your
520 /// callback needs to adjust for variances.
521 ///
522 /// ## Parameters
523 /// - `interval`: the timer delay, in nanoseconds, passed to `callback`.
524 /// - `callback`: the [`SDL_TimerCallback`] function to call when the specified
525 /// `interval` elapses.
526 /// - `userdata`: a pointer that is passed to `callback`.
527 ///
528 /// ## Return value
529 /// Returns a timer ID or 0 on failure; call [`SDL_GetError()`] for more
530 /// information.
531 ///
532 /// ## Thread safety
533 /// It is safe to call this function from any thread.
534 ///
535 /// ## Availability
536 /// This function is available since SDL 3.2.0.
537 ///
538 /// ## See also
539 /// - [`SDL_AddTimer`]
540 /// - [`SDL_RemoveTimer`]
541 pub fn SDL_AddTimerNS(
542 interval: Uint64,
543 callback: SDL_NSTimerCallback,
544 userdata: *mut ::core::ffi::c_void,
545 ) -> SDL_TimerID;
546}
547
548unsafe extern "C" {
549 /// Remove a timer created with [`SDL_AddTimer()`].
550 ///
551 /// ## Parameters
552 /// - `id`: the ID of the timer to remove.
553 ///
554 /// ## Return value
555 /// Returns true on success or false on failure; call [`SDL_GetError()`] for more
556 /// information.
557 ///
558 /// ## Thread safety
559 /// It is safe to call this function from any thread.
560 ///
561 /// ## Availability
562 /// This function is available since SDL 3.2.0.
563 ///
564 /// ## See also
565 /// - [`SDL_AddTimer`]
566 pub fn SDL_RemoveTimer(id: SDL_TimerID) -> ::core::primitive::bool;
567}
568
569#[cfg(doc)]
570use crate::everything::*;