This commit is contained in:
Torkel Danielsson 2020-12-28 18:44:02 +01:00 committed by GitHub
commit 23aea7fe08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 105 additions and 0 deletions

View File

@ -221,6 +221,7 @@ information on what to include when reporting a bug.
(#442)
- [EGL] Added ANGLE backend selection via `EGL_ANGLE_platform_angle` extension
(#1380)
- [Win32] Added a machine shutdown callback that can interrupt shutdowns
## Contact
@ -275,6 +276,7 @@ skills.
- Bailey Cosier
- Noel Cower
- Jason Daly
- Torkel Danielsson
- Jarrod Davis
- Olivier Delannoy
- Paul R. Deppe

View File

@ -618,6 +618,28 @@ void window_close_callback(GLFWwindow* window)
}
@endcode
If you wish to be notified when the user attempts to shut down the machine, or
interrupt a shutdown, then set a machine shutdown callback.
@code
glfwSetMachineShutdownCallback(window, machine_shutdown_callback);
@endcode
The callback function is called when GLFW detects that the machine is shutting down.
It can be used for example to save data to disk in order to minimize risk of data loss.
Another use is to interrupt the shutdown (return false).
@code
int machine_shutdown_callback(GLFWwindow* window)
{
initiate_save_of_important_data_to_disk();
if (prevent_machine_shutdown())
return GLFW_FALSE;
else
return GLFW_TRUE;
}
@endcode
@subsection window_size Window size

View File

@ -1412,6 +1412,22 @@ typedef void (* GLFWwindowsizefun)(GLFWwindow*,int,int);
*/
typedef void (* GLFWwindowclosefun)(GLFWwindow*);
/*! @brief The function pointer type for machine shutdown callbacks.
*
* This is the function pointer type for machine shutdown callbacks.
* A machine shutdown callback function has the following signature:
* @code
* void function_name(GLFWwindow* window)
* @endcode
*
* @sa @ref machine_shutdown
* @sa @ref glfwSetMachineShutdownCallback
* @return GLFW_TRUE to accept the shutdown or GLFW_FALSE to interrupt it
*
* @ingroup window
*/
typedef int (* GLFWmachineShutdownfun)(GLFWwindow*);
/*! @brief The function pointer type for window content refresh callbacks.
*
* This is the function pointer type for window content refresh callbacks.
@ -3843,6 +3859,34 @@ GLFWAPI GLFWwindowsizefun glfwSetWindowSizeCallback(GLFWwindow* window, GLFWwind
*/
GLFWAPI GLFWwindowclosefun glfwSetWindowCloseCallback(GLFWwindow* window, GLFWwindowclosefun callback);
/*! @brief Sets the machine shutdown callback for the specified window.
*
* This function sets the machine shutdown callback of the specified window, which is
* called when the operating system is preparing to shut down the machine.
*
* @param[in] window The window whose callback to set.
* @param[in] callback The new callback, or `NULL` to remove the currently set
* callback.
* @return The previously set callback, or `NULL` if no callback was set or the
* library had not been [initialized](@ref intro_init).
*
* @callback_signature
* @code
* void function_name(GLFWwindow* window)
* @endcode
* For more information about the callback parameters, see the
* [function pointer type](@ref GLFWmachineShutdownfun).
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref machine_shutdown
*
* @ingroup window
*/
GLFWAPI GLFWmachineShutdownfun glfwSetMachineShutdownCallback(GLFWwindow* window, GLFWmachineShutdownfun callback);
/*! @brief Sets the refresh callback for the specified window.
*
* This function sets the refresh callback of the specified window, which is

View File

@ -408,6 +408,7 @@ struct _GLFWwindow
GLFWwindowposfun pos;
GLFWwindowsizefun size;
GLFWwindowclosefun close;
GLFWmachineShutdownfun shutdown;
GLFWwindowrefreshfun refresh;
GLFWwindowfocusfun focus;
GLFWwindowiconifyfun iconify;
@ -729,6 +730,7 @@ void _glfwInputWindowIconify(_GLFWwindow* window, GLFWbool iconified);
void _glfwInputWindowMaximize(_GLFWwindow* window, GLFWbool maximized);
void _glfwInputWindowDamage(_GLFWwindow* window);
void _glfwInputWindowCloseRequest(_GLFWwindow* window);
GLFWbool _glfwInputMachineShutdown(_GLFWwindow* window);
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor);
void _glfwInputKey(_GLFWwindow* window,

View File

@ -627,6 +627,11 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
return 0;
}
case WM_QUERYENDSESSION:
{
return _glfwInputMachineShutdown(window);
}
case WM_INPUTLANGCHANGE:
{
_glfwUpdateKeyNamesWin32();

View File

@ -138,6 +138,16 @@ void _glfwInputWindowCloseRequest(_GLFWwindow* window)
window->callbacks.close((GLFWwindow*) window);
}
// Notifies shared code that the machine is shuting down
//
GLFWbool _glfwInputMachineShutdown(_GLFWwindow *window)
{
if (window->callbacks.shutdown) {
return window->callbacks.shutdown((GLFWwindow*) window);
}
return GLFW_TRUE;
}
// Notifies shared code that a window has changed its desired monitor
//
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor)
@ -1020,6 +1030,17 @@ GLFWAPI GLFWwindowclosefun glfwSetWindowCloseCallback(GLFWwindow* handle,
return cbfun;
}
GLFWAPI GLFWmachineShutdownfun glfwSetMachineShutdownCallback(GLFWwindow* handle,
GLFWmachineShutdownfun cbfun)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP_POINTERS(window->callbacks.shutdown, cbfun);
return cbfun;
}
GLFWAPI GLFWwindowrefreshfun glfwSetWindowRefreshCallback(GLFWwindow* handle,
GLFWwindowrefreshfun cbfun)
{

View File

@ -329,6 +329,14 @@ static void window_close_callback(GLFWwindow* window)
glfwSetWindowShouldClose(window, slot->closeable);
}
static int window_machine_shutdown_callback(GLFWwindow* window)
{
printf("%08x at %0.3f: Machine shutdown detected\n",
counter++,
glfwGetTime());
return GLFW_TRUE;
}
static void window_refresh_callback(GLFWwindow* window)
{
Slot* slot = glfwGetWindowUserPointer(window);
@ -627,6 +635,7 @@ int main(int argc, char** argv)
glfwSetFramebufferSizeCallback(slots[i].window, framebuffer_size_callback);
glfwSetWindowContentScaleCallback(slots[i].window, window_content_scale_callback);
glfwSetWindowCloseCallback(slots[i].window, window_close_callback);
glfwSetMachineShutdownCallback(slots[i].window, window_machine_shutdown_callback);
glfwSetWindowRefreshCallback(slots[i].window, window_refresh_callback);
glfwSetWindowFocusCallback(slots[i].window, window_focus_callback);
glfwSetWindowIconifyCallback(slots[i].window, window_iconify_callback);