mirror of
https://github.com/glfw/glfw.git
synced 2025-10-02 21:00:57 +00:00
add machine shutdown callback
This commit is contained in:
parent
3327050ca6
commit
067598814d
@ -618,6 +618,23 @@ void window_close_callback(GLFWwindow* window)
|
|||||||
}
|
}
|
||||||
@endcode
|
@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
|
@subsection window_size Window size
|
||||||
|
|
||||||
|
@ -1412,6 +1412,21 @@ typedef void (* GLFWwindowsizefun)(GLFWwindow*,int,int);
|
|||||||
*/
|
*/
|
||||||
typedef void (* GLFWwindowclosefun)(GLFWwindow*);
|
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.
|
/*! @brief The function pointer type for window content refresh callbacks.
|
||||||
*
|
*
|
||||||
* This is 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);
|
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.
|
/*! @brief Sets the refresh callback for the specified window.
|
||||||
*
|
*
|
||||||
* This function sets the refresh callback of the specified window, which is
|
* This function sets the refresh callback of the specified window, which is
|
||||||
|
@ -408,6 +408,7 @@ struct _GLFWwindow
|
|||||||
GLFWwindowposfun pos;
|
GLFWwindowposfun pos;
|
||||||
GLFWwindowsizefun size;
|
GLFWwindowsizefun size;
|
||||||
GLFWwindowclosefun close;
|
GLFWwindowclosefun close;
|
||||||
|
GLFWmachineShutdownfun shutdown;
|
||||||
GLFWwindowrefreshfun refresh;
|
GLFWwindowrefreshfun refresh;
|
||||||
GLFWwindowfocusfun focus;
|
GLFWwindowfocusfun focus;
|
||||||
GLFWwindowiconifyfun iconify;
|
GLFWwindowiconifyfun iconify;
|
||||||
@ -729,6 +730,7 @@ void _glfwInputWindowIconify(_GLFWwindow* window, GLFWbool iconified);
|
|||||||
void _glfwInputWindowMaximize(_GLFWwindow* window, GLFWbool maximized);
|
void _glfwInputWindowMaximize(_GLFWwindow* window, GLFWbool maximized);
|
||||||
void _glfwInputWindowDamage(_GLFWwindow* window);
|
void _glfwInputWindowDamage(_GLFWwindow* window);
|
||||||
void _glfwInputWindowCloseRequest(_GLFWwindow* window);
|
void _glfwInputWindowCloseRequest(_GLFWwindow* window);
|
||||||
|
void _glfwInputMachineShutdown(_GLFWwindow* window);
|
||||||
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor);
|
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor);
|
||||||
|
|
||||||
void _glfwInputKey(_GLFWwindow* window,
|
void _glfwInputKey(_GLFWwindow* window,
|
||||||
|
@ -1199,6 +1199,13 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
|
|||||||
DragFinish(drop);
|
DragFinish(drop);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case WM_QUERYENDSESSION:
|
||||||
|
case WM_ENDSESSION:
|
||||||
|
{
|
||||||
|
_glfwInputMachineShutdown(window);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
|
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
|
||||||
|
19
src/window.c
19
src/window.c
@ -138,6 +138,14 @@ void _glfwInputWindowCloseRequest(_GLFWwindow* window)
|
|||||||
window->callbacks.close((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
|
// Notifies shared code that a window has changed its desired monitor
|
||||||
//
|
//
|
||||||
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor)
|
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor)
|
||||||
@ -1020,6 +1028,17 @@ GLFWAPI GLFWwindowclosefun glfwSetWindowCloseCallback(GLFWwindow* handle,
|
|||||||
return cbfun;
|
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,
|
GLFWAPI GLFWwindowrefreshfun glfwSetWindowRefreshCallback(GLFWwindow* handle,
|
||||||
GLFWwindowrefreshfun cbfun)
|
GLFWwindowrefreshfun cbfun)
|
||||||
{
|
{
|
||||||
|
@ -329,6 +329,13 @@ static void window_close_callback(GLFWwindow* window)
|
|||||||
glfwSetWindowShouldClose(window, slot->closeable);
|
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)
|
static void window_refresh_callback(GLFWwindow* window)
|
||||||
{
|
{
|
||||||
Slot* slot = glfwGetWindowUserPointer(window);
|
Slot* slot = glfwGetWindowUserPointer(window);
|
||||||
@ -627,6 +634,7 @@ int main(int argc, char** argv)
|
|||||||
glfwSetFramebufferSizeCallback(slots[i].window, framebuffer_size_callback);
|
glfwSetFramebufferSizeCallback(slots[i].window, framebuffer_size_callback);
|
||||||
glfwSetWindowContentScaleCallback(slots[i].window, window_content_scale_callback);
|
glfwSetWindowContentScaleCallback(slots[i].window, window_content_scale_callback);
|
||||||
glfwSetWindowCloseCallback(slots[i].window, window_close_callback);
|
glfwSetWindowCloseCallback(slots[i].window, window_close_callback);
|
||||||
|
glfwSetMachineShutdownCallback(slots[i].window, window_machine_shutdown_callback);
|
||||||
glfwSetWindowRefreshCallback(slots[i].window, window_refresh_callback);
|
glfwSetWindowRefreshCallback(slots[i].window, window_refresh_callback);
|
||||||
glfwSetWindowFocusCallback(slots[i].window, window_focus_callback);
|
glfwSetWindowFocusCallback(slots[i].window, window_focus_callback);
|
||||||
glfwSetWindowIconifyCallback(slots[i].window, window_iconify_callback);
|
glfwSetWindowIconifyCallback(slots[i].window, window_iconify_callback);
|
||||||
|
Loading…
Reference in New Issue
Block a user