diff options
| author | dam <dam@gudinoff> | 2023-09-20 18:00:20 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-09-20 18:00:20 +0100 |
| commit | 65adffcf572c98c0198affcdf729d989fec253ae (patch) | |
| tree | 31f2c0fe7ca0cec7e7b932bb6c2f8126a7577bac /TUI/windows.jai | |
| parent | 9e2fc467ad0e779734d836656875cf92bcb5732a (diff) | |
| download | task-time-tracker-65adffcf572c98c0198affcdf729d989fec253ae.tar.zst task-time-tracker-65adffcf572c98c0198affcdf729d989fec253ae.zip | |
Moved TUI into a module with split OS-based implementations.
Diffstat (limited to 'TUI/windows.jai')
| -rw-r--r-- | TUI/windows.jai | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/TUI/windows.jai b/TUI/windows.jai new file mode 100644 index 0000000..ef0cfa8 --- /dev/null +++ b/TUI/windows.jai @@ -0,0 +1,111 @@ +#import "Windows"; + + + kernel32 :: #system_library "kernel32"; + + GetConsoleScreenBufferInfo :: (hConsoleOutput: HANDLE, lpConsoleScreenBufferInfo: *CONSOLE_SCREEN_BUFFER_INFO) -> bool #foreign kernel32; + ReadConsoleA :: (hConsoleHandle: HANDLE, buff : *u8, chars_to_read : s32, chars_read : *s32, lpInputControl := *void ) -> bool #foreign kernel32; + // ReadConsole :: (hConsoleInput: HANDLE, lpBuffer: *u8, nNumberOfCharsToRead: s32, lpNumberOfCharsRead: *s32, pInputControl := *void) -> bool #foreign kernel32; + GetConsoleMode :: (hConsoleHandle: HANDLE, lpMode: *u32) -> bool #foreign kernel32; + SetConsoleMode :: (hConsoleHandle: HANDLE, dwMode: u32) -> bool #foreign kernel32; + GetLastError :: () -> s32 #foreign kernel32; + + ENABLE_VIRTUAL_TERMINAL_INPUT :: 0x0200; + + ENABLE_PROCESSED_OUTPUT :: 0x0001; + ENABLE_WRAP_AT_EOL_OUTPUT :: 0x0002; + ENABLE_VIRTUAL_TERMINAL_PROCESSING :: 0x0004; + DISABLE_NEWLINE_AUTO_RETURN :: 0x0008; + ENABLE_LVB_GRID_WORLDWIDE :: 0x0010; + + SHORT :: s16; + WORD :: u16; + DWORD :: s32; + + COORD :: struct { + X : SHORT; + Y : SHORT; + } + + SMALL_RECT :: struct { + Left : SHORT; + Top : SHORT; + Right : SHORT; + Bottom : SHORT; + } + + CONSOLE_SCREEN_BUFFER_INFO :: struct { + dwSize : COORD; + dwCursorPosition : COORD; + + wAttributes : WORD; + srWindow : SMALL_RECT; + dwMaximumWindowSize : COORD; + } + + + stdin: HANDLE; + initial_stdin_mode: u32; + stdout: HANDLE; + initial_stdout_mode: u32; + + +OS_prepare_terminal :: () { + print("TODO TUI\n", to_standard_error = true); + + + // stdin + stdin = GetStdHandle(STD_INPUT_HANDLE ); + if stdin == INVALID_HANDLE_VALUE { + print("Invalid input handler.", to_standard_error = true); + return; + } + if GetConsoleMode(stdin, *initial_stdin_mode) == false { + print("Failed to get input mode.", to_standard_error = true); + return; + } + if SetConsoleMode(stdin, initial_stdin_mode | ENABLE_VIRTUAL_TERMINAL_INPUT) == false { + print("Failed to set input mode: %.", GetLastError(), to_standard_error = true); + return; + } + + // stdout + stdout = GetStdHandle(STD_OUTPUT_HANDLE); + outMode: u32 = 0; + if stdout == INVALID_HANDLE_VALUE { + print("Invalid output handler.", to_standard_error = true); + return; + } + if GetConsoleMode(stdout, *initial_stdout_mode) == false { + print("Failed to get output mode.", to_standard_error = true); + return; + } + if SetConsoleMode(stdout, initial_stdout_mode | ENABLE_PROCESSED_OUTPUT| ENABLE_VIRTUAL_TERMINAL_PROCESSING) == false { + print("Failed to set output mode: %.", GetLastError(), to_standard_error = true); + return; + } +} + +OS_reset_terminal :: () { + print("TODO TUI\n", to_standard_error = true); + + if SetConsoleMode(stdin, initial_stdin_mode) == false { + print("Failed to reset input mode: %.", GetLastError(), to_standard_error = true); + return; + } + + if SetConsoleMode(stdout, initial_stdout_mode) == false { + print("Failed to reset output mode: %.", GetLastError(), to_standard_error = true); + return; + } +} + +OS_get_terminal_size :: () -> rows: int, columns: int { + + ScreenBufferInfo: CONSOLE_SCREEN_BUFFER_INFO; + GetConsoleScreenBufferInfo(stdout, *ScreenBufferInfo); + columns := ScreenBufferInfo.srWindow.Right - ScreenBufferInfo.srWindow.Left + 1; + rows := ScreenBufferInfo.srWindow.Bottom - ScreenBufferInfo.srWindow.Top + 1; + + return rows, columns; +} |
