| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module termios |
| 5 | |
| 6 | // not used but needed for function declarations |
| 7 | type TcFlag = int |
| 8 | type Speed = int |
| 9 | type Cc = u8 |
| 10 | |
| 11 | // Termios represents platform dependent flags representing the terminal state. |
| 12 | // Linux https://github.com/lattera/glibc/blob/master/sysdeps/unix/sysv/linux/bits/termios.h |
| 13 | // OpenBSD https://github.com/openbsd/src/blob/master/sys/sys/termios.h |
| 14 | // FreeBSD https://web.mit.edu/freebsd/head/sys/sys/_termios.h |
| 15 | // Solaris https://github.com/omniti-labs/illumos-omnios/blob/master/usr/src/uts/common/sys/termios.h |
| 16 | // DragonFly https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/sys/_termios.h |
| 17 | // QNX https://github.com/vocho/openqnx/blob/master/trunk/lib/c/public/termios.h |
| 18 | pub struct Termios { |
| 19 | pub mut: |
| 20 | c_iflag TcFlag |
| 21 | c_oflag TcFlag |
| 22 | c_cflag TcFlag |
| 23 | c_lflag TcFlag |
| 24 | c_line Cc |
| 25 | c_cc [32]Cc |
| 26 | c_ispeed Speed |
| 27 | c_ospeed Speed |
| 28 | } |
| 29 | |
| 30 | // flag provides a termios flag of the correct size for the underlying C.termios structure. |
| 31 | pub fn flag(value int) TcFlag { |
| 32 | return TcFlag(value) |
| 33 | } |
| 34 | |
| 35 | // invert is a platform dependant way to bitwise NOT (~) TcFlag as its length varies across platforms. |
| 36 | // It is only implemented for Unix like OSes. |
| 37 | pub fn invert(value TcFlag) TcFlag { |
| 38 | return TcFlag(~int(value)) |
| 39 | } |
| 40 | |
| 41 | // tcgetattr is an unsafe wrapper around C.termios and keeps its semantic. |
| 42 | // It is only implemented for Unix like OSes |
| 43 | pub fn tcgetattr(fd int, mut t Termios) int { |
| 44 | $if wasm32_emscripten { |
| 45 | return 0 |
| 46 | } |
| 47 | eprintln('term.termios: tcgetattr,tcsetattr,ioctl,set_state are not implemented for the platform') |
| 48 | eprintln('tcgetattr, fd: ${fd}, t: ${t}') |
| 49 | return 0 |
| 50 | } |
| 51 | |
| 52 | // tcsetattr is an unsafe wrapper around C.termios and keeps its semantic. |
| 53 | // It is only implemented for Unix like OSes. |
| 54 | pub fn tcsetattr(fd int, optional_actions int, mut t Termios) int { |
| 55 | eprintln('tcsetattr, fd: ${fd}, optional_actions: ${optional_actions}, t: ${t}') |
| 56 | return 0 |
| 57 | } |
| 58 | |
| 59 | // ioctl is an unsafe wrapper around C.ioctl and keeps its semantic. |
| 60 | // It is only implemented for Unix like OSes |
| 61 | pub fn ioctl(fd int, request u64, arg voidptr) int { |
| 62 | eprintln('ioctl, fd: ${fd}, request: ${request}, arg: ${arg}') |
| 63 | return 0 |
| 64 | } |
| 65 | |
| 66 | // set_state applies the flags in the `new_state` to the descriptor `fd`. |
| 67 | pub fn set_state(fd int, new_state Termios) int { |
| 68 | eprintln('set_state, fd: ${fd} | new_state: ${new_state}') |
| 69 | return 0 |
| 70 | } |
| 71 | |
| 72 | // disable_echo disables echoing characters as they are typed, when that Termios state is later set with termios.set_state(fd,t). |
| 73 | pub fn (mut t Termios) disable_echo() { |
| 74 | t.c_lflag &= invert(8) |
| 75 | } |
| 76 | |