Skip to content

Commit

Permalink
Merge branch 'master' into dart-vdd-prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Dampfwalze committed Apr 11, 2024
2 parents 5f64625 + 3f80291 commit e0e4a69
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 167 deletions.
5 changes: 4 additions & 1 deletion examples/monitor_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,15 @@
# you can ask to be notified.
# this represents the complete current state of the driver
#
# DriverClient.receive(Callable[list[Monitor], None])
# DriverClient.receive(Optional[Callable[list[Monitor], None]])
client.receive(lambda d: print(d))
# one way to use this might be to auto update your driver instance
def set_monitors(data):
client.monitors = data
# calling it on a new callback will cancel the old one and set the new one
client.receive(set_monitors)
# calling it with no args will cancel the current receiver
client.receive()

# gets latest states from driver
#
Expand Down
14 changes: 12 additions & 2 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions rust/bindings/pyvdd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ crate-type = ["cdylib"]
pyo3 = "0.21.1"
driver-ipc = { path = "../../driver-ipc" }

[dependencies.windows]
version = "0.54.0"
features = ["Win32_Foundation", "Win32_System_Threading"]

[lints]
workspace = true
97 changes: 25 additions & 72 deletions rust/bindings/pyvdd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,22 @@

mod utils;

use std::fmt::Debug;
use std::{
borrow::{Borrow, Cow},
collections::HashSet,
fmt::Display,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
sync::atomic::{AtomicBool, Ordering},
};
use std::{fmt::Debug, sync::Mutex};

use driver_ipc::{
ClientCommand, Dimen, DriverClient, EventCommand, Id, Mode, Monitor, RefreshRate,
};
use driver_ipc::{Dimen, DriverClient, EventCommand, Id, Mode, Monitor, RefreshRate};
use pyo3::prelude::*;
use pyo3::{
exceptions::{PyIndexError, PyRuntimeError, PyTypeError},
pyclass::boolean_struct::False,
types::{DerefToPyAny, PyList, PyLong},
DowncastIntoError, PyClass, PyTypeCheck,
};
use windows::Win32::{
Foundation::{DuplicateHandle, DUPLICATE_SAME_ACCESS, HANDLE},
System::{
Threading::{GetCurrentProcess, GetCurrentThread},
IO::CancelSynchronousIo,
},
};

use self::utils::IntoPyErr as _;

Expand Down Expand Up @@ -336,7 +324,6 @@ impl IntoPyListIter for Py<PyTypedList> {
#[pyo3(name = "DriverClient")]
struct PyDriverClient {
client: DriverClient,
thread_registry: Arc<Mutex<Option<HANDLE>>>,
/// The list of monitors
/// Sig: list[Monitor]
#[pyo3(get)]
Expand All @@ -358,11 +345,7 @@ impl PyDriverClient {

let monitors = state_to_pytypedlist(py, client.monitors())?;

let slf = Self {
client,
monitors,
thread_registry: Arc::new(Mutex::new(None)),
};
let slf = Self { client, monitors };

Ok(slf)
}
Expand Down Expand Up @@ -417,58 +400,28 @@ impl PyDriverClient {
}

/// Get notified of other clients changing driver configuration
/// Sig: receive(Callable[list[Monitor], None])
fn receive(&self, callback: PyObject) {
// cancel the receiver internally if called again
{
let lock = self.thread_registry.lock().unwrap().take();
if let Some(thread) = lock {
unsafe {
_ = CancelSynchronousIo(thread);
}
}
/// Sig: receive(Optional[Callable[list[Monitor], None]])
fn receive(&self, callback: Option<PyObject>) {
if let Some(callback) = callback {
self.client.set_event_receiver(move |cmd| {
let EventCommand::Changed(data) = cmd else {
unreachable!()
};

Python::with_gil(|py| {
let state = state_to_pylist(py, &data);
let Ok(state) = state else {
return;
};

if let Err(e) = callback.call1(py, (state,)) {
e.print(py);
}
});
});
} else {
self.client.terminate_receiver();
}

let registry = self.thread_registry.clone();
self.client.set_receiver(
// init - store thread handle for later
Some(move || {
let mut lock = registry.lock().unwrap();

let pseudo_handle = unsafe { GetCurrentThread() };
let current_process = unsafe { GetCurrentProcess() };

let mut thread_handle = HANDLE::default();
unsafe {
_ = DuplicateHandle(
current_process,
pseudo_handle,
current_process,
&mut thread_handle,
0,
false,
DUPLICATE_SAME_ACCESS,
);
}

*lock = Some(thread_handle);
}),
// cb
move |command| {
if let ClientCommand::Event(EventCommand::Changed(data)) = command {
Python::with_gil(|py| {
let state = state_to_pylist(py, &data);
let Ok(state) = state else {
return;
};

if let Err(e) = callback.call1(py, (state,)) {
e.print(py);
}
});
}
},
);
}

/// Find a monitor by Id
Expand Down
2 changes: 1 addition & 1 deletion rust/driver-ipc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ thiserror = "1.0.58"
owo-colors = "4.0.0"
serde_json = "1.0.114"
windows = { version = "0.54.0", features = ["Win32_Foundation"] }
win-pipes = { git = "https://github.com/MolotovCherry/WinPipes-rs" }
lazy_format = "2.0.3"
joinery = "3.1.0"
winreg = "0.52.0"
tokio = { version = "1.37.0", features = ["full"] }
Loading

0 comments on commit e0e4a69

Please sign in to comment.