diff options
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/TUI/unix.jai | 201 |
1 files changed, 28 insertions, 173 deletions
diff --git a/modules/TUI/unix.jai b/modules/TUI/unix.jai index bb22030..e1c0b3a 100644 --- a/modules/TUI/unix.jai +++ b/modules/TUI/unix.jai @@ -1,11 +1,5 @@ #scope_file -/* -TODO : then do a good implementation of the libc functions about attributes... -*/ - -USE_LIBC :: true; - #import "Atomics"; #import "System"; #import "POSIX"; @@ -13,7 +7,7 @@ USE_LIBC :: true; // Queue selector used in tcflush(...). // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios-struct.h // MACOS : https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/termios.h.auto.html - QueueSelector :: enum s32 { + Queue_Selector :: enum s32 { #if OS == { case .LINUX; TCIFLUSH :: 0; // Discard data received but not yet read. @@ -30,7 +24,7 @@ USE_LIBC :: true; // Optional actions used in tcsetattr(...). // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios-tcflow.h // MACOS : https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/termios.h.auto.html - OptionalActions :: enum s32 { + Optional_Actions :: enum s32 { TCSANOW :: 0; // Change immediately. TCSADRAIN :: 1; // Change when pending output is written. TCSAFLUSH :: 2; // Flush pending input before changing. @@ -74,7 +68,7 @@ USE_LIBC :: true; ICRNL :: 0x00000100; // Map CR to NL on input. #if OS == { - + case .LINUX; IXON :: 0x00000400; // Enable start/stop output control. IXANY :: 0x00000800; // Any character will restart after stop. @@ -84,12 +78,11 @@ USE_LIBC :: true; IXON :: 0x00000200; // Enable start/stop output control. IXANY :: 0x00000400; // Any character will restart after stop. IXOFF :: 0x00000800; // Enable start/stop input control. - } } // Output modes. - // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios.h + // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios-c_oflag.h // MACOS : https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/termios.h.auto.html Output_Modes :: enum_flags u32 { #if OS == { @@ -113,7 +106,7 @@ USE_LIBC :: true; } // Control modes. - // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios.h + // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios-c_cflag.h // MACOS : https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/termios.h.auto.html Control_Modes :: enum u32 { #if OS == { @@ -147,64 +140,54 @@ USE_LIBC :: true; } // Local modes. - // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios.h + // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios-c_lflag.h // MACOS : https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/termios.h.auto.html Local_Modes :: enum_flags u32 { #if OS == { case .LINUX; ISIG :: 0x00000001; // Enable signals. - ICANON :: 0x00000002; // Do erase and kill processing. + ICANON :: 0x00000002; // Canonical input (erase and kill processing). ECHO :: 0x00000008; // Enable echo. ECHOE :: 0x00000010; // Visual erase for ERASE. ECHOK :: 0x00000020; // Echo NL after KILL. ECHONL :: 0x00000040; // Echo NL even if ECHO is off. - NOFLSH :: 0x00000080; // Disable flush after interrupt. + NOFLSH :: 0x00000080; // Disable flush after interrupt or quit. TOSTOP :: 0x00000100; // Send SIGTTOU for background output. IEXTEN :: 0x00008000; // Enable DISCARD and LNEXT. case .MACOS; - ISIG :: 0x00000080; // Enable signals. - ICANON :: 0x00000100; // Do erase and kill processing. + ISIG :: 0x00000080; // Enable signals INTR, QUIT, [D]SUSP. + ICANON :: 0x00000100; // Canonicalize input lines. ECHO :: 0x00000008; // Enable echo. ECHOE :: 0x00000002; // Visual erase for ERASE. ECHOK :: 0x00000004; // Echo NL after KILL. ECHONL :: 0x00000010; // Echo NL even if ECHO is off. NOFLSH :: 0x80000000; // Disable flush after interrupt. - TOSTOP :: 0x00400000; // Send SIGTTOU for background output. + TOSTOP :: 0x00400000; // Stop background jobs from output. IEXTEN :: 0x00000400; // Enable DISCARD and LNEXT. } } // Control Characters - TODO WIP - // LINUX : ??? + // LINUX : https://sourceware.org/git/glibc.git -> ./sysdeps/unix/sysv/linux/bits/termios-c_cc.h // MACOS : https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/termios.h.auto.html Control_Chars :: enum u8 { - VINTR :: 0; - VQUIT :: 1; - VERASE :: 2; - VKILL :: 3; - VEOF :: 4; - VTIME :: 5; // Time-out value (tenths of a second) [!ICANON]. - VMIN :: 6; // Minimum number of bytes read at once [!ICANON]. - VSWTC :: 7; - VSTART :: 8; - VSTOP :: 9; - VSUSP :: 10; - VEOL :: 11; - VREPRINT :: 12; - VDISCARD :: 13; - VWERASE :: 14; - VLNEXT :: 15; - VEOL2 :: 16; - } - + // Unused consts: + // VINTR, VQUIT, VERASE, VKILL, VEOF, VSWTC, VSTART, VSTOP, VSUSP, VEOL, VREPRINT, VDISCARD, VWERASE, VLNEXT, VEOL2 + + #if OS == { -#if USE_LIBC { - // Required to do unlocking input. - libc :: #system_library "libc"; + case .LINUX; + VTIME :: 5; // Time-out value (tenths of a second) [!ICANON]. + VMIN :: 6; // Minimum number of bytes read at once [!ICANON]. + case .MACOS; + VTIME :: 17; // Time-out value (tenths of a second) [!ICANON]. + VMIN :: 16; // Minimum number of bytes read at once [!ICANON]. + } + } + // https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcsetattr.c.html tcsetattr :: (fd: s32, optional_actions: s32, termios_p : *Terminal_IO_Mode) -> s32 #foreign libc; @@ -213,134 +196,6 @@ USE_LIBC :: true; // https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcflush.c.html tcflush :: (fd: s32, queue_selector: s32) -> s32 #foreign libc; -} -else { - - // https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcsetattr.c.html - tcsetattr :: (fd: s32, optional_actions: s32, termios_p : *Terminal_IO_Mode) -> s32 { - - #if OS == .LINUX { - TCSETS :: 0x5402; - TCSETSW :: 0x5403; - TCSETSF :: 0x5404; - tcflag_t :: u32; - cc_t :: u8; - __KERNEL_NCCS :: 19; - __kernel_termios :: struct { - c_iflag : tcflag_t; // input mode flags - c_oflag : tcflag_t; // output mode flags - c_cflag : tcflag_t; // control mode flags - c_lflag : tcflag_t; // local mode flags - c_line : cc_t; // line discipline - c_cc : [__KERNEL_NCCS]cc_t; // control characters - }; - - k_termios: __kernel_termios; - cmd: u64; - if optional_actions == { - case xx OptionalActions.TCSANOW; - cmd = TCSETS; - - case xx OptionalActions.TCSADRAIN; - cmd = TCSETSW; - - case xx OptionalActions.TCSAFLUSH; - cmd = TCSETSF; - - case; - return EINVAL; - } - // k_termios.c_iflag = termios_p.c_iflag & ~IBAUD0; - k_termios.c_iflag = xx termios_p.c_iflag; - k_termios.c_oflag = xx termios_p.c_oflag; - k_termios.c_cflag = xx termios_p.c_cflag; - k_termios.c_lflag = xx termios_p.c_lflag; - k_termios.c_line = xx termios_p.c_line; - // #if _HAVE_C_ISPEED && _HAVE_STRUCT_TERMIOS_C_ISPEED - // k_termios.c_ispeed = termios_p->c_ispeed; - // #endif - // #if _HAVE_C_OSPEED && _HAVE_STRUCT_TERMIOS_C_OSPEED - // k_termios.c_ospeed = termios_p->c_ospeed; - // #endif - memcpy(*k_termios.c_cc[0], *termios_p.c_cc[0], __KERNEL_NCCS * 1);//size_of(cc_t)); - return ioctl(fd, cmd, *k_termios); - } - #if OS == .MACOS { - // return __ioctl (fd, TIOCSETAF, termios_p); - #assert(false, "NOT IMPLEMENTED"); - } - return 0; - } - - // https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcgetattr.c.html - tcgetattr :: (fd: s32, termios_p: *Terminal_IO_Mode) -> s32 { - TCSETS :: 0x5402; - TCSETSW :: 0x5403; - TCSETSF :: 0x5404; - tcflag_t :: u32; - cc_t :: u8; - __KERNEL_NCCS :: 19; - __kernel_termios :: struct { - c_iflag : tcflag_t; // input mode flags - c_oflag : tcflag_t; // output mode flags - c_cflag : tcflag_t; // control mode flags - c_lflag : tcflag_t; // local mode flags - c_line : cc_t; // line discipline - c_cc : [__KERNEL_NCCS]cc_t; // control characters - }; - - - // int - // __tcgetattr (int fd, struct termios *termios_p) - // { - // struct __kernel_termios k_termios; - k_termios: __kernel_termios; - retval: int; - retval = ioctl(fd, TCGETS, *k_termios); - if retval == 0 { - termios_p.c_iflag = xx k_termios.c_iflag; - termios_p.c_oflag = xx k_termios.c_oflag; - termios_p.c_cflag = xx k_termios.c_cflag; - termios_p.c_lflag = xx k_termios.c_lflag; - termios_p.c_line = xx k_termios.c_line; - // #if _HAVE_STRUCT_TERMIOS_C_ISPEED - // # if _HAVE_C_ISPEED - // termios_p->c_ispeed = k_termios.c_ispeed; - // # else - // termios_p->c_ispeed = k_termios.c_cflag & (CBAUD | CBAUDEX); - // # endif - // #endif - // #if _HAVE_STRUCT_TERMIOS_C_OSPEED - // # if _HAVE_C_OSPEED - // termios_p->c_ospeed = k_termios.c_ospeed; - // # else - // termios_p->c_ospeed = k_termios.c_cflag & (CBAUD | CBAUDEX); - // # endif - // #endif - size_of_cc_t := __KERNEL_NCCS * 1; - memcpy(*termios_p.c_cc[0], *k_termios.c_cc[0], size_of_cc_t); - // memset(*termios_p.c_cc[0] + size_of_cc_t + 1, _POSIX_VDISABLE, (NCCS - __KERNEL_NCCS) * 1); - // - // if (sizeof (cc_t) == 1 || _POSIX_VDISABLE == 0 || (unsigned char) _POSIX_VDISABLE == (unsigned char) -1) { - // memset (__mempcpy (&termios_p->c_cc[0], &k_termios.c_cc[0], __KERNEL_NCCS * sizeof (cc_t)), _POSIX_VDISABLE, (NCCS - __KERNEL_NCCS) * sizeof (cc_t)); - // } - // else - // { - // memcpy (&termios_p->c_cc[0], &k_termios.c_cc[0], __KERNEL_NCCS * sizeof (cc_t)); - // for (size_t cnt = __KERNEL_NCCS; cnt < NCCS; ++cnt) { - // termios_p->c_cc[cnt] = _POSIX_VDISABLE; - // } - // } - } - return xx retval; - } - - // https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcflush.c.html - tcflush :: inline (fd: s32, queue_selector: s32) -> s32 { - TCFLSH :: 0x540B; - return ioctl(fd, TCFLSH, queue_selector); - } -} //////////////////////////////////////////////////////////////////////////////// @@ -396,7 +251,7 @@ OS_prepare_terminal :: () -> success := true { raw_tio_mode.c_cc[Control_Chars.VMIN] = 1; raw_tio_mode.c_cc[Control_Chars.VTIME] = 0; - error = tcsetattr(STDIN_FILENO, xx OptionalActions.TCSANOW, *raw_tio_mode); + error = tcsetattr(STDIN_FILENO, xx Optional_Actions.TCSANOW, *raw_tio_mode); if error { error_code, error_string := get_error_value_and_string(); log_error("Failed to set raw_tio_mode: code %, %", error_code, error_string); @@ -410,7 +265,7 @@ OS_prepare_terminal :: () -> success := true { OS_reset_terminal :: inline () -> success := true { restore_resize_handler(); - error := tcsetattr(STDIN_FILENO, xx OptionalActions.TCSANOW, *initial_tio_mode); + error := tcsetattr(STDIN_FILENO, xx Optional_Actions.TCSANOW, *initial_tio_mode); if error { error_code, error_string := get_error_value_and_string(); log_error("Failed to set initial_tio_mode: code %, %", error_code, error_string); @@ -420,7 +275,7 @@ OS_reset_terminal :: inline () -> success := true { } OS_flush_input :: inline () -> success := true { - error := tcflush(STDIN_FILENO, xx QueueSelector.TCIFLUSH); + error := tcflush(STDIN_FILENO, xx Queue_Selector.TCIFLUSH); if error { error_code, error_string := get_error_value_and_string(); log_error("Failed to flush input: code %, %", error_code, error_string); |
