virtual-machine 0.0.4 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/console.rs DELETED
@@ -1,83 +0,0 @@
1
- //! Console handling for terminal I/O (not available in WASM).
2
-
3
- #![cfg(not(target_arch = "wasm32"))]
4
-
5
- use std::io::{self, Read};
6
- use std::sync::mpsc;
7
- use std::thread;
8
- use std::sync::atomic::{AtomicBool, Ordering};
9
- use std::sync::Arc;
10
-
11
- pub struct Console {
12
- rx: mpsc::Receiver<u8>,
13
- original_termios: Option<libc::termios>,
14
- running: Arc<AtomicBool>,
15
- }
16
-
17
- impl Console {
18
- pub fn new() -> Self {
19
- let (tx, rx) = mpsc::channel();
20
- let running = Arc::new(AtomicBool::new(true));
21
- let r_clone = running.clone();
22
-
23
- // Setup raw mode
24
- let mut original_termios = None;
25
- if unsafe { libc::isatty(libc::STDIN_FILENO) } == 1 {
26
- let mut termios = unsafe { std::mem::zeroed() };
27
- if unsafe { libc::tcgetattr(libc::STDIN_FILENO, &mut termios) } == 0 {
28
- original_termios = Some(termios);
29
- let mut raw = termios;
30
- unsafe {
31
- libc::cfmakeraw(&mut raw);
32
- libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, &raw);
33
- }
34
- }
35
- }
36
-
37
- thread::spawn(move || {
38
- let mut buf = [0u8; 1];
39
- let stdin = io::stdin();
40
- let mut handle = stdin.lock();
41
-
42
- while r_clone.load(Ordering::Relaxed) {
43
- // This read is blocking.
44
- // In a real app we might want non-blocking or select,
45
- // but for this simple VM, blocking thread is fine.
46
- if handle.read_exact(&mut buf).is_ok() {
47
- let b = buf[0];
48
- // Translate Ctrl-A x to exit?
49
- // Let's leave exit handling to the consumer or special key.
50
- // Typically Ctrl-A x is (1, 120).
51
- // We just pass raw bytes.
52
- if tx.send(b).is_err() {
53
- break;
54
- }
55
- } else {
56
- break;
57
- }
58
- }
59
- });
60
-
61
- Self {
62
- rx,
63
- original_termios,
64
- running,
65
- }
66
- }
67
-
68
- pub fn poll(&self) -> Option<u8> {
69
- self.rx.try_recv().ok()
70
- }
71
- }
72
-
73
- impl Drop for Console {
74
- fn drop(&mut self) {
75
- self.running.store(false, Ordering::Relaxed);
76
- if let Some(termios) = self.original_termios {
77
- unsafe {
78
- libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, &termios);
79
- }
80
- }
81
- }
82
- }
83
-