add machine shutdown callback

This commit is contained in:
Torkel 2020-10-22 18:39:38 +09:00
parent 3327050ca6
commit 067598814d
6 changed files with 96 additions and 0 deletions

View File

@ -618,6 +618,23 @@ void window_close_callback(GLFWwindow* window)
}
@endcode
If you wish to be notified when the user attempts to shut down the machine, 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.
@code
void machine_shutdown_callback(GLFWwindow* window)
{
initiate_save_of_important_data_to_disk();
}
@endcode
@subsection window_size Window size

View File

@ -1412,6 +1412,21 @@ 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
*
* @ingroup window
*/
typedef void (* 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 +3858,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);
void _glfwInputMachineShutdown(_GLFWwindow* window);
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor);
void _glfwInputKey(_GLFWwindow* window,

View File

@ -1199,6 +1199,13 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
DragFinish(drop);
return 0;
}
case WM_QUERYENDSESSION:
case WM_ENDSESSION:
{
_glfwInputMachineShutdown(window);
return 0;
}
}
return DefWindowProcW(hWnd, uMsg, wParam, lParam);

View File

@ -138,6 +138,14 @@ void _glfwInputWindowCloseRequest(_GLFWwindow* window)
window->callbacks.close((GLFWwindow*) window);
}
// Notifies shared code that the machine is shuting down
//
void _glfwInputMachineShutdown(_GLFWwindow *window)
{
if (window->callbacks.shutdown)
window->callbacks.shutdown((GLFWwindow*) window);
}
// Notifies shared code that a window has changed its desired monitor
//
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor)
@ -1020,6 +1028,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,13 @@ static void window_close_callback(GLFWwindow* window)
glfwSetWindowShouldClose(window, slot->closeable);
}
static void window_machine_shutdown_callback(GLFWwindow* window)
{
printf("%08x at %0.3f: Machine shutdown detected\n",
counter++,
glfwGetTime());
}
static void window_refresh_callback(GLFWwindow* window)
{
Slot* slot = glfwGetWindowUserPointer(window);
@ -627,6 +634,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);