Add the ipc shutdown event (#2652)

This event is triggered when the connection to the ipc is about to
shutdown because of a user action such as with a `restart` or `exit`
command. The `change` field indicates why the ipc is shutting down. It
can be either "restart" or "exit".

fixes #2318
This commit is contained in:
Tony Crisci
2017-01-22 17:08:32 -05:00
committed by Michael Stapelberg
parent 564945bc14
commit 04dcf42397
7 changed files with 133 additions and 8 deletions

View File

@ -1562,7 +1562,7 @@ void cmd_exit(I3_CMD) {
#ifdef I3_ASAN_ENABLED
__lsan_do_leak_check();
#endif
ipc_shutdown();
ipc_shutdown(SHUTDOWN_REASON_EXIT);
unlink(config.ipc_socket_path);
xcb_disconnect(conn);
exit(0);
@ -1595,7 +1595,7 @@ void cmd_reload(I3_CMD) {
*/
void cmd_restart(I3_CMD) {
LOG("restarting i3\n");
ipc_shutdown();
ipc_shutdown(SHUTDOWN_REASON_RESTART);
unlink(config.ipc_socket_path);
/* We need to call this manually since atexit handlers dont get called
* when exec()ing */

View File

@ -62,11 +62,39 @@ void ipc_send_event(const char *event, uint32_t message_type, const char *payloa
}
/*
* Calls shutdown() on each socket and closes it. This function to be called
* For shutdown events, we send the reason for the shutdown.
*/
static void ipc_send_shutdown_event(shutdown_reason_t reason) {
yajl_gen gen = ygenalloc();
y(map_open);
ystr("change");
if (reason == SHUTDOWN_REASON_RESTART) {
ystr("restart");
} else if (reason == SHUTDOWN_REASON_EXIT) {
ystr("exit");
}
y(map_close);
const unsigned char *payload;
ylength length;
y(get_buf, &payload, &length);
ipc_send_event("shutdown", I3_IPC_EVENT_SHUTDOWN, (const char *)payload);
y(free);
}
/*
* Calls shutdown() on each socket and closes it. This function is to be called
* when exiting or restarting only!
*
*/
void ipc_shutdown(void) {
void ipc_shutdown(shutdown_reason_t reason) {
ipc_send_shutdown_event(reason);
ipc_client *current;
while (!TAILQ_EMPTY(&all_clients)) {
current = TAILQ_FIRST(&all_clients);

View File

@ -259,7 +259,7 @@ void i3_restart(bool forget_layout) {
restore_geometry();
ipc_shutdown();
ipc_shutdown(SHUTDOWN_REASON_RESTART);
LOG("restarting \"%s\"...\n", start_argv[0]);
/* make sure -a is in the argument list or add it */