diff --git a/glfw/main_loop.h b/glfw/main_loop.h new file mode 100644 index 000000000..1353abdbb --- /dev/null +++ b/glfw/main_loop.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2019 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#pragma once + +#include "internal.h" + +#ifndef GLFW_LOOP_BACKEND +#define GLFW_LOOP_BACKEND x11 +#endif + +static GLFWbool keep_going = GLFW_FALSE, tick_callback_requested = GLFW_FALSE; + +void _glfwPlatformRequestTickCallback() { + tick_callback_requested = GLFW_TRUE; +} + +void _glfwPlatformStopMainLoop(void) { + if (keep_going) { + keep_going = GLFW_FALSE; + _glfwPlatformPostEmptyEvent(); + } +} + +void _glfwPlatformRunMainLoop(GLFWtickcallback callback, void* data) { + keep_going = GLFW_TRUE; + tick_callback_requested = GLFW_FALSE; + while(keep_going) { + while (tick_callback_requested) { + tick_callback_requested = GLFW_FALSE; + callback(data); + } + _glfwPlatformWaitEvents(); + } +} + +unsigned long long _glfwPlatformAddTimer(double interval, bool repeats, GLFWuserdatafreefun callback, void *callback_data, GLFWuserdatafreefun free_callback) { + return addTimer(&_glfw.GLFW_LOOP_BACKEND.eventLoopData, "user timer", interval, 1, repeats, callback, callback_data, free_callback); +} + +void _glfwPlatformRemoveTimer(unsigned long long timer_id) { + removeTimer(&_glfw.GLFW_LOOP_BACKEND.eventLoopData, timer_id); +} diff --git a/glfw/source-info.json b/glfw/source-info.json index 1da7e1b69..497bb2229 100644 --- a/glfw/source-info.json +++ b/glfw/source-info.json @@ -65,7 +65,8 @@ "osmesa_context.h", "linux_joystick.h", "null_joystick.h", - "linux_notify.h" + "linux_notify.h", + "main_loop.h" ], "protocols": [ "stable/xdg-shell/xdg-shell.xml", @@ -131,7 +132,8 @@ "osmesa_context.h", "linux_joystick.h", "null_joystick.h", - "linux_notify.h" + "linux_notify.h", + "main_loop.h" ], "sources": [ "x11_init.c", diff --git a/glfw/wl_init.c b/glfw/wl_init.c index 1e19f10bd..cf412927c 100644 --- a/glfw/wl_init.c +++ b/glfw/wl_init.c @@ -854,3 +854,6 @@ const char* _glfwPlatformGetVersionString(void) #endif ; } + +#define GLFW_LOOP_BACKEND wl +#include "main_loop.h" diff --git a/glfw/x11_init.c b/glfw/x11_init.c index 6bf91280c..87ba62787 100644 --- a/glfw/x11_init.c +++ b/glfw/x11_init.c @@ -770,35 +770,4 @@ const char* _glfwPlatformGetVersionString(void) ; } -static GLFWbool keep_going = GLFW_FALSE, tick_callback_requested = GLFW_FALSE; - -void _glfwPlatformRequestTickCallback() { - tick_callback_requested = GLFW_TRUE; -} - -void _glfwPlatformStopMainLoop(void) { - if (keep_going) { - keep_going = GLFW_FALSE; - _glfwPlatformPostEmptyEvent(); - } -} - -void _glfwPlatformRunMainLoop(GLFWtickcallback callback, void* data) { - keep_going = GLFW_TRUE; - tick_callback_requested = GLFW_FALSE; - while(keep_going) { - while (tick_callback_requested) { - tick_callback_requested = GLFW_FALSE; - callback(data); - } - _glfwPlatformWaitEvents(); - } -} - -unsigned long long _glfwPlatformAddTimer(double interval, bool repeats, GLFWuserdatafreefun callback, void *callback_data, GLFWuserdatafreefun free_callback) { - return addTimer(&_glfw.x11.eventLoopData, "user timer", interval, 1, repeats, callback, callback_data, free_callback); -} - -void _glfwPlatformRemoveTimer(unsigned long long timer_id) { - removeTimer(&_glfw.x11.eventLoopData, timer_id); -} +#include "main_loop.h"