Add a VTE example

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2021-07-28 23:19:20 +04:00
parent 3b614f2e66
commit c8b236f092
3 changed files with 76 additions and 0 deletions

View File

@ -4,6 +4,7 @@ members = [
"qemu-display-listener",
"qemu-rdw",
"qemu-vnc",
"qemu-vte",
"xtask",
]
@ -21,3 +22,4 @@ rdw = { path = '/home/elmarco/src/rdw/rdw' }
gdk4-wayland = { path = '/home/elmarco/src/gtk4-rs/gdk4-wayland' }
gdk4-x11 = { path = '/home/elmarco/src/gtk4-rs/gdk4-x11' }
gtk4 = { path = '/home/elmarco/src/gtk4-rs/gtk4' }
gtk4-sys = { path = '/home/elmarco/src/gtk4-rs/gtk4/sys' }

16
qemu-vte/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "qemu-vte"
version = "0.1.0"
edition = "2018"
[dependencies]
log = "0.4"
pretty_env_logger = "0.4"
once_cell = "1.5"
zbus = { version = "2.0.0-beta" }
qemu-display-listener = { path = "../qemu-display-listener", features = ["glib"] }
futures = "0.3.13"
[dependencies.vte]
package = "vte4"
git = "https://gitlab.gnome.org/malureau/vte4-rs"

58
qemu-vte/src/main.rs Normal file
View File

@ -0,0 +1,58 @@
use glib::{clone, MainContext};
use qemu_display_listener::Chardev;
use std::os::unix::io::AsRawFd;
use std::os::unix::net::UnixStream;
use vte::prelude::*;
use vte::{glib, gtk, gio};
use zbus::azync::Connection;
use futures::prelude::*;
fn main() {
pretty_env_logger::init();
let app = gtk::Application::new(Some("org.qemu.vte-example"), Default::default());
app.connect_activate(move |app| {
let window = gtk::ApplicationWindow::new(app);
window.set_title(Some("D-Bus serial example"));
window.set_default_size(350, 70);
let term = vte::Terminal::new();
window.set_child(Some(&term));
MainContext::default().spawn_local(clone!(@strong window => async move {
let conn = Connection::new_session().await
.expect("Failed to connect to DBus")
.into();
if let Ok(c) = Chardev::new(&conn, "serial").await {
let (p0, p1) = UnixStream::pair().unwrap();
if c.proxy.register(p1.as_raw_fd().into()).await.is_ok() {
log::info!("ok");
let istream = unsafe { gio::UnixInputStream::take_fd(p0) }
.dynamic_cast::<gio::PollableInputStream>()
.unwrap();
let mut read = istream.into_async_read().unwrap();
loop {
let mut buffer = [0u8; 8192];
match read.read(&mut buffer[..]).await {
Ok(0) => break,
Ok(len) => {
term.feed(&buffer[..len]);
}
Err(e) => {
log::warn!("{}", e);
break;
}
}
}
}
}
}));
window.show();
});
app.run();
}