Skip to content

Commit 2dca47a

Browse files
Merge pull request #119 from FrameworkComputer/touchscreen-win
touchscreen_win: Find the right path with hidapi
2 parents 169235a + 4fa875c commit 2dca47a

File tree

1 file changed

+63
-26
lines changed

1 file changed

+63
-26
lines changed

framework_lib/src/touchscreen_win.rs

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::touchscreen::TouchScreen;
1+
use hidapi::HidApi;
2+
use std::path::Path;
3+
4+
use crate::touchscreen::{TouchScreen, ILI_PID, ILI_VID};
25
#[allow(unused_imports)]
36
use windows::{
47
core::*,
@@ -19,35 +22,69 @@ pub struct NativeWinTouchScreen {
1922

2023
impl TouchScreen for NativeWinTouchScreen {
2124
fn open_device() -> Option<Self> {
22-
// TODO: I don't know if this might be different on other systems
23-
// Should enumerate and find the right one
24-
// See: https://learn.microsoft.com/en-us/windows-hardware/drivers/hid/finding-and-opening-a-hid-collection
25-
let path =
26-
w!(r"\\?\HID#ILIT2901&Col03#5&357cbf85&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}");
25+
debug!("Looking for touchscreen HID device");
26+
match HidApi::new() {
27+
Ok(api) => {
28+
for dev_info in api.device_list() {
29+
let vid = dev_info.vendor_id();
30+
let pid = dev_info.product_id();
31+
let usage_page = dev_info.usage_page();
32+
if vid != ILI_VID {
33+
trace!(" Skipping VID:PID. Expected {:04X}:*", ILI_VID);
34+
continue;
35+
}
36+
debug!(
37+
" Found {:04X}:{:04X} (Usage Page {:04X})",
38+
vid, pid, usage_page
39+
);
40+
if usage_page != 0xFF00 {
41+
debug!(" Skipping usage page. Expected {:04X}", 0xFF00);
42+
continue;
43+
}
44+
if pid != ILI_PID {
45+
debug!(" Warning: PID is {:04X}, expected {:04X}", pid, ILI_PID);
46+
}
2747

28-
let res = unsafe {
29-
CreateFileW(
30-
path,
31-
FILE_GENERIC_WRITE.0 | FILE_GENERIC_READ.0,
32-
FILE_SHARE_READ | FILE_SHARE_WRITE,
33-
None,
34-
OPEN_EXISTING,
35-
// hidapi-rs is using FILE_FLAG_OVERLAPPED but it doesn't look like we need that
36-
FILE_FLAGS_AND_ATTRIBUTES(0),
37-
None,
38-
)
39-
};
40-
let handle = match res {
41-
Ok(h) => h,
42-
Err(err) => {
43-
error!("Failed to open device {:?}", err);
44-
return None;
48+
debug!(" Found matching touchscreen HID device");
49+
debug!(" Path: {:?}", dev_info.path());
50+
debug!(" IC Type: {:04X}", pid);
51+
52+
// TODO: Enumerate with windows
53+
// Should enumerate and find the right one
54+
// See: https://learn.microsoft.com/en-us/windows-hardware/drivers/hid/finding-and-opening-a-hid-collection
55+
let path = dev_info.path().to_str().unwrap();
56+
57+
let res = unsafe {
58+
CreateFileW(
59+
&HSTRING::from(Path::new(path)),
60+
FILE_GENERIC_WRITE.0 | FILE_GENERIC_READ.0,
61+
FILE_SHARE_READ | FILE_SHARE_WRITE,
62+
None,
63+
OPEN_EXISTING,
64+
// hidapi-rs is using FILE_FLAG_OVERLAPPED but it doesn't look like we need that
65+
FILE_FLAGS_AND_ATTRIBUTES(0),
66+
None,
67+
)
68+
};
69+
let handle = match res {
70+
Ok(h) => h,
71+
Err(err) => {
72+
error!("Failed to open device {:?}", err);
73+
return None;
74+
}
75+
};
76+
77+
debug!("Opened {:?}", path);
78+
79+
return Some(NativeWinTouchScreen { handle });
80+
}
81+
}
82+
Err(e) => {
83+
error!("Failed to open hidapi. Error: {e}");
4584
}
4685
};
4786

48-
debug!("Opened {:?}", path);
49-
50-
Some(NativeWinTouchScreen { handle })
87+
None
5188
}
5289

5390
fn send_message(&self, message_id: u8, read_len: usize, data: Vec<u8>) -> Option<Vec<u8>> {

0 commit comments

Comments
 (0)