v / vlib / term / termios / termios_qnx.c.v
99 lines · 83 sloc · 2.34 KB · a87a4d73b9ab25cfff0822f4e94cf2a2d9e64323
Raw
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//
5// Serves as more advanced input method
6// based on the work of https://github.com/AmokHuginnsson/replxx
7//
8module termios
9
10#include <termios.h>
11#include <sys/ioctl.h>
12
13// https://github.com/vocho/openqnx/blob/master/trunk/lib/c/public/termios.h
14
15const cclen = 20
16
17type TcFlag = int
18type Speed = int
19type Cc = u8
20
21// Termios stores the terminal options
22pub struct C.termios {
23mut:
24 c_iflag TcFlag
25 c_oflag TcFlag
26 c_cflag TcFlag
27 c_lflag TcFlag
28 c_cc [cclen]Cc
29 reserved [3]u32
30 c_ispeed Speed
31 c_ospeed Speed
32}
33
34fn C.tcgetattr(fd i32, termios_p &C.termios) i32
35
36fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
37
38fn C.ioctl(fd i32, request u64, args ...voidptr) i32
39
40// flag provides a termios flag of the correct size
41// for the underlying C.termios structure
42@[inline]
43pub fn flag(value int) TcFlag {
44 return int(value)
45}
46
47// invert is a platform dependant way to bitwise NOT (~) TcFlag
48// as its length varies across platforms
49@[inline]
50pub fn invert(value TcFlag) TcFlag {
51 return ~int(value)
52}
53
54pub struct Termios {
55pub mut:
56 c_iflag TcFlag
57 c_oflag TcFlag
58 c_cflag TcFlag
59 c_lflag TcFlag
60 c_cc [cclen]Cc
61 reserved [3]u32
62 c_ispeed Speed
63 c_ospeed Speed
64}
65
66// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
67@[inline]
68pub fn tcgetattr(fd int, mut termios_p Termios) int {
69 unsafe {
70 return C.tcgetattr(fd, &C.termios(termios_p))
71 }
72}
73
74// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
75@[inline]
76pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
77 unsafe {
78 return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
79 }
80}
81
82// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
83@[inline]
84pub fn ioctl(fd int, request u64, arg voidptr) int {
85 unsafe {
86 return C.ioctl(fd, request, arg)
87 }
88}
89
90// set_state applies the flags in the `new_state` to the descriptor `fd`.
91pub fn set_state(fd int, new_state Termios) int {
92 mut x := new_state
93 return tcsetattr(0, C.TCSANOW, mut x)
94}
95
96// disable_echo disables echoing characters as they are typed, when that Termios state is later set with termios.set_state(fd,t).
97pub fn (mut t Termios) disable_echo() {
98 t.c_lflag &= invert(C.ECHO)
99}
100