-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
demo+usb: Display the device description obtained from the acquired d…
…evice Added rusb to the crates. Added libusb to the manifest. Signed-off-by: Hubert Figuière <[email protected]>
- Loading branch information
Showing
5 changed files
with
136 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,9 @@ | |
// Hubert Figuière <[email protected]> | ||
// | ||
|
||
use std::cell::RefCell; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use std::{cell::RefCell, collections::HashMap, os::fd::AsRawFd, sync::Arc}; | ||
|
||
use adw::{prelude::*, subclass::prelude::*}; | ||
use futures_util::{lock::Mutex, StreamExt}; | ||
use glib::clone; | ||
use gtk::glib; | ||
|
||
use ashpd::zbus::zvariant::ObjectPath; | ||
use ashpd::{ | ||
desktop::{ | ||
usb::{Device, UsbOptions, UsbProxy}, | ||
|
@@ -131,6 +124,22 @@ mod imp { | |
} | ||
} | ||
|
||
fn usb_describe_device(fd: &dyn AsRawFd) -> ashpd::Result<String> { | ||
let context = rusb::Context::new() | ||
.map_err(|_| ashpd::PortalError::Failed("rusb Context".to_string()))?; | ||
let handle = unsafe { context.open_device_with_fd(fd.as_raw_fd()) } | ||
.map_err(|_| ashpd::PortalError::Failed("open USB device".to_string()))?; | ||
let device = handle.device(); | ||
let device_desc = device.device_descriptor().unwrap(); | ||
Ok(format!( | ||
"Bus {:03} Device {:03} ID {:04x}:{:04x}", | ||
device.bus_number(), | ||
device.address(), | ||
device_desc.vendor_id(), | ||
device_desc.product_id() | ||
)) | ||
} | ||
|
||
pub(super) async fn refresh_devices(&self) -> ashpd::Result<()> { | ||
let page = self.obj(); | ||
|
||
|
@@ -152,54 +161,16 @@ mod imp { | |
row.connect_share_clicked(clone!( | ||
#[strong] | ||
page, | ||
move |row| { | ||
move |widget| { | ||
glib::spawn_future_local(clone!( | ||
#[strong] | ||
row, | ||
#[strong] | ||
device_id, | ||
#[strong] | ||
widget, | ||
#[strong] | ||
page, | ||
async move { | ||
let root = row.native().unwrap(); | ||
let identifier = WindowIdentifier::from_native(&root).await; | ||
let usb = UsbProxy::new().await.unwrap(); | ||
let result = usb | ||
.acquire_devices(&identifier, &[(&device_id, device_writable)]) | ||
.await; | ||
match result { | ||
Ok(resp) => { | ||
if resp.response().is_ok() { | ||
loop { | ||
let result = usb.finish_acquire_devices().await; | ||
match result { | ||
Ok(result) => { | ||
println!("result {result:?}"); | ||
if !result.1 { | ||
continue; | ||
} | ||
for device in &result.0 { | ||
page.imp().acquired_device(&device.0); | ||
} | ||
} | ||
Err(err) => { | ||
tracing::error!( | ||
"Finish acquire device error: {err}" | ||
); | ||
page.error(&format!( | ||
"Finish acquire device error: {err}" | ||
)); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
Err(err) => { | ||
tracing::error!("Acquire device error: {err}"); | ||
page.error(&format!("Acquire device error: {err}")); | ||
} | ||
} | ||
page.imp().share(&widget, &device_id, device_writable).await | ||
} | ||
)); | ||
} | ||
|
@@ -214,19 +185,7 @@ mod imp { | |
device_id, | ||
#[strong] | ||
page, | ||
async move { | ||
let usb = UsbProxy::new().await.unwrap(); | ||
let result = usb.release_devices(&[&device_id]).await; | ||
println!("{result:?}"); | ||
match result { | ||
Ok(_) => {} | ||
Err(err) => { | ||
tracing::error!("Acquire device error: {err}"); | ||
page.error(&format!("Acquire device error: {err}")); | ||
} | ||
} | ||
page.imp().released_device(&device_id); | ||
} | ||
async move { page.imp().unshare(&device_id).await } | ||
)); | ||
} | ||
)); | ||
|
@@ -235,6 +194,73 @@ mod imp { | |
Ok(()) | ||
} | ||
|
||
async fn finish_acquire_device(&self, usb: &UsbProxy<'_>, object_path: &ObjectPath<'_>) { | ||
loop { | ||
let result = usb.finish_acquire_devices(object_path).await; | ||
match result { | ||
Ok(result) => { | ||
println!("result {result:?}"); | ||
if !result.1 { | ||
continue; | ||
} | ||
for device in &result.0 { | ||
if let Ok(fd) = &device.1 { | ||
match Self::usb_describe_device(&Fd::from(fd)) { | ||
Ok(describe) => self.obj().info(&describe), | ||
Err(err) => self.obj().info(&err.to_string()), | ||
} | ||
} | ||
self.acquired_device(&device.0); | ||
} | ||
} | ||
Err(err) => { | ||
tracing::error!("Finish acquire device error: {err}"); | ||
self.obj() | ||
.error(&format!("Finish acquire device error: {err}")); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
async fn share(&self, widget: >k::Button, device_id: &String, device_writable: bool) { | ||
let root = widget.native().unwrap(); | ||
let identifier = WindowIdentifier::from_native(&root).await; | ||
let usb = UsbProxy::new().await.unwrap(); | ||
let result = usb | ||
.acquire_devices( | ||
identifier.as_ref(), | ||
&[Device(device_id.to_string(), device_writable)], | ||
) | ||
.await; | ||
match result { | ||
Ok(resp) => { | ||
if resp.response().is_ok() { | ||
let path = resp.path(); | ||
self.finish_acquire_device(&usb, path).await; | ||
} | ||
} | ||
Err(err) => { | ||
tracing::error!("Acquire device error: {err}"); | ||
self.obj().error(&format!("Acquire device error: {err}")); | ||
} | ||
} | ||
} | ||
|
||
async fn unshare(&self, device_id: &str) { | ||
let usb = UsbProxy::new().await.unwrap(); | ||
let result = usb.release_devices(&[device_id]).await; | ||
println!("{result:?}"); | ||
match result { | ||
Ok(_) => {} | ||
Err(err) => { | ||
tracing::error!("Acquire device error: {err}"); | ||
self.obj().error(&format!("Acquire device error: {err}")); | ||
} | ||
} | ||
self.released_device(device_id); | ||
} | ||
|
||
pub(super) async fn start_session(&self) -> ashpd::Result<()> { | ||
let usb = UsbProxy::new().await?; | ||
let session = usb.create_session().await?; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters