aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TUI/module.jai58
-rw-r--r--TUI/unix.jai52
2 files changed, 53 insertions, 57 deletions
diff --git a/TUI/module.jai b/TUI/module.jai
index 6ca6f59..18df2c5 100644
--- a/TUI/module.jai
+++ b/TUI/module.jai
@@ -158,23 +158,17 @@ start :: () {
write_strings(Commands.HideCursor, Commands.SaveCursorPosition, Commands.EnterAlternateBuffer, Commands.SetUTF8);
OS_prepare_terminal();
-
- was_resized = false;
- init(*resize_mutex, "resize_mutex");
+
input_string.data = input_buffer.data;
input_string.count = 0;
initialized = true;
-
- set_handler(); // TODO Move to beter place.
}
stop :: () {
if initialized == false return;
initialized = false;
-
- restore_handler(); // TODO Move to beter place.
-
+
OS_reset_terminal();
write_strings(Commands.EnterMainBuffer, Commands.RestoreCursorPosition, Commands.ShowCursor);
}
@@ -301,52 +295,4 @@ get_cursor_position :: () -> row: int, column: int {
}
else #if OS == .LINUX || OS == .MACOS {
// Prototyping zone... keep clear!
-
- 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();
- 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;
- }
-
- process_resize :: (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;
- }
- }
-
- // TODO Rename this procedure.
- set_handler :: () {
- sa : sigaction_t;
- sa.sa_handler = process_resize;
- sigemptyset(*(sa.sa_mask));
- sa.sa_flags = SA_RESTART;
- sigaction(SIGWINCH, *sa, null);
- }
-
- // TODO Rename this procedure.
- restore_handler :: () {
- sa : sigaction_t;
- sa.sa_handler = SIG_DFL;
- sigaction(SIGWINCH, null, *sa);
- }
-
}
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);
+}