21 #include <SDL2/SDL_timer.h> 46 static std::map<std::size_t, timer>* ptimers =
new std::map<std::size_t, timer>();
82 executing_id_removed =
false;
89 if(executing_id_removed) {
99 DBG_GUI_E <<
"Pushing timer event in queue.";
104 std::scoped_lock lock(timers_mutex);
106 auto itor =
get_timers().find(reinterpret_cast<std::size_t>(
id));
110 result = itor->second.interval;
117 event.user.data1 =
id;
118 event.user.data2 =
nullptr;
120 SDL_PushEvent(&event);
128 const std::function<
void(std::size_t
id)>&
callback,
131 static_assert(
sizeof(std::size_t) ==
sizeof(
void*),
"Pointer and std::size_t are not the same size");
137 std::scoped_lock lock(timers_mutex);
141 }
while(next_timer_id == 0 ||
get_timers().count(next_timer_id) > 0);
143 timer.
sdl_id = SDL_AddTimer(
144 interval,
timer_callback, reinterpret_cast<void*>(next_timer_id));
148 WRN_GUI_E <<
"Failed to create an sdl timer.";
159 std::scoped_lock lock(timers_mutex);
164 DBG_GUI_E <<
"Added timer " << next_timer_id <<
".";
170 DBG_GUI_E <<
"Removing timer " <<
id <<
".";
172 std::scoped_lock lock(timers_mutex);
176 LOG_GUI_E <<
"Can't remove timer since it no longer exists.";
180 if(
id == executing_id) {
181 executing_id_removed =
true;
185 if(!SDL_RemoveTimer(itor->second.sdl_id)) {
195 DBG_GUI_E <<
"The timer is already out of the SDL timer list.";
203 DBG_GUI_E <<
"Executing timer " <<
id <<
".";
205 std::function<void(size_t)>
callback =
nullptr;
207 std::scoped_lock lock(timers_mutex);
211 LOG_GUI_E <<
"Can't execute timer since it no longer exists.";
215 callback = itor->second.callback;
217 if(itor->second.interval == 0) {
Define the common log macros for the gui toolkit.
static bool executing_id_removed
Did somebody try to remove the timer during its execution?
static std::size_t next_timer_id
Ids for the timers.
static uint32_t timer_callback(uint32_t, void *id)
std::function< void(std::size_t id)> callback
static std::size_t executing_id
The id of the event being executed, 0 if none.
std::string id
Text to match against addon_info.tags()
Contains the gui2 timer routines.
std::size_t add_timer(const uint32_t interval, const std::function< void(std::size_t id)> &callback, const bool repeat)
Adds a new timer.
Helper to make removing a timer in a callback safe.
bool execute_timer(const std::size_t id)
Executes a timer.
static std::map< std::size_t, timer > & get_timers()
The active timers.
bool remove_timer(const std::size_t id)
Removes a timer.