diff options
| author | dam <dam@gudinoff> | 2023-11-12 00:31:44 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-11-12 00:31:44 +0000 |
| commit | 06a4894c50c56dd8236c1e401bf422d0a8f47c14 (patch) | |
| tree | f7e3f9d7deb6e003bb3621df7ceeb4e4ed22002d /TUI/unix.jai | |
| parent | 90beac697609d7d4926e18a0eb0c576b9a22058b (diff) | |
| download | task-time-tracker-06a4894c50c56dd8236c1e401bf422d0a8f47c14.tar.zst task-time-tracker-06a4894c50c56dd8236c1e401bf422d0a8f47c14.zip | |
Moved OS dependent code.
Diffstat (limited to 'TUI/unix.jai')
| -rw-r--r-- | TUI/unix.jai | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/TUI/unix.jai b/TUI/unix.jai index a35a60a..7ff1e72 100644 --- a/TUI/unix.jai +++ b/TUI/unix.jai @@ -137,7 +137,6 @@ #scope_export OS_prepare_terminal :: () { - error: s32; tcgetattr(STDIN_FILENO, *initial_tio_mode); // TODO Log on error. raw_tio_mode = initial_tio_mode; raw_tio_mode.c_iflag &= ~(.IGNBRK | .BRKINT | .PARMRK | .ISTRIP | .INLCR | .IGNCR | .ICRNL | .IXON); @@ -148,10 +147,16 @@ OS_prepare_terminal :: () { raw_tio_mode.c_cc[Control_Chars.VMIN] = 1; raw_tio_mode.c_cc[Control_Chars.VTIME] = 0; tcsetattr(STDIN_FILENO, 0, *raw_tio_mode); // TODO Log on error. + + was_resized = false; + init(*resize_mutex, "resize_mutex"); + prepare_resize_handler(); } OS_reset_terminal :: () { tcsetattr(STDIN_FILENO, 0, *initial_tio_mode); // TODO Log on error. + + restore_resize_handler(); } OS_read_input :: (buffer: *u8, bytes_to_read: s64) -> bytes_read: s64, error: bool = false, error_message: string = "" { @@ -162,3 +167,48 @@ OS_read_input :: (buffer: *u8, bytes_to_read: s64) -> bytes_read: s64, error: bo } return bytes_read; } + +OS_wait_for_input :: (timeout_milliseconds: s32) -> is_input_available: bool { + fds := pollfd.[ .{ fd = STDIN_FILENO, events = POLLIN, revents = 0 } ]; + nfds := fds.count; + poll_return := poll(fds.data, xx nfds, xx timeout_milliseconds); // TODO Wait for input using poll syscall. This breaks and throws '-1 | 4 | Interrupted system call' when we resize the window while polling. + error_code, error_message := get_error_value_and_string(); // FIXME Not used. + return ifx poll_return > 0 then true else false; +} + +resize_mutex : Mutex; +was_resized : bool; + +OS_was_terminal_resized :: () -> bool { + lock(*resize_mutex); + defer unlock(*resize_mutex); + defer was_resized = false; + return was_resized; +} + +resize_handler :: (signal_code : s32) #c_call { + new_context : Context; + push_context new_context { + print("SIGNAL:%", signal_code); + if signal_code != SIGWINCH then return; + if was_resized == true then return; + print("RESIZE\n"); + lock(*resize_mutex); + defer unlock(*resize_mutex); + was_resized = true; + } +} + +prepare_resize_handler :: () { + sa : sigaction_t; + sa.sa_handler = resize_handler; + sigemptyset(*(sa.sa_mask)); + sa.sa_flags = SA_RESTART; + sigaction(SIGWINCH, *sa, null); +} + +restore_resize_handler :: () { + sa : sigaction_t; + sa.sa_handler = SIG_DFL; + sigaction(SIGWINCH, null, *sa); +} |
