Files
2020-04-03 12:10:53 -07:00

63 lines
2.7 KiB
C

/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.
*/
#pragma once
typedef struct _TimerManager TimerManager;
// Prototype for callback function.
typedef void (*TimerEventHandler)(unsigned id, void *user_data);
// Params:
// handler_func - Pointer to a function that will be called when the timer fires.
// user_data - A pointer to user data. This library does not care what it is. Can be NULL. It will be passed to the timer handler function.
//
// Returns NULL on failure.
TimerManager *timer_manager_create(TimerEventHandler handler_func, void *user_data);
void timer_manager_delete(TimerManager *tman);
// Iterates through all the running timers to see if any have elapsed. Calls the event handler for any that have.
void timer_manager_do_tick(TimerManager *tman);
// Returns a c string that represents the current state of all active timers (ie
// ones with an end time in the future). You can pass this into
// timer_manager_load_state() if you want to resume the current session after
// restarting your application for some reason.
//
// You are responsible for free'ing the returned string.
//
// Always returns a valid string.
char *timer_manager_save_state(TimerManager *tman);
// Parses the specified state string (that was previously generated by a call to
// timer_manager_save_state) and updates existing timers to have the end time
// specified in the state string. If a timer is not present in the TimerManager
// but is in the state string, then an error is signaled. If a timer is present in
// the TimerManager but is not in the state string, then it is unaltered and no
// error occurs.
//
// Returns -1 on failure, 0 on success.
int timer_manager_load_state(TimerManager *tman, const char *state_string);
// id - This number will be passed to the timeout handler when the timer fires. Must be unique - no other timer can have the same id.
// user_state - A pointer to be passed to the timeout handler function when it is called.
// Returns -1 on failure, 0 on success.
int timer_manager_create_timer(TimerManager *tman, unsigned id, unsigned duration_in_seconds);
void timer_manager_delete_timer(TimerManager *tman, unsigned id);
// Returns -1 on failure, 0 on success. If called on a timer that is already running, the elapse time will be set to now + duration.
int timer_manager_set_duration(TimerManager *tman, unsigned id, unsigned duration_in_seconds);
// Starts a timer, or restarts it if it is already running. Returns -1 on failure, 0 on success.
int timer_manager_start_timer(TimerManager *tman, unsigned id);
// Returns -1 on failure, 0 on success.
int timer_manager_stop_timer(TimerManager *tman, unsigned id);