Avoid needing to continuously add/remove timers
This commit is contained in:
parent
7624578c21
commit
9f35b9281b
1
glfw/glfw3.h
vendored
1
glfw/glfw3.h
vendored
@ -1662,6 +1662,7 @@ GLFWAPI void glfwRunMainLoop(GLFWtickcallback callback, void *callback_data);
|
|||||||
GLFWAPI void glfwStopMainLoop(void);
|
GLFWAPI void glfwStopMainLoop(void);
|
||||||
GLFWAPI void glfwRequestTickCallback(void);
|
GLFWAPI void glfwRequestTickCallback(void);
|
||||||
GLFWAPI unsigned long long glfwAddTimer(double interval, bool repeats, GLFWuserdatafun callback, void * callback_data, GLFWuserdatafun free_callback);
|
GLFWAPI unsigned long long glfwAddTimer(double interval, bool repeats, GLFWuserdatafun callback, void * callback_data, GLFWuserdatafun free_callback);
|
||||||
|
GLFWAPI void glfwUpdateTimer(unsigned long long timer_id, double interval, bool enabled);
|
||||||
GLFWAPI void glfwRemoveTimer(unsigned long long);
|
GLFWAPI void glfwRemoveTimer(unsigned long long);
|
||||||
|
|
||||||
/*! @brief Terminates the GLFW library.
|
/*! @brief Terminates the GLFW library.
|
||||||
|
|||||||
4
glfw/init.c
vendored
4
glfw/init.c
vendored
@ -344,6 +344,10 @@ GLFWAPI unsigned long long glfwAddTimer(
|
|||||||
return _glfwPlatformAddTimer(interval, repeats, callback, callback_data, free_callback);
|
return _glfwPlatformAddTimer(interval, repeats, callback, callback_data, free_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GLFWAPI void glfwUpdateTimer(unsigned long long timer_id, double interval, bool enabled) {
|
||||||
|
_glfwPlatformUpdateTimer(timer_id, interval, enabled);
|
||||||
|
}
|
||||||
|
|
||||||
GLFWAPI void glfwRemoveTimer(unsigned long long timer_id) {
|
GLFWAPI void glfwRemoveTimer(unsigned long long timer_id) {
|
||||||
_glfwPlatformRemoveTimer(timer_id);
|
_glfwPlatformRemoveTimer(timer_id);
|
||||||
}
|
}
|
||||||
|
|||||||
1
glfw/internal.h
vendored
1
glfw/internal.h
vendored
@ -787,6 +787,7 @@ void _glfwPlatformRunMainLoop(GLFWtickcallback, void*);
|
|||||||
void _glfwPlatformRequestTickCallback();
|
void _glfwPlatformRequestTickCallback();
|
||||||
void _glfwPlatformStopMainLoop(void);
|
void _glfwPlatformStopMainLoop(void);
|
||||||
unsigned long long _glfwPlatformAddTimer(double interval, bool repeats, GLFWuserdatafreefun callback, void *callback_data, GLFWuserdatafreefun free_callback);
|
unsigned long long _glfwPlatformAddTimer(double interval, bool repeats, GLFWuserdatafreefun callback, void *callback_data, GLFWuserdatafreefun free_callback);
|
||||||
|
void _glfwPlatformUpdateTimer(unsigned long long timer_id, double interval, GLFWbool enabled);
|
||||||
void _glfwPlatformRemoveTimer(unsigned long long timer_id);
|
void _glfwPlatformRemoveTimer(unsigned long long timer_id);
|
||||||
|
|
||||||
char* _glfw_strdup(const char* source);
|
char* _glfw_strdup(const char* source);
|
||||||
|
|||||||
5
glfw/main_loop.h
vendored
5
glfw/main_loop.h
vendored
@ -44,3 +44,8 @@ unsigned long long _glfwPlatformAddTimer(double interval, bool repeats, GLFWuser
|
|||||||
void _glfwPlatformRemoveTimer(unsigned long long timer_id) {
|
void _glfwPlatformRemoveTimer(unsigned long long timer_id) {
|
||||||
removeTimer(&_glfw.GLFW_LOOP_BACKEND.eventLoopData, timer_id);
|
removeTimer(&_glfw.GLFW_LOOP_BACKEND.eventLoopData, timer_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _glfwPlatformUpdateTimer(unsigned long long timer_id, double interval, GLFWbool enabled) {
|
||||||
|
changeTimerInterval(&_glfw.GLFW_LOOP_BACKEND.eventLoopData, timer_id, interval);
|
||||||
|
toggleTimer(&_glfw.GLFW_LOOP_BACKEND.eventLoopData, timer_id, enabled);
|
||||||
|
}
|
||||||
|
|||||||
@ -865,7 +865,6 @@ set_cocoa_pending_action(CocoaPendingAction action, const char *wd) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static id_type state_check_timer = 0;
|
|
||||||
static void process_global_state(void *data);
|
static void process_global_state(void *data);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -874,20 +873,13 @@ do_state_check(id_type timer_id UNUSED, void *data) {
|
|||||||
process_global_state(self);
|
process_global_state(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static id_type state_check_timer = 0;
|
||||||
request_global_state_check_in(double seconds, ChildMonitor *self) {
|
|
||||||
if (state_check_timer) remove_main_loop_timer(state_check_timer);
|
|
||||||
state_check_timer = add_main_loop_timer(seconds, false, do_state_check, self, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
process_global_state(void *data) {
|
process_global_state(void *data) {
|
||||||
ChildMonitor *self = data;
|
ChildMonitor *self = data;
|
||||||
if (state_check_timer) {
|
|
||||||
remove_main_loop_timer(state_check_timer);
|
|
||||||
state_check_timer = 0;
|
|
||||||
}
|
|
||||||
maximum_wait = -1;
|
maximum_wait = -1;
|
||||||
|
bool state_check_timer_enabled = false;
|
||||||
|
|
||||||
double now = monotonic();
|
double now = monotonic();
|
||||||
if (global_state.has_pending_resizes) process_pending_resizes(now);
|
if (global_state.has_pending_resizes) process_pending_resizes(now);
|
||||||
@ -918,16 +910,18 @@ process_global_state(void *data) {
|
|||||||
if (has_open_windows) {
|
if (has_open_windows) {
|
||||||
if (maximum_wait >= 0) {
|
if (maximum_wait >= 0) {
|
||||||
if (maximum_wait == 0) request_tick_callback();
|
if (maximum_wait == 0) request_tick_callback();
|
||||||
else request_global_state_check_in(maximum_wait, self);
|
else state_check_timer_enabled = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
stop_main_loop();
|
stop_main_loop();
|
||||||
}
|
}
|
||||||
|
update_main_loop_timer(state_check_timer, MAX(0, maximum_wait), state_check_timer_enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
main_loop(ChildMonitor *self, PyObject *a UNUSED) {
|
main_loop(ChildMonitor *self, PyObject *a UNUSED) {
|
||||||
#define main_loop_doc "The main thread loop"
|
#define main_loop_doc "The main thread loop"
|
||||||
|
state_check_timer = add_main_loop_timer(1000, true, do_state_check, self, NULL);
|
||||||
run_main_loop(process_global_state, self);
|
run_main_loop(process_global_state, self);
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
if (cocoa_pending_actions_wd) { free(cocoa_pending_actions_wd); cocoa_pending_actions_wd = NULL; }
|
if (cocoa_pending_actions_wd) { free(cocoa_pending_actions_wd); cocoa_pending_actions_wd = NULL; }
|
||||||
|
|||||||
3
kitty/glfw-wrapper.c
generated
3
kitty/glfw-wrapper.c
generated
@ -29,6 +29,9 @@ load_glfw(const char* path) {
|
|||||||
*(void **) (&glfwAddTimer_impl) = dlsym(handle, "glfwAddTimer");
|
*(void **) (&glfwAddTimer_impl) = dlsym(handle, "glfwAddTimer");
|
||||||
if (glfwAddTimer_impl == NULL) fail("Failed to load glfw function glfwAddTimer with error: %s", dlerror());
|
if (glfwAddTimer_impl == NULL) fail("Failed to load glfw function glfwAddTimer with error: %s", dlerror());
|
||||||
|
|
||||||
|
*(void **) (&glfwUpdateTimer_impl) = dlsym(handle, "glfwUpdateTimer");
|
||||||
|
if (glfwUpdateTimer_impl == NULL) fail("Failed to load glfw function glfwUpdateTimer with error: %s", dlerror());
|
||||||
|
|
||||||
*(void **) (&glfwRemoveTimer_impl) = dlsym(handle, "glfwRemoveTimer");
|
*(void **) (&glfwRemoveTimer_impl) = dlsym(handle, "glfwRemoveTimer");
|
||||||
if (glfwRemoveTimer_impl == NULL) fail("Failed to load glfw function glfwRemoveTimer with error: %s", dlerror());
|
if (glfwRemoveTimer_impl == NULL) fail("Failed to load glfw function glfwRemoveTimer with error: %s", dlerror());
|
||||||
|
|
||||||
|
|||||||
4
kitty/glfw-wrapper.h
generated
4
kitty/glfw-wrapper.h
generated
@ -1443,6 +1443,10 @@ typedef unsigned long long (*glfwAddTimer_func)(double, bool, GLFWuserdatafun, v
|
|||||||
glfwAddTimer_func glfwAddTimer_impl;
|
glfwAddTimer_func glfwAddTimer_impl;
|
||||||
#define glfwAddTimer glfwAddTimer_impl
|
#define glfwAddTimer glfwAddTimer_impl
|
||||||
|
|
||||||
|
typedef void (*glfwUpdateTimer_func)(unsigned long long, double, bool);
|
||||||
|
glfwUpdateTimer_func glfwUpdateTimer_impl;
|
||||||
|
#define glfwUpdateTimer glfwUpdateTimer_impl
|
||||||
|
|
||||||
typedef void (*glfwRemoveTimer_func)(unsigned long);
|
typedef void (*glfwRemoveTimer_func)(unsigned long);
|
||||||
glfwRemoveTimer_func glfwRemoveTimer_impl;
|
glfwRemoveTimer_func glfwRemoveTimer_impl;
|
||||||
#define glfwRemoveTimer glfwRemoveTimer_impl
|
#define glfwRemoveTimer glfwRemoveTimer_impl
|
||||||
|
|||||||
@ -1130,6 +1130,11 @@ add_main_loop_timer(double interval, bool repeats, timer_callback_fun callback,
|
|||||||
return glfwAddTimer(interval, repeats, callback, callback_data, free_callback);
|
return glfwAddTimer(interval, repeats, callback, callback_data, free_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
update_main_loop_timer(id_type timer_id, double interval, bool enabled) {
|
||||||
|
glfwUpdateTimer(timer_id, interval, enabled);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
remove_main_loop_timer(id_type timer_id) {
|
remove_main_loop_timer(id_type timer_id) {
|
||||||
glfwRemoveTimer(timer_id);
|
glfwRemoveTimer(timer_id);
|
||||||
|
|||||||
@ -223,6 +223,7 @@ typedef void (* timer_callback_fun)(id_type, void*);
|
|||||||
typedef void (* tick_callback_fun)(void*);
|
typedef void (* tick_callback_fun)(void*);
|
||||||
id_type add_main_loop_timer(double interval, bool repeats, timer_callback_fun callback, void *callback_data, timer_callback_fun free_callback);
|
id_type add_main_loop_timer(double interval, bool repeats, timer_callback_fun callback, void *callback_data, timer_callback_fun free_callback);
|
||||||
void remove_main_loop_timer(id_type timer_id);
|
void remove_main_loop_timer(id_type timer_id);
|
||||||
|
void update_main_loop_timer(id_type timer_id, double interval, bool enabled);
|
||||||
void run_main_loop(tick_callback_fun, void*);
|
void run_main_loop(tick_callback_fun, void*);
|
||||||
void request_tick_callback(void);
|
void request_tick_callback(void);
|
||||||
void stop_main_loop(void);
|
void stop_main_loop(void);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user