From bab61702f3c65f9b5c3370756e4adb7d4c743372 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 18 Jun 2024 17:16:47 +0000 Subject: [PATCH] Handle SIGWINCH in editor(1). --- editor/editor.c | 16 ++++++++++++++++ editor/input.c | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/editor/editor.c b/editor/editor.c index 2aab3f28..36965f71 100644 --- a/editor/editor.c +++ b/editor/editor.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -270,6 +271,14 @@ bool editor_save_file(struct editor* editor, const char* path) return fclose(fp) != EOF; } +volatile sig_atomic_t had_sigwinch = 0; + +static void sigwinch(int signum) +{ + (void) signum; + had_sigwinch = 1; +} + int main(int argc, char* argv[]) { setlocale(LC_ALL, ""); @@ -279,6 +288,13 @@ int main(int argc, char* argv[]) if ( !isatty(1) ) err(1, "standard output"); + sigset_t sigwinch_set; + sigemptyset(&sigwinch_set); + sigaddset(&sigwinch_set, SIGWINCH); + sigprocmask(SIG_BLOCK, &sigwinch_set, NULL); + struct sigaction sa = { .sa_handler = sigwinch }; + sigaction(SIGWINCH, &sa, NULL); + struct editor editor; initialize_editor(&editor); diff --git a/editor/input.c b/editor/input.c index ca64bc8b..ca41bb7b 100644 --- a/editor/input.c +++ b/editor/input.c @@ -353,12 +353,22 @@ void editor_input_process_byte(struct editor_input* editor_input, } } +extern volatile sig_atomic_t had_sigwinch; + void editor_input_process(struct editor_input* editor_input, struct editor* editor) { + sigset_t sigset; + sigemptyset(&sigset); struct pollfd pfd = { .fd = 0, .events = POLLIN }; - do editor_input_process_byte(editor_input, editor); - while ( poll(&pfd, 1, 0) == 1 ); + struct timespec timeout = { .tv_sec = 0 }; + struct timespec* timeout_ptr = NULL; + while ( !had_sigwinch && ppoll(&pfd, 1, timeout_ptr, &sigset) == 1 ) + { + editor_input_process_byte(editor_input, editor); + timeout_ptr = &timeout; + } + had_sigwinch = 0; } void editor_input_end(struct editor_input* editor_input)