mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-02-18 22:05:25 -05:00
terminal: call user-defined callback when destroying terminal
main uses this to get the exit code of the terminal.
This commit is contained in:
parent
70b236d66d
commit
1e41a25f00
4 changed files with 38 additions and 13 deletions
33
main.c
33
main.c
|
|
@ -26,14 +26,27 @@
|
||||||
static void
|
static void
|
||||||
print_usage(const char *prog_name)
|
print_usage(const char *prog_name)
|
||||||
{
|
{
|
||||||
printf("Usage: %s [OPTION]...\n", prog_name);
|
printf("Usage: %s [OPTIONS]...\n", prog_name);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("Options:\n");
|
printf("Options:\n");
|
||||||
printf(" -f,--font=FONT comma separated list of fonts in fontconfig format (monospace)\n"
|
printf(" -f,--font=FONT comma separated list of fonts in fontconfig format (monospace)\n"
|
||||||
" -t,--term=TERM value to set the environment variable TERM to (foot)\n"
|
" -t,--term=TERM value to set the environment variable TERM to (foot)\n"
|
||||||
" -g,--geometry=WIDTHxHEIGHT set initial width and height\n"
|
" -g,--geometry=WIDTHxHEIGHT set initial width and height\n"
|
||||||
|
" -s,--server run as a server\n"
|
||||||
" -v,--version show the version number and quit\n");
|
" -v,--version show the version number and quit\n");
|
||||||
printf("\n");
|
}
|
||||||
|
|
||||||
|
struct shutdown_context {
|
||||||
|
struct terminal **term;
|
||||||
|
int exit_code;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
term_shutdown_cb(void *data, int exit_code)
|
||||||
|
{
|
||||||
|
struct shutdown_context *ctx = data;
|
||||||
|
*ctx->term = NULL;
|
||||||
|
ctx->exit_code = exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
@ -136,6 +149,7 @@ main(int argc, char *const *argv)
|
||||||
struct fdm *fdm = NULL;
|
struct fdm *fdm = NULL;
|
||||||
struct wayland *wayl = NULL;
|
struct wayland *wayl = NULL;
|
||||||
struct terminal *term = NULL;
|
struct terminal *term = NULL;
|
||||||
|
struct shutdown_context shutdown_ctx = {.term = &term, .exit_code = EXIT_FAILURE};
|
||||||
|
|
||||||
if ((fdm = fdm_init()) == NULL)
|
if ((fdm = fdm_init()) == NULL)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
@ -143,7 +157,10 @@ main(int argc, char *const *argv)
|
||||||
if ((wayl = wayl_init(fdm)) == NULL)
|
if ((wayl = wayl_init(fdm)) == NULL)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
if ((term = term_init(&conf, fdm, wayl, argc, argv)) == NULL)
|
if (!as_server && (term = term_init(&conf, fdm, wayl, argc, argv,
|
||||||
|
&term_shutdown_cb, &shutdown_ctx)) == NULL)
|
||||||
|
goto out;
|
||||||
|
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
while (tll_length(wayl->terms) > 0) {
|
while (tll_length(wayl->terms) > 0) {
|
||||||
|
|
@ -157,15 +174,11 @@ main(int argc, char *const *argv)
|
||||||
out:
|
out:
|
||||||
shm_fini();
|
shm_fini();
|
||||||
|
|
||||||
tll_foreach(wayl->terms, it)
|
server_destroy(server);
|
||||||
term_destroy(it->item);
|
term_destroy(term);
|
||||||
|
|
||||||
int child_ret = wayl->last_exit_value;
|
|
||||||
|
|
||||||
wayl_destroy(wayl);
|
wayl_destroy(wayl);
|
||||||
fdm_destroy(fdm);
|
fdm_destroy(fdm);
|
||||||
config_free(conf);
|
config_free(conf);
|
||||||
|
|
||||||
return ret == EXIT_SUCCESS ? child_ret : ret;
|
return ret == EXIT_SUCCESS && !as_server ? shutdown_ctx.exit_code : ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -289,7 +289,8 @@ initialize_fonts(struct terminal *term, const struct config *conf)
|
||||||
|
|
||||||
struct terminal *
|
struct terminal *
|
||||||
term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl,
|
term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl,
|
||||||
int argc, char *const *argv)
|
int argc, char *const *argv,
|
||||||
|
void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data)
|
||||||
{
|
{
|
||||||
int ptmx = -1;
|
int ptmx = -1;
|
||||||
int flash_fd = -1;
|
int flash_fd = -1;
|
||||||
|
|
@ -411,6 +412,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl,
|
||||||
.lower_fd = delay_lower_fd,
|
.lower_fd = delay_lower_fd,
|
||||||
.upper_fd = delay_upper_fd,
|
.upper_fd = delay_upper_fd,
|
||||||
},
|
},
|
||||||
|
.shutdown_cb = shutdown_cb,
|
||||||
|
.shutdown_data = shutdown_data,
|
||||||
};
|
};
|
||||||
|
|
||||||
initialize_color_cube(term);
|
initialize_color_cube(term);
|
||||||
|
|
@ -484,8 +487,12 @@ fdm_shutdown(struct fdm *fdm, int fd, int events, void *data)
|
||||||
assert(wayl->focused != term);
|
assert(wayl->focused != term);
|
||||||
assert(wayl->moused != term);
|
assert(wayl->moused != term);
|
||||||
|
|
||||||
|
void (*cb)(void *, int) = term->shutdown_cb;
|
||||||
|
void *cb_data = term->shutdown_data;
|
||||||
|
|
||||||
int exit_code = term_destroy(term);
|
int exit_code = term_destroy(term);
|
||||||
|
if (cb != NULL)
|
||||||
|
cb(cb_data, exit_code);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -287,12 +287,18 @@ struct terminal {
|
||||||
int lower_fd;
|
int lower_fd;
|
||||||
int upper_fd;
|
int upper_fd;
|
||||||
} delayed_render_timer;
|
} delayed_render_timer;
|
||||||
|
|
||||||
|
bool is_shutting_down;
|
||||||
|
void (*shutdown_cb)(void *data, int exit_code);
|
||||||
|
void *shutdown_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct config;
|
struct config;
|
||||||
struct terminal *term_init(
|
struct terminal *term_init(
|
||||||
const struct config *conf, struct fdm *fdm, struct wayland *wayl,
|
const struct config *conf, struct fdm *fdm, struct wayland *wayl,
|
||||||
int argc, char *const *argv);
|
int argc, char *const *argv,
|
||||||
|
void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data);
|
||||||
|
|
||||||
bool term_shutdown(struct terminal *term);
|
bool term_shutdown(struct terminal *term);
|
||||||
int term_destroy(struct terminal *term);
|
int term_destroy(struct terminal *term);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -150,7 +150,6 @@ struct wayland {
|
||||||
tll(struct terminal *) terms;
|
tll(struct terminal *) terms;
|
||||||
struct terminal *focused;
|
struct terminal *focused;
|
||||||
struct terminal *moused;
|
struct terminal *moused;
|
||||||
int last_exit_value; /* TODO: exit value from client(s)? */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct wayland *wayl_init(struct fdm *fdm);
|
struct wayland *wayl_init(struct fdm *fdm);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue